List of content you will read in this article:
Linux systems are based on the files and directories structure. It provides you various ways in order to find a directory within the system. One of the major tasks of any user working on Linux is to find the file or directory. The Linux system provides various utilities that help in searching the file using the command-line interface. Some commands that can be used are- find, locate, and which. But the last command, "which," is used primarily for looking for the specific command.
Within this guide, we are majorly focusing on the find command due to its efficiency and reliability as compared to other search commands in searching for a file or directory. The locate command is not commonly used due to its downside, as it looks for one or more databases that are created by the updatedb
command and do not consider the live file system. Also, the locate command is not flexible for searching from a starting point.
Syntax of the locate command
You can use the locate command with the following syntax.
# locate [option] [search-pattern]
We are explaining why the locate command is neglected in comparison to the find command. Here, we are searching for the "pkg" directory within the current working directory.
In the below-mentioned command, the option "--basename" or "-b" helps the locate command to only match the file or directory basename but does not match the path. Here, "\" is a gobbling character and disables the replacement of pkg by *pkg*.
$ locate --basename '\pkg'
In the above output, locate will start the search from the root directory and return the other directories with the same name. So, to avoid such an issue, we have moved to the find command using the below syntax.
Syntax of find command
$ find starting-point options [expression]
Suppose we want to search for the same directory as mentioned above, "pkg" within the current working directory. You can use the following command where the "-name" option will help read the expression, the directory basename.
$ find . -name "pkg"
If in case, after running the above command, you will get the "permission denied" error, you can use the sudo
command as mentioned below.
$ sudo find . -name "pkg"
If you want to restrict the find command from searching the other file types except for directories, you can use the -type
flag along with the find command that will specify the file type. Here, -d
represents the directory.
$ sudo find . -type d -name "pkg"
If you want to list down the directory in a long listing format, you can use the "ls" along with the find command, as seen below.
$ sudo find . -type d -name "pkg" -ls
Another option is the "-iname" that will implement the case-sensitive search.
$ sudo find . -type d -iname "pkg"
$ sudo find . -type d -iname "PKG"
If you want to implement more advanced usage, you can check the man pages of find and locate using the below command.
$ man find
$ man locate
Conclusion
With Linux's various commands, you can make your work easier and more efficient. One of the commonly used commands is for searching a file or directory from the system. For this, you can use the locate command and the find command as per your choice. Both commands work well but in different situations that we have mentioned.
You can go through this article to understand the use of the find and locate commands on CentOS 8. If you have any questions or suggestions, please leave them in the comment section below.
People Are Also Reading: