A Linux system administrator needs to know some Linux tricks. In this article, you will learn How to use find Command to search Directories more efficiently.
Everything is a file including directories in Linux. So follow this tutorial to see the different ways of finding a directory. One of the common things a Linux user will do within the command line is searching for a file or a directory.
Find, locate, and which command is one of the several different means and utilities used for searching for a file. But the last utility (which) is only used for locating a command.
How to use find Command to search Directories more efficiently
To accomplish the guide, we will focus on the find utility, which searches files on a live Linux filesystem and is more efficient and reliable as compared to locate.
But let’s explain a little about the downside of locate. It reads one or more databases created by updatedb, it does not search through a live filesystem. In addition, it does not as well offer flexibility regarding where to search from (starting point).
The ways of using ‘find’ Command
Have a look at the find command running.
locate [option] [search-pattern]
locate --basename '\pkg'
Please Note the option –basename or -b tells locate to only match the file (directory) basename (which is exactly pkg) but not the path (/path/to/pkg). Where \ is a globbing character, it disables the implicit replacement of pkg by *pkg*.
You can use find by following the simplified syntax below.
find starting-point options [expression]
To involve more, let us show you more examples.
Run the following command, where the -name flag reads the expression which in this case is the directory basename, to search for the same directory (pkg) above, within the current working directory.
find . -name "pkg"
In case you faced any “Permission denied” errors, use the sudo command.
sudo find . -name "pkg"
You can also prevent find from searching for other file types except for directories by using -type flag to specify the type of file.
sudo find . -type d -name "pkg"
Then, employ the action switch -ls, if you want to list the directory in a long listing format.
sudo find . -type d -name "pkg" -ls
Then, the option -iname will enable a case insensitive search:
sudo find . -type d -iname "pkg" sudo find . -type d -iname "PKG"
Please consider that you will be able to find more interesting and advanced usage information, by reading the man pages of find and locate.
man find man locate
Good job! you’re all set. At the end of this article, we prefer to mention again that the find command is more reliable and efficient for searching files in a Linux system when weighed against the locate command.
Dear user, we wish this tutorial how to use ‘find’ Command to search Directories more efficiently would be helpful for you, to ask any question or review the conversation of our users about this article, please visit Ask page. Also to improve your knowledge, there are so many useful tutorials ready for Eldernode training.