Find is an one of the versatile command and very useful to searches files, directory tree hierarchy on Linux and Unix. It's have many options and types for searching files from the directories. The following commands given different outputs,
Syntax :
find [PATH-NAME] [OPTIONS]
Basic usage:
1. Last Execute find command:
$ !find
2. Search Specific directory:
The Below command default will print all files. For example list out from csf directory,
$ find csf/
csf/
csf/csf.blocklists
csf/Crypt
csf/Crypt/Blowfish_PP.pm
csf/Crypt/CBC.pm
csf/sshalert.txt
3. Find a specific File :
The below examples to find a file linuxfaq.txt from /home path,
$ find /home/linuxfaq -name "linuxfaq.txt"
/home/linuxfaq/linuxfaq.txt
4. Find a file from current directory:
Do you want to specific file from current working directory use this,
$ find . -name "linuxfaq.txt"
5. Find Specific file extension:
The below command displays .php exetension only in /home/thelinuxfaq user directory tree.
$ find /home/thelinuxfaq -name "*.php"
./common/connection.php
./defpub.php
./classifieds.php
./feedback.php
./articles.php
6. case-insensitive searching:
$ find . -iname "*.PhP"
./index.php
./email.php
7. Set Limit maxdepth of directory traversal:
$ find . -type f -cmin 15 -prune
Find command by default searches entire directory tree recursively, can set limit maxdepth option searching traversal. Assume that, if you dont want to search more than 2 to 3 directory tree down levels, this maxdepth is helpful.
/home
../thelinuxfaq
../files
../paths
$ find /home/ -maxdepth 3 -name "*.php"
8. Find and print files only:
Find and print the files or images except directories from current working directory like, .txt, .sh, .jpg, etc.
$ find . -maxdepth 1 -type f
./named.conf
./.viminfo
./centos.txt
./vnstat-1.7-1.el5.rf.x86_64.rpm
./.tcshrc
9. Find Specific Directory Name:
$ find / -type d -name thelinuxfaq
10. Find and Print all graphic image files:
$ find . -type f -exec file {} \; | grep -o -P '^.+: \w+ image'
./pgrouting/src/common/doc/functions/images/before_node_net.png: PNG image
./pgrouting/src/common/doc/functions/images/after_node_net.png: PNG image
./pgrouting/doc/static/images/ccbysa.png: PNG image
./pgrouting/doc/static/images/pgrouting.png: PNG image
11. Find Empty Files or Directory:
Find all Empty Files,
$ find /tmp -type f -empty
Find all empty directory,
$ find /tmp -type d -empty
12. Find a file and remove it.
$ find . -type f -name "linuxfaq.php" -exec rm -f {} \;
Part V – Find Files and Directories Based on Size
Find files or Directories by modified times:
Also, possible to searches files or directories based on last modified day, hour, minutes. We can easily to understand by the following commands,
Options:
-atime its determine for last accessed time of file.
-ctime its determine for last
Arithmetic operator + sign is used to search for greater than,
Arithmetic oeprator - sign is used to search for less than,
No If there is no sign specified to exact.
13. Days :
The below command find all modified exact 5 days the file from currenty directory
$ find . -mtime 5
.
./hosts
./hostname
./hostconfig.json
./config.json
The below command find all modified more than 5 days the file from currenty directory,
$ find . -mtime +5
The below command find all modified less than 5 days the file from currenty directory
$ find . -mtime -5
14. Minutes :
$ find . -type f -mmin -60 -prune
15. Find hidden files
$ find /root -type f -name ".*"
16. Find and change permission:
$ find . -type f -name "*.php" -exec chmod 755 {} \;
File Permission:
To find and print files with 777 permission,
$ find /root -type f -perm 0777
/root/download/csf/netblock.txt
/root/download/csf/loadalert.txt
/root/download/csf/resalert.txt
/root/download/csf/changelog.txt
Find executable permission :
$ find / -perm /a=x
Find files without 755 permission,
$ find / -type f ! -perm 755
Find SFID files:
$ find / -perm /g+s
Find Files with 755 Permissions and Chmod to 0644
$ find / -type f -perm 0755 -print -exec chmod 0644 {} \;
Find directories with 755 permission and chmod to 644
$ find / -type d -perm 0777 -print -exec chmod 0755 {} \;
Comments (0)