linux-finding-files

Linux Tutorial: Finding Files in Linux

Note: In Linux, everything is treated as files.

Even folders are treated as files.

There will be situations where you might probably want to find a file in your Linux system. You can find files and folders in Linux using the following commands

  1. Which
  2. Whereis
  3. Locate
  4. Find

Which:

Which command returns the location of the executable’s ( installer files present in your system) which you are looking for. For example, a search for apache web server executable (apache2 (Ubuntu), httpd (centos) ) can be done using the following command.

which apache2

The above command will return the location of the apache web server executable in the system.

Whereis:

Whereis command returns the location of binaries, executable’s and the man pages of the file you are looking for. Whereis command is associated with three options, -s, -b and –m. when you use whereis command with –s switch, it returns the source file, -b returns only binary and –m returns the man pages.

whereis apache2

The above command returns the source, binary and man pages.

whereis –s apache2

The above command returns only the source if the source exists.

whereis –m apache2
The above command returns the man pages for apache2.

Locate:

Locate command is used to locate files on your system. Before using the locate command, you have to update you db because there is a database which stores all the locations of your files in the system. This db file can be found in the following location

/var/lib/mlocate/mlocate.db

This database has to be updated in order to use the locate utility efficiently. This can be done by running the updatedb utility in the terminal.

updatedb

You have to run the updatedb utility every time in order to get the latest changes made to the file system. For example, you ran the updatedb and after that, you created a new file demo.txt . If you try to find demo.txt using locate, it won’t return any output. If you run updated utility and use the locate command again, you would be able to find the demo.txt file location.
You can locate files using the following command.

locate filenae

Find:

Find is an awesome utility to locate files on your system. You can find files based on name and using the find utility.

Finding files using name

You can search for a specific file by its name by using –name switch with the find command.

find –name demo.txt

Finding files using type

You can search for a specific file by its type (eg: directory) by using –type switch with the find command.

find –type d –name demo

Finding file based on size

Using find, you can list files based on size.

find ~ -size -50M

The above command lists the files which are less than 50 MB

find ~ -size +50MB

The above command lists the file which is above 50 MB

kindly share this post and leave a comment for feedback

Other Interesting Blogs

Leave a Comment

Share via
Copy link
Powered by Social Snap