Find by name
[root@lsy study]# find ./ -name "*eth0" #Find the name * eth0 under the current directory
[root@lsy study]# find ./ -iname "*eth0" #i ignore case query
Find by size
#Find the file in the / etc directory that is larger than 5M, and then use the - LS parameter to display in a long format (- ls and the system's ls are not a command)
[root@lsy study]# find /etc -size +5M -ls
#Find the file in the / etc directory that is larger than 5M, and use the system's ls to display it in a long format.
[root@lsy study]# find /etc -size +5M |xargs ls -lh
Find by type
Find the type of the current directory is file, and ignore the case. Find the name * - eth0, which is displayed in long format.
[root@lsy study]# find ./ -type f -iname "*-eth0" -ls
Find the type of the current directory is directory, and ignore the case. Find the name * - eth0, which is displayed in long format.
[root@lsy study]# find ./ -type d -iname "*-eth0" -ls
Find the type of linked file under / bin, ignore the case, find the name b * and display it in long format.
[root@lsy study]# find /bin/ -type l -iname "b*" -ls
[root@lsy study]# find /dev/ -type b -ls
[root@lsy study]# find /dev/ -type c -ls
[root@lsy study]# find /etc/ -type f -size +3M -name "hw*"
Find in time
+7. Based on the current time, find the content 7 days ago
[root@lsy study]# find ./ -type f -name "file*" -mtime +7
[root@lsy study]# find ./ -type f -name "file*" -mtime +7 -delete
-7. Find the documents of the last 7 days and print the documents of the day.
[root@lsy study]# find ./ -type f -name "file*" -mtime -7
Find day 7 file
[root@lsy study]# find ./ -type f -mtime 7
Find by user
#Look up the home directory. The type is directory and the owner is jack. At the same time, look up only one layer.
[root@lsy study]# find /home/ -maxdepth 1 -type d -user jack
#Find the home directory. The type is directory and the group is hr. At the same time, find only one level.
[root@lsy study]# find /home/ -maxdepth 1 -type d -group hr -ls
#Find the home directory. The type is directory and the owner is jack. The group is hr. At the same time, find only one layer.
[root@lsy study]# find /home/ -maxdepth 1 -type d -user jack -group hr -ls
#Look up the home directory. The type is directory, the owner is jack, or the group is hr.
[root@lsy study]# find /home/ -maxdepth 1 -type d -user jack -o -group hr|xargs ls -ld
#Look up the home directory. The type is that the directory has no owner.
[root@lsy study]# find /home/ -maxdepth 1 -type d -nouser -ls
#Look up the home directory. The type is that the directory does not belong to a group.
[root@lsy study]# find /home/ -maxdepth 1 -type d -nogroup -ls
#Find the home directory. The type is that the directory has no owner or group.
[root@lsy study]# find /home/ -maxdepth 1 -type d -nouser -o -nogroup |xargs ls -ld
Find with permission
Permissions to find files exactly are 644
[root@lsy study]# find ./ -type f -perm 644
#Include 444 permission, i.e. greater than or equal to 444
[root@lsy study]# find ./ -type f -name "file*" -perm -444
#Find global writable (each permission must contain w)
[root@lsy study]# find . -perm -222 -ls
#Include set uid
[root@lsy study]# find /usr/sbin -perm -4000 -ls
#Include set gid
[root@lsy study]# find /usr/sbin -perm -2000 -ls
#Include sticky
[root@lsy study]# find /usr/sbin -perm -1000 -ls
Include action
-delte,Only files can be deleted,If you want to delete a directory,You need to make sure the directory is empty,Otherwise, it cannot be deleted.
[root@lsy study]# find ./log/ -type f -name "*.log" -delete
-ok,Any custom command can be executed,But you will be prompted to confirm.
[root@lsy study]# find /etc/ -name "ifcfg*" -ok cp -vp {} /tmp \;
-exec
[root@lsy study]# find /etc/ -name "ifcfg*" -exec cp -vp {} /tmp \;
[root@lsy study]# find log/ -type d -exec cp -rpv {} /tmp \;
[root@lsy study]# find test/ -type f -exec rm -f {} \;
More examples
1.lookup/tmp Under the directory, the owner is not root,And the file name is not c File at the beginning
[root@lsy study]# find /tmp/ ! -user root ! -name "c*"
/tmp/ttt
2.lookup/var Directory is subordinate to root,And belong to group. mail All files for
[root@lsy study]# find /var/ -type f -user root -a -group mail -ls
3.lookup/var Directory does not belong to root,lp,gdm All files for the group
[root@lsy study]# find /var/ -type f ! \( -group root -o -group lp -o -group gdm \) |xargs ls -ld
4.lookup/var The file modified in the last week in the directory is not owned by root,Neither postfix Documents
[root@lsy study]# find /var/ -type f -mtime -7 ! -user root ! -name "postfix"
5.lookup/etc Greater than 1 in directory M All documents of common type
[root@lsy study]# find /etc/ -type f -size +1M
6.take/etc/All directories in(Directory only)Copy to/tmp The directory structure does not change
[root@lsy study]# find /etc/ -type d -exec mkdir -p /tmp/{} \;
7.take/etc Directory copy to/var/tmp/,/var/tmp/etc All directory permissions for 777/var/tmp/etc All files in directory permission 666
[root@lsy study]# cp -rp /etc/ /var/tmp/
[root@lsy study]# find /var/tmp/etc/ -type d -exec chmod 777 {} \;
[[root@lsy study]# find /var/tmp/etc/ -type f -exec chmod 666 {} \;
[root@lsy study]# find /var/tmp/etc/ ! -type d -exec chmod 666 {} \;
8.Retain/var/log/Next last 7 days log file,Delete all others
[root@lsy study]# find /var/log/ -type f -mtime +7 -delete
9.Establish touch file{1..10}10 File, Retain file9,Delete all other times
[root@lsy study]# find ./ -type f ! -name "file9" -delete
10.
mkdir /root/dir1
touch /root/dir1/file{1..10}
find /root/dir1 -type f -name "file5"
find /root/dir1 ! -name "file5"
find /root/dir1 -name "file5" -o -name "file9"
find /root/dir1 -name "file5" -o -name "file9" -ls
find /root/dir1 \( -name "file5" -o -name "file9" \) -ls
find /root/dir1 \( -name "file5" -o -name "file9" \) -exec rm -rvf {} \;
find /root/dir1 ! \( -name "file4" -o -name "file8" \) -exec rm -vf {} \;