INTRODUCTION
For beginners who dealings first time with a Linux machine face one problem, how to find files and folders they looking for.
In this guide we will cover how to use find command with examples. This will help you to find files and folders on your Linux using bash shell.
SOLUTION
Finding files by Name:
`Easiest way to find files is by name:
# find /path/to/target/directory -name "fileName"
Let’s take one example:
Suppose you remember only the file name which is “backup.sh”, but forget the location of file. In this condition I will use find command and search required file in whole system by giving the location “/” (mean root of file system).
See the command in action:
# find / -name "backup.sh"
Search Result:
/home/haider/backup.sh /backup.sh
The result of above command is showing I have this file on multiple location, command found this file on “/” (root) and other copy is in home directory of user haider which is /home/haider
Note: This command dealing all the keywords as case sensitive, meaning a search for backup.sh is different than Backup.sh.
In case, you forget the name exactly if it was in capital words or not.
Here is command to search files, but ignore the case of the filename.
# find / -iname "backup.sh"
Search Result:
/home/haider/backup.sh /Backup.sh /backup.sh
Now it found two files, the difference is only upper and lower character “B” in file name.
Find something by specifying type
Common types are:
- d: directory
- f: regular file
- b: block device
- l: symbolic link
< class="post-text">For example if you want to find directory called haider. you will use following command:
# find / -type d -iname "haider"
Output:
/home/haider /userProfiles/haider
By Extensions
For instance, you want to search all files ending with .conf from /etc/ directory.
see command:
# find /etc -type f -name "*.conf"
Sample Output:
/etc/yum.conf /etc/systemd/user.conf /etc/systemd/journald.conf /etc/systemd/system.conf /etc/systemd/bootchart.conf /etc/systemd/logind.conf /etc/rsyslog.conf /etc/tmpfiles.d/tuned.conf /etc/httpd/conf.modules.d/00-mpm.conf /etc/httpd/conf.modules.d/00-dav.conf /etc/httpd/conf.modules.d/01-cgi.conf /etc/httpd/conf.modules.d/00-lua.conf /etc/httpd/conf.modules.d/00-systemd.conf /etc/httpd/conf.modules.d/10-php.conf /etc/httpd/conf.modules.d/00-proxy.conf /etc/httpd/conf.modules.d/00-base.conf /etc/httpd/conf/httpd.conf /etc/httpd/conf.d/userdir.conf /etc/httpd/conf.d/welcome.conf /etc/httpd/conf.d/php.conf /etc/httpd/conf.d/autoindex.conf /etc/host.conf /etc/NetworkManager/NetworkManager.conf
Next: Search Text within Files in Linux
If you like this Post, please give us your valuable feedback by pressing Vote Up / Vote Down Button. Thanks.
[thumbs-rating-buttons]