How to find a file in Linux command with examples

If you are looking to a solution with which you can easily find a file in Linux OS and largest or smallest among them with some commands, then you are at the right place. No matter which type of desktop manager you use.

If you want to find a file graphically in Linux then you can use Nautilus in Gnome, dolphin in KDE, and Thunar in Xfce. Now let’s discuss how you can find a file in Linux using the command line or terminal.

Commands to find a file in Linux

The find command

The find command is used to find all the files that are present in the current directory.

For example :

If you want to find all the files present in the desktop directory then you just need to navigate to the desktop and fire the command find and this will show you all the files present in desktop.

~cd Desktop/

~/Desktop$ find .

The dot indicates the current directory.

The result :

Exampleprogram.txt

Helloworldprogram.txt

Codingnotes.txt

example.txt

If you know the approximate name of the file then The find command is also very useful. Because this command along with  -name argument is used to find the file related to specific characters.

You just need to navigate to the directory where you want to search a file then write the find command along with the approximate name and this command will show you all the files related to that approximate name.

For example

~cd Desktop/

~/ Desktop$ find . -name pro\*

The result :

Exampleprogram.txt

Helloworldprogram.txt

This command is showing all the file names that are including given specific character pro.

The locate command

The locate command is a little bit faster than the find command because the find command searches the result in real-time while the locate command uses a previously built database.

You can update the database when you want you just need to fire up the command sudo updatedb.

The locate command will give you all the files on the system that contains your search criteria fully matchable or partially matchable.

For example :

$ locate program

Result :

~home/john/Desktop/program.txt

~home/john/Documents/JAVA_programs

The locate command shows all the folder or the file that contains programs.

The which command

The which command is suitable for creating the shortcut of the program on the desktop.

Because this command will return the absolute path of executable.

~$which chrome

~/user/bin/chrome

This command will only display the one matching executable. If you want to display all the executable then you need to add -an option with the command.

The  Whereis command

The whereis command is mainly used for finding where the binary, pages, and source files are located.

For example :

~$ whereis firefox

~$/user/bin/firefox/  etc/firefox  /user/share/man/man1/firefox.1.gz

If you don’t want the path of source and man pages and you only want the path of executable then you need to use the -b along with whereis.

For example :

~$ whereis -b firefox

~$/user/bin/firefox

If you only want the source then you need to add -s instead of -b and this will give you source pages.

For example :

~$ whereis -s firefox

If you want to locate only the man pages then you need to replace -b with -m.

For example:

~$whereis -m firefox

Basic Difference between which and whereis command:

  1. The which command is only able to show you the location of the binary for a given command while whereis command can show you the location of the binary, source, and the man pages for a command.
  2. The whereis command display all the things that it finds. While the which command only shows the first executable that it finds.

Determining the file type in Linux

The file command is used to determine the file type in Linux.

The file command runs three sets of tests: first is the file system test, second is the magic number test and the third is the language test. If the first test succeeds then the file type to be printed.

For example :

~$ file example.txt

~$ ASCII text

~$ file example.tar

~$ POSIX tar archive(GNU)

~$ file pictures/

~$ directory

Finding the size of the directory :

The du command is used to find the size of the directory. This command will display the disk usage of the given directory.

For example :

Now let’s find out the size of directory pictures in a human-readable format.

~$ sudo du -sh /pictures

Result :

80GB     /pictures

Explanation of the command :

sudo – The command starts with sudo because of most of the files in the given directory own by the root user and not accessible for the normal users.

s – only display the total size of the specified directory.

h - this is responsible for displaying the size of the file in a human-readable format.

/pictures  the path of the directory.

Conclusion :

In Linux, lots of predefined commands are present you just have the right knowledge of it and Linux will become easy for you. We hope that our guide would be helpful to you in finding your file.

Leave a Comment