Updated April 17, 2023
Introduction to Find Directory Linux
In Linux, everything is considered as a file that also includes directories. One thing a Linux user will do in common is searching for a directory or a file. There are different ways to search a file or directory in Linux. Commands such as Locate, find and which are used for searching a file or directory. However, the utility command is used only for locating the command. In this article, we are going to learn about Find Directory Linux and will see how to search for files and directories using various command-line options.
Using Locate Command
The locate command is much faster than find command since the locate command does not search through the real file system. It makes use of the database which is previously built. This command will return a list of path names that matches the search criteria.
The locate command usually searches all the files in the system starting from the root and displays the results that matches all or some part of the criteria.
# locate [option] [search pattern]
Let us assume that we are looking for a directory called kgf in the present working directory.
Use the command shown below.
$ locate --basename '\kgf'
The above command searches all the files or directory name which matches kgf. The following output is produced.
From the output, it can be seen that the locate command will start searching from the root directory. Therefore files that contain the same name in different directories are matched and displayed on the screen.
To overcome this issue find command is used.
The find command helps us to look up for files for which we know only the approximate names. In simple words, the find command searches for a file in the current working directory and recursively through the subdirectories that matches the given search criteria. You can search for files by name, extension, group, modification date, permissions, etc.
To display all files which are present in the current working directory use the following command.
The dot (.) symbol indicates the current working directory.
Common Syntax of Find Command
$ find [options] [starting path] [expression]
- The find command’s behavior and optimization methods are controlled by the options attribute.
- The starting path attribute will specify the directory from where the find command should begin the search.
- The expression attribute will control the test that searches the directory hierarchy.
Options Used in Find Command
It specifies the base of the file name that matches the shell pattern.
This option ignores the case and performs a search which is case insensitive.
This will print the full name of the file in the output screen.
This option will display the current file in the ls -dils format in the output screen.
This option will list only folders and directories.
This will only search for files.
This will search for files or folders which don’t match the test criteria.
This will search the current directory and subdirectories upto X levels deep.
Optimization options [-01, -02, -03]
The find option uses optimization for enhancing performance. There are three optimization levels 01, -02, and -03
- -01: This is the default optimization and the search is based on the file name.
- -02: This one prioritizes the filename search and then runs the file type search.
- -03: This level allows the find command to rearrange the search tests based on efficiency and the likelihood of success.
How to Find Directory in Linux?
Suppose you want to find a directory named apk in the root file system, use the following command.
$ find / -type d -name "apk"
$ sudo find / -type d -name "apk"
If the output shows the permission denied message, add 2>/dev/null at the end of the command.
$ find / -type d -name "apk" 2>/dev/null
How to Find Directory in Linux Named Projects in the Home Directory?
To search for Projects directory present in the $HOME directory, use the following command.
$ find $HOME -type d -name Projects
How to get a Detailed List of Files and Directories?
Use the -ls option to list the present file in the ls output format.
$ find / -name "apk" –ls
How to List only Directories?
If you want to search only the directories and skip the file names use the -type d option as shown below.
$ find / -type d -name "apk" -ls
How to Perform Case Insensitive Search?
By default, the search is case sensitive. To ignore case sensitive search use the -iname option as shown below.
$ find / -type d -iname "apk" -ls
$ find / -type d -iname "apk"
Finding Files by Modification Time
Find command can also be used to search a file based on its last modification time.
$ find / -name "*png" -mtime -5
$ find /home/john/ -name "png" -mtime -4
The first command searches all the files in the files system and returns files that end with characters “png” which is modified in the last 5 days. The second command searches the home directory of the user named john and returns the files that end with characters “png” which is modified in the last 4 days.
Other Common Examples of Finding a file Using Find Command
$ find . -name samplefile.txt
The above command searches for a file named samplefile.text in the current directory and its sub-directories.
$ find /home -name *.png
This command will search for all files with the extension .png in the /home directory and directories beneath it.
$ find . -type f -empty
This will search for an empty file in the current directory.
Using which Command
This command will display the absolute path of the executable file or program. This is useful for creating a shortcut for the program in the desktop or in the desktop file manager.
$ which firefox
We hope that this EDUCBA information on “Find Directory Linux” was beneficial to you. You can view EDUCBA’s recommended articles for more information.