find Linux systems

01 Linux system Find command

find Overview

Linux findCommand to find the file in the specified directory. Any string parameters located before will be considered directory name you want to find. If you use this command does not set any parameters, the find command will find subdirectories and files in the current directory. And will look into all the subdirectories and files are displayed.

find [path] [type] [Action] [Content]

find parameters

-name:指定名称

-user:按文件属主查找
-group:按文件属组查找
-path:文件/目录匹配查找
-type:文件类型
  -f   #普通文件
  -d   #目录
  -c   #字符
  -b   #块设备
  -s   #socket
  -l   #链接文件 

-mtime:按修改时间查找
-atime:按文件访问时间查找
-ctime:按文件创建时间查找
#时间数字:+7 七天以前、7 第七天、-7 最近七天

-perm:按权限查找

-maxdepth:查找深度

-size:+10k 大于10k、-100k 小于100k

find name lookup

1. Environmental Simulation

rm -rf /data/
mkdir -p /data
touch /data/{a,b,c}.txt

[root@xmh ~]# ls -l /data/
total 0
-rw-r--r-- 1 root root 0 Jan 27 20:35 a.txt
-rw-r--r-- 1 root root 0 Jan 27 20:35 b.txt
-rw-r--r-- 1 root root 0 Jan 27 20:35 c.txt

2. Practice

//查找/data目录下a.txt文件
[root@xmh ~]# find /data/ -name "a.txt"
/data/a.txt

//查找/data目录下以.txt结尾的文件
[root@xmh ~]# find /data/ -name "*.txt"
/data/a.txt
/data/b.txt
/data/c.txt

find Typefind

1. Environmental Simulation

rm -rf /data/
mkdir /data/{dir1,dir2,link} -p
touch /data/{a..c}.txt
ln -s /data/link /data/sort_link

2. Practice

-type:文件类型
  -f   #普通文件
  -d   #目录
  -c  #字符
  -b  #块设备
  -s  #socket
  -l  #链接文件 
  
//查找/data目录下的所有普通文件
[root@xmh ~]# find /data/ -type f
/data/a.txt
/data/b.txt
/data/c.txt

//查找/data目录下的所有软链接
[root@xmh ~]# find /data/ -type l
/data/sort_link

//查找/data目录下的所有目录
[root@xmh ~]# find /data/ -type d
/data/
/data/dir1
/data/dir2
/data/link

find size Find

1. Environmental Simulation

rm -rf /data/
mkdir /data -p
touch /data/{a..f}.txt
seq 100000 >/data/a.txt  
seq 59000 >/data/b.txt     
seq 20000 >/data/c.txt  
seq 999999 >/data/d.txt  
seq 9999999 >/data/e.txt  
seq 999999999 >/data/f.txt 

[root@xmh ~]# ls -lh /data/
total 9.3G
-rw-r--r-- 1 root root 576K Jan 27 20:15 a.txt
-rw-r--r-- 1 root root 335K Jan 27 20:15 b.txt
-rw-r--r-- 1 root root 107K Jan 27 20:15 c.txt
-rw-r--r-- 1 root root 6.6M Jan 27 20:16 d.txt
-rw-r--r-- 1 root root  76M Jan 27 20:16 e.txt
-rw-r--r-- 1 root root 9.3G Jan 27 20:16 f.txt

2. Practice

-size:+10k 大于10k、-100k 小于100k

//查找/data目录下大于100K并小于300K的文件
[root@xmh ~]# find /data/ -size +100k -size -300k |xargs  ls -lh
-rw-r--r-- 1 root root 107K Jan 27 20:15 /data/c.txt

//查找/data目录下大于500K并小于100M的文件
[root@xmh ~]# find /data/ -size +500k -size -100M  |xargs  ls -lh
-rw-r--r-- 1 root root 576K Jan 27 20:15 /data/a.txt
-rw-r--r-- 1 root root 6.6M Jan 27 20:16 /data/d.txt
-rw-r--r-- 1 root root  76M Jan 27 20:16 /data/e.txt

//查找/data目录下大于1G的文件
[root@xmh ~]# find /data/ -size +1G |xargs ls -lh
-rw-r--r-- 1 root root 9.3G Jan 27 20:16 /data/f.txt

