How to Find a File from any Directory?

In Linux, file management plays an important role to keep everything organized. It is beneficial for sharing information easily, overcoming data loss risks, providing a backup, and many others. All the files are stored in directories. The files stored in a directory depend on the directory size. Sometimes the directory size limit exceeds then it is necessary to find files for moving to another location.

This post enlists and explains possible methods to find a file from any directory.

Method 1: Using the “find” Command

The “find” is the simplest built-in command line tool to search and find files and directories based on their name, size, extension, type, modified date, and many other associated parameters.

Let’s see how this command is beneficial to find files from any directory.

Example 1: Find a File by Name 

Execute the “find” command with the “name(search file name)” flag to get the mentioned file in the current directory:

$ find . -name file1.txt

The “file1.txt” occurs in the current directory and subdirectory, and also trash bin has been displayed in the terminal followed by its absolute paths.

Note: The “.(dot)” indicates the current directory in the above output. It can also be replaced by the combination of  “~/(tilde slash)”.

For Specific Directory

Specify the absolute path of the directory after the “find” command from which the user wants to search the targeted file by name:

$ find /home/itslinuxfoss/Documents -name file1.txt

The “file1.txt” file has been found from the “Documents” directory.

Example 2: Find a File by Extension

The “find” command allows the user to find the file using its extension. For this instance, all files having “.txt” extensions are found from the specified “Documents” directory:

$ find /home/itslinuxfoss/Documents -name *.txt

There is only one “.txt” file in the “Documents” directory.

Example 3: Find a File by Size

Sometimes, the user needs to find the larger files that take up much space in the directory. 

For this purpose, specify the size limit in integers, i.e., 10 MB using the supported “size” flag of the “find” command in this way:

$ find ~/ -size +10M -ls

The “ls” command is used to show the files information like permissions, owner, group and especially size:

In the output, the highlighted blocks shows the files sizes in “Mb” units which are greater than “10MB”.

Example 4: Find a File by Modified Time

In Linux, modifications occur in files continuously or after a specified time interval. 

To find a file based on its modification time, use the “mtime(maximum time)” flag of the “find” command:

The number of days also specified, i.e., “3” in this case:

$ find /home -name "*.txt" -mtime -3

The “home” directory, subdirectories, and the trash bin contain the above displayed “.txt” files modified before 3 days.

Example 5: Find a File by Content

The “find” command also allows the users to find a file by the content, i.e., searching a specific word or line. For this purpose, follow the below-mentioned “find” command:

$ find /home/itslinuxfoss/Downloads -type f -exec grep "This" '{}' \; -print

The above command holds the following additional parameters:

  • type f: Denotes the file type i.e “f(for file)”, and “d(for directory)”.
  • exec: Search the matching result and return “0” for its successful completion.
  • grep: Represents the “grep” command that filters out the mentioned constant from the files.
  • {}: The “exec” uses {} for passing the current file content/name to the specified command. 
  • \: Identifies the end of the command’s arguments. 
  • print: Prints the specified file content in the terminal.

The string “This” is printed on the screen present in the “Downloads” directory files having extensions “.txt” and “.pdf”.

Method 2: Using the “tree” Command

The external Unix/Linux “tree” command corresponds to the “recursive directory” that provides the directory information in a hierarchical format.

 It does not come by default, so before usage, it needs to be installed as per Linux distribution:

$ sudo yum install tree                             #For CentOS/RHEL
$ sudo dnf install tree                             #For Fedora
$ sudo apt install tree                             #For Debian/Ubuntu

The “tree” command line utility has been installed successfully.

Example 1: Find File By Extension

Run the “tree” command with the uppercase “-P(prints the matched files)” to view the “Pictures” directory files having “.pdf” extensions:

$ find /home/itslinuxfoss/Pictures -P “*.pdf”

The “Pictures” directory contains “4” pdf files.

Example 2: Find All Files From the Directory

Apart from specific files the user can also find all the files available in the targeted directory. For this purpose use the “-a(all)” flag of the tree command:

$ find /home/itslinuxfoss/Pictures -a

The output shows the multiple extension files present in the “Pictures” directory.

Method 3: Using the “locate” Command

The “locate” is an external command line tool to quickly find the files/directories from the Linux system. It is considered an alternative to the “find” command because of its efficiency and reliability.

Install “mlocate” Tool

First, install the “mlocate” command line utility in the desired Linux distributions using their default package managers:

$ sudo yum install mlocate                          #For CentOS/RHEL
$ sudo dnf install mlocate                          #For Fedora
$ sudo apt install mlocate                          #For Ubuntu/Debian-based

The “mlocate” command utility has been successfully installed in current Ubuntu 22.04 system

Example 1: Find a File by Name

Specify the name of the file with the “locate” command to find out the mentioned file from the database:

$ locate file

All the files that match to searched “file” name have been found and displayed in the terminal.

Example 2: Find a File by Extension

Execute the “locate” command with its “n(number of files/directories)” flag to find the desired no of files by extension:

$ locate -n 5 ".pdf"

The “5” files having “.pdf” extensions are displayed from the terminal.

Conclusion

In Linux, the “find”, “tree”, and “locate” commands can be utilized to find the file from the current or the particular directory. Each command displays the file from itself, subdirectories, and the trash bin in an organized manner using its supported options. This post has provided all methods to find a file from any directory in Linux.