How to Find a File in Linux/Ubuntu from the Command Line?


Linux/Ubunu’s Terminal is very powerful and it enables all operations via the command line, for example, fast and efficient file management, which saves a lot of time as compared to performing file management via GUI. However, it is difficult to find files in the Linux/Ubuntu Command Line as compared to other popular operating systems like Windows and macOS that enable search by “File Explorer” and “Finder” respectively. Additionally, It gets even more difficult while managing the server where the only option is to use the Command Line.

For this purpose, Linux/Ubuntu offers different commands that help to search files efficiently. Among these commands, the most useful/ commands are stated below:

  • Find Command.
  • Locate Command.
  • Fd-find Command.
  • Grep Command.
  • Which Command.
  • Whereis Command.

How to Find a File via Find Command?

The find command is a helpful and powerful command to search files and directories when it is known where to search for the files. The find command searches in the file system in real-time, thus taking time depending on the number of files in the system. Find command supports numerous options to optimize the search for files and directories such as search by name, type, size, modifications, permissions,

How to Find a Single File by Name?

To find a file by name, run the command below:


The above output returns the path of the searched files, i.e., “LinuxGenie.txt”, “LinuxGenie.png”, “LinuxGenie.docx” and “LinuxGenie.xlsx”. “LinuxGenie.txt”, “LinuxGenie.docx”, and “LinuxGenie.xls” are found in the “Extra” directory while “LinuxGenie.png” is found in the “Screenshots” directory.

How to Find a Single File by Name in the Current Directory?

To find a single file by name in the current directory, run the below:

$ find . -name LinuxGenie.txt
$ find . -name LinuxGenie.png

Where “.” finds files in the current path.

The above output returns the path of the searched file, i.e., “LinuxGenie.txt”, and “LinuxGenie.png”. “LinuxGenie.txt” is found in the “Extra” directory while “LinuxGenie.png” is found in the “Screenshots” directory.

How to Find Multiple Files by Name in a Specific Directory?

To find multiple files by name in a specific directory, run the following command:

$ find /home/linuxuser/Extra ‘LinuxGenie’

Where “/home/linuxuser/Extra” is the path of the file.

The above output returns all files with the name “LinuxGenie”, i.e., “LinuxGenie.docx”, “LinuxGenie.txt”, and “LinuxGenie.xlsx”.

How to Find Multiple Files by Name with All Extension?

To find multiple files by name with all extensions, execute the following command:

$ find -name ‘LinuxGenie.*’

The above output returns all files with the name “LinuxGenie”, i.e., “LinuxGenie.png”, “LinuxGenie.docx”, “LinuxGenie.txt”, and “LinuxGenie.xlsx” in the current directory.

How to Find Empty Files?

To find empty files, execute the following command:

$ find /home/linuxuser/Extra -empty

Where the “empty” is a flag to search for the empty files.

The above output returns all the empty files.

How to Find File by Size?

Files can be searched by size via the find command. To search files with respect to file size, use the following flags:

  • c: bytes.
  • b: 512-byte
  • k: Kilobytes
  • M: Megabytes
  • G: Gigabytes

 

For example, to find files less than 2MB in the “Extra” directory:

$ find /home/linuxuser/Extra -type f -size -2M

Where:

  • /home/linuxuser/Extra: path to look for files.
  • Type: flag for searching type of file.
  • f: regular file
  • 2M: 2 Megabytes

The above output returns all the files that are less than 2MB.

Additionally, to find files greater than 18 MB in the “Home” directory, execute the following command:

$ find /home/linuxuser -type f -size +18M

The above output returns all the files that are greater than 18MB in the “Home” directory.

Finally, to find files between 5 MB to 10 MB in the “Home” directory, execute the following command:

$ find /home/linuxuser/ -type f -size +5 -size 10M

The above output returns all the files between 5 MB to 10 MB in the “Home” directory.

How to Find Files by Modification Time?

To find all text files in the “Home” directory that have been modified in the last three days, execute the following command:

$ find /home/linuxuser -name “*.txt” -mtime 3

Where:

  • /home/linuxuser: the path to find files.
  • *.txt: Search all text files.
  • -mtime: Modification Time.

The above output retrieves all the files that have been updated in the past three days.

Additionally, to find all “.png” files in the “Home” that have been modified in more than fifteen days, execute the following command:

$ find /home/linuxuser -name “*.png” -mtime +15