find permission lookup

1. Environmental Simulation

rm -rf /data/
mkdir /data -p
touch /data/{a..c}.txt
chmod 777 /data/a.txt    
chmod 600 /data/b.txt  
chmod 000 /data/c.txt 

2. Practice

-perm:按权限查找

[root@xmh ~]# ll /data/
total 0
-rwxrwxrwx 1 root root 0 Jan 27 20:20 a.txt
-rw------- 1 root root 0 Jan 27 20:20 b.txt
---------- 1 root root 0 Jan 27 20:20 c.txt

//查找/data目录下权限为777的文件
[root@xmh ~]# find /data/ -perm 777  |xargs ls -l
-rwxrwxrwx 1 root root 0 Jan 27 20:20 /data/a.txt

//查找/data目录下权限为600的文件
[root@xmh ~]# find /data/ -perm 600  |xargs ls -lh 
-rw------- 1 root root 0 Jan 27 20:20 /data/b.txt

//查找/data目录下权限为000的文件
[root@xmh ~]# find /data/ -perm 000  |xargs ls -lh   
---------- 1 root root 0 Jan 27 20:20 /data/c.txt

find users to find

1. Environmental Simulation

rm -rf /data/
mkdir /data -p
touch /data/{a..d}.txt
chown sa.sa /data/a.txt
chown xmh.xmh /data/c.txt
chown xmh /data/d.txt

[root@xmh ~]# ll -l /data/
total 0
-rw-r--r-- 1 sa   sa   0 Jan 27 20:26 a.txt
-rw-r--r-- 1 root root 0 Jan 27 20:26 b.txt
-rw-r--r-- 1 xmh  xmh  0 Jan 27 20:26 c.txt
-rw-r--r-- 1 xmh  root 0 Jan 27 20:26 d.txt

2. Practice

-user:按文件属主查找
-group:按文件属组查找

//查找/data目录下属于sa用户的文件
[root@xmh ~]# find /data/ -user sa |xargs ls -l
-rw-r--r-- 1 sa sa 0 Jan 27 20:24 /data/a.txt

//查找/data目录下属于root用户的文件
[root@xmh ~]# find /data/ -type f  -user root  |xargs ls -l
-rw-r--r-- 1 root root 0 Jan 27 20:26 /data/b.txt

//查找/data目录下属于root用户组的文件
[root@xmh ~]# find /data/ -type f -group root |xargs  ls -l
-rw-r--r-- 1 root root 0 Jan 27 20:26 /data/b.txt
-rw-r--r-- 1 xmh  root 0 Jan 27 20:26 /data/d.txt

find time to find

//时间数字:+7 七天以前、7 第七天、-7 最近七天
-mtime:按修改时间查找
-atime:按文件访问时间查找
-ctime:按文件创建时间查找

//按修改时间查找七天以前的文件
[root@xmh ~]# find / -mtime +7

//按文件访问时间查找最近七天的文件
[root@xmh ~]# find / -atime -7

//按文件创建时间查找第七天的文件
[root@xmh ~]# find / -atime 7

find action

1. Environmental Simulation

rm -rf /data/
mkdir /data -p
touch /data/{a..z}.txt

2. Practice

//结合exec,查找/data目录下类型为 f  且名称为a.txt的文件删除
[root@xmh ~]# find /data/ -type f -name "a.txt" -exec rm {} \;

//结合管道,查找/data目录下所有以.txt的文件删除
[root@xmh ~]# find /data -type f -name "*.txt" |xargs  rm -rf  

//结合管道,查找/var/log目录,删除15天以前修改过的文件
[root@xmh ~]# find /var/log/  -type f  -mtime +15 |xargs rm -f


结合exec:将查找到的文件传给"{}",表示对前面查找到的文件,做"rm"动作
结合管道:将查找到的文件传给管道符xargs rm -f(xanrgs把需要删除的内容变成一行,最后接rm -f 删除,相当于rm -f /data/a.txt /data/b.txt)

find exclude directory lookup

//排除/usr和/tmp目录
[root@xmh ~]# find / ! -path "/usr/*" ! -path "/tmp/*" -type f -name "test_*"

Guess you like

Origin www.cnblogs.com/jasonminghao/p/12310381.html