Linux provides different tools to find file for different attributes. We can find files according to their size, name, modification time location etc. But one of the most used metric is finding file according to its name. In this tutorial we will look different tools to find files by name in Linux.
find is the most feature-full tool to find files according to its name.
find provides a lot more features which can be examined in the following tutorial.
We will use
-name option which is used to specify the file name we search. In this example we will look file named
db.conf in this example.
$ find . -name db.conf
Using find Caseinsenstive
In previous example we have searched in a case sensitive manner. Case sensitive will only list same case names. For example if we search for
db.conf it will only match
db.conf contained name but will not match
DB.conf or similar. We disable case sensitivity and match
DB.conf too with the
-iname as we ca see just prefixing with
i . In this example we will search caseinsenstive.
$ find . -iname db.conf
We can also specify the location we can to search. In previous example we have all ready specified the location by using
. which means current working path. We can put other path. In this example we will search in
$ find /home/ismail -name db.conf
Locate is very fast and useful tool.
locate will search its database and try to match given term.
locate will match the all name and path for given term we can only look for the name with
-b option. This will only match file or folder name not the whole path. In this example we will search for
$ locate -b db.conf
Using ls and less
There is also a bit different and interesting way to search files.
ls command is used to list files and directories. We can also use
ls recursively where we can list sub files and directories too. We will redirect the
ls output to the
less command. Than we will use
less search function which is run with
/ to search in the output.
$ ls -R /home/ismail | less
db.conf with the following command in the less screen.