The above output returns all the files that have been altered in the past fifteen days.

How to Locate Files Based on Access Time?

To find all “.txt” files in the “Home” that have been accessed in the last three days, execute the following command:

$ find /home/linuxuser -name “*.txt” -atime 3

Where “-atime” is a flag to access time.

The output retrieves all the files that have been accessed in the specified duration (i.e., the past three days).

How to Locate Files by Change Time?

To find all “.txt” files in the “Home” that have been changed in the last two days, execute the following command:

$ find /home/linuxuser -name “*.txt” -ctime 2

Where “ctime” is a flag to change time

The above output returns all the files that have been changed in the last two days.

How to Find/Locate Files by Permissions?

To find the files by permissions, execute the following command.

$ find /home/linuxuser/Extra -perm 664

Where:

  • -prem: flag to find files with permissions.
  • 664: read & write privileges for user and group while read-only for others.

The above output returns all the files according to specified privileges.

How to Find a File via Locate Command?

The locate command is used when a user doesn’t know where to search for a file(s). The locate command is faster as compared to the find command as it searches the file in its own pre-built database in real-time. In order to use the locate command effectively, the database of the filesystem is required to be updated regularly using the “updatedb” command. The locate command is not installed on Ubuntu by default. To install the “locate”, first update the system repositories by running the following command:


Then, execute the following command to install the locate command:

$ sudo apt install plocate

Press Y to continue with the installation process:

The above output indicates the successful installation of the locate command.

How to Find/Locate a Single File by Name?

To find a single file by name, execute the following commands:

$ locate LinuxGenie.txt
$ locate LinuxGenie.png

The above command returns an absolute path of searched files, i.e., LinuxGenie.txt and LinuxGenie.png.

How to Find Multiple Files with the Same Name but Different Extensions?

To find multiple files by the same name but different extensions, execute the following command:


Where “b” searches the file names only.

The above output returns the absolute path of searched files. Additionally, it can be observed from the output that there are multiple files with the name “LinuxGenie” but all files have different extensions, i.e., “.docx”, “.txt”, “.xlsx” and “.png”.

How to Find a File via Fdfind Command?

The syntax of findfd is quite similar to the syntax of find but fdfind command is faster than the find command. This is due to multithreading being enabled on fdfind command, resulting in fast search results. The fdfidn command is not pre-installed on Ubuntu. To install the “fdfind”, first update the system repositories by running the following command:


Then, execute the following command to install fd-find:

$ sudo apt install fd-find

The above output indicates the successful installation of the fdfind. In order to find all the files/directories by the name of “LinuxGenie” in the current directory, run the following command:

The above output returns four files, i.e., LinuxGenie.xls, LinuxGenie.txt, LinuxGenie.docx, LinuxGenie.png and one directory “LinuxGenie” by the name Linux Genie.

How to Utilize Grep Command to Locate a File?

The grep command filters out string patterns from text-based formats such as text files and streams. Additionally, the grep command filters out all the files containing a specific string pattern. In order to find all files containing a string pattern “LinuxGenie”, first navigate to the desired directory via the cd command and execute the following command:

$ cd
$ grep -l LinuxGenie *

Where:

  • l: returns files with matches
  • *: search all files in the directory

The output filters out three files, i.e., LinuxGenie.dox, LinueGenie.txt and LinuxGenie.xls in the directory “Extra” that contained the string pattern “LinuxGenie”.

How to Find a File via Which Command?

Which command is utilized to find the binary of a command. Additionally, which command returns the absolute path of the command. To find a binary via which command, run the following command:

$ which find
$ which locate

The above output shows the absolute path of the binary of the find and locate command.

How to Find a File via Whereis Command?

Whereis command is utilized to find the binary of a command. Additionally, “whereis” command returns the absolute path along with the manual page files of the searched command. To find a binary via “whereis” command, run the following command:

$ whereis find
$ whereis locate

The above output shows the absolute path of the binary along with the manual page files of the find and locate command.

Conclusion

Files can be searched effectively via the command line interface in Linux/Ubuntu systems via find, locate, and fdfind commands. In addition to this, the grep command can be executed to fetch files containing a certain string pattern. Last but not least, users can execute the which and whereis commands to locate the binary of the specified command. This article demonstrated different ways of finding a file via the Command Line Interface on Linux/Ubuntu systems.

Print Friendly, PDF & Email