Linux Tutorial: Finding Files in Linux
- Last Updated On: March 19, 2014
- By: Scriptcrunch Editorial
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
- Which
- Whereis
- Locate
- 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
Scriptcrunch Editorial
Other Interesting Blogs
[80% OFF] Linux Foundation Coupon for November 2024
Hi Techies, I wanted to let you know about a pretty sweet deal with the Linux Foundation Coupon that is running now.
[40% OFF] Linux Foundation LFCA, LFCS & LFCT Exam Voucher Codes
Linux Foundation has announced up to a $284 discount on its Linux certification programs Linux Foundation Certified IT Associate (LFCA) and Linux
CKA Certification Study Guide (Certified Kubernetes Administrator)
This comprehensive CKA certification exam study guide covers all the important aspects of the Certified Kubernetes Administrator exam and useful resources. Passing