find命令总结

//查找为txt后缀的文件,然后复制到/data下改为.bak格式。
[root@i-3pedyvud tmp]# find / -name "*.txt" -exec cp {} /data/{}.bak \;


//所有用户都有特殊权限的文件,然后ls
[root@i-3pedyvud tmp]# find / -perm -7000 -ls
find: ‘/proc/21235/task/21235/fd/6’: No such file or directory
find: ‘/proc/21235/task/21235/fdinfo/6’: No such file or directory
find: ‘/proc/21235/fd/5’: No such file or directory
find: ‘/proc/21235/fdinfo/5’: No such file or directory
8691755   76 -rwsrwsrwt   1 root     root        73888 Aug  9  2019 /usr/bin/chage


//任何用户有特殊权限的文件,然后ls
[root@i-3pedyvud tmp]# find / -perm /7000 -ls


//批量创建用户和删除用户
[root@i-3pedyvud tmp]# echo user{1..10} | xargs -n1 useradd
useradd: user 'user1' already exists
useradd: user 'user2' already exists
useradd: user 'user3' already exists
useradd: user 'user4' already exists
useradd: user 'user5' already exists
useradd: user 'user6' already exists
useradd: user 'user7' already exists
useradd: user 'user8' already exists
useradd: user 'user9' already exists
useradd: user 'user10' already exists
[root@i-3pedyvud tmp]# echo user{1..10} | xargs -n1 userdel
[root@i-3pedyvud tmp]# 

查找/目录下排除/usr、/proc、/sys、/etc、/var、/boot 、/run的文件

find / \( -path "/usr/*" -o -path "/proc/*" -o -path "/sys/*" -o -path "/etc/*" -o -path "/var/*" -o -path "/boot/*" -o -path "/run/*" \) -prune -o -type f -print


查找/目录下排除/usr、/proc、/sys、/etc、/var、/boot 、/run的文件之后的txt文件

find / \( -path "/usr/*" -o -path "/proc/*" -o -path "/sys/*" -o -path "/etc/*" -o -path "/var/*" -o -path "/boot/*" -o -path "/run/*" \) -prune -o -name "*.txt" -type f -print

查找/目录下排除/usr、/proc、/sys、/etc、/var、/boot 、/run的文件之后的txt文件 

find / \( -path "/usr/*" -o -path "/proc/*" -o -path "/sys/*" -o -path "/etc/*" -o -path "/var/*" -o -path "/boot/*" -o -path "/run/*" \) -prune -o -type f -print -ls | sort -nr -k7|grep txt

查找/目录下排除/usr、/proc、/sys、/etc、/var、/boot 、/run的文件之后按第七列(文件大小)排序


[root@i-3pedyvud ~]# find / \( -path "/usr/*" -o -path "/proc/*" -o -path "/sys/*" -o -path "/etc/*" -o -path "/var/*" -o -path "/boot/*" -o -path "/run/*" \) -prune -o -type f -print -ls | sort -nr -k7

查找/目录下排除/usr、/proc、/sys、/etc、/var、/boot 、/run的文件之后按第七列(文件大小)排序并排除/*/所有的隐藏文件

find / \( -path "/usr/*" -o -path "/proc/*" -o -path "/sys/*" -o -path "/etc/*" -o -path "/var/*" -o -path "/boot/*" -o -path "/run/*" -o -path "/*/.*" \) -prune -o -type f -print -ls | sort -nr -k7
简名全名中文作用
atimeAccess Time访问时间

最后一次访问文件(读取或执行)的时间

ctimeChange Time变化时间

最后一次改变文件(属性或权限)或者目录(属性或权限)的时间

mtimeModify Time修改时间

最后一次修改文件(内容)或者目录(内容)的时间

什么命令会修改atime、ctime、mtime

命令atimemtimectime
mvNNY
cpYNN
chmodNNY
chownNNY
touchYYY
>、>>YYY
vim、vi(不修改)YNN
vim、vi(修改)YYY
./test.shYNN

为什么修改内容,三个时间都会变呢?

因为对文件编辑之后,不仅访问了文件(改变了atime),还增删了内容,这个文件的大小等状态也发生了变化,所以mtime、ctime变化也很正常

当前目录下1天内修改过的文件,如果无,则显示当前目录下的文件(即ls -l)

[root@i-3pedyvud ~]# find . -type f -mtime -1| xargs ls -l

当前目录下6天-7天之间修改过的文件,如果无,则显示当前目录下的文件(即ls -l)

[root@i-3pedyvud bashdb]# find . -type f -mtime 6 | xargs ls -l  (表示文件修改时间距离当前为6天的文件,即距离当前时间6天(6*24小时-6*24+24小时)的文件。
-rw-r--r-- 1 root root  2388 1月  11 16:52 ./bashdb-main.inc
-rw-r--r-- 1 root root  2884 1月  11 16:52 ./bashdb-part2.sh
-rw-r--r-- 1 root root  5690 1月  11 16:52 ./bashdb-trace
-rw-r--r-- 1 root root  4910 1月  11 16:52 ./command/action.sh
-rw-r--r-- 1 root root  1475 1月  11 16:52 ./command/alias.sh
-rw-r--r-- 1 root root  4278 1月  11 16:52 ./command/backtrace.sh
-rw-r--r-- 1 root root  5219 1月  11 16:52 ./command/break.sh
-rw-r--r-- 1 root root  1069 1月  11 16:52 ./command/clear.sh
-rw-r--r-- 1 root root  1975 1月  11 16:52 ./command/commands.sh
-rw-r--r-- 1 root root  1835 1月  11 16:52 ./command/complete.sh
-rw-r--r-- 1 root root  1924 1月  11 16:52 ./command/condition.sh
-rw-r--r-- 1 root root  2115 1月  11 16:52 ./command/continue.sh

查找当前目录下的文件

[root@i-3pedyvud ~]# find . -type f -exec ls -l {} \;
-rw-r--r--. 1 root root 18 Dec 29  2013 ./.bash_logout
-rw-r--r--. 1 root root 176 Dec 29  2013 ./.bash_profile
-rw-r--r--. 1 root root 176 Dec 29  2013 ./.bashrc
-rw-r--r--. 1 root root 100 Dec 29  2013 ./.cshrc
-rw-r--r--. 1 root root 129 Dec 29  2013 ./.tcshrc
-rw-r--r-- 1 root root 392 Nov  4 11:37 ./.ssh/authorized_keys
-rw-r--r--. 1 root root 26464 Dec 31 00:05 ./.bash_history
-rw------- 1 root root 1024 Dec  9 11:12 ./.rnd
-r--r--r-- 1 root root 249827 Dec 14 09:58 ./ca-bundle.trust.crt
-rw-r--r-- 1 root root 1606 Dec 14 09:58 ./eccabcgkmangcn.crt

查找当前目录下后缀为txt的目录拷贝到123目录 (-t将所有文件的参数复制至目标目录)

[root@i-3pedyvud ~]# find ./ -type f -name "*.txt" | xargs cp -t ./123
[root@i-3pedyvud ~]# ll 123
总用量 12
-rw-r--r-- 1 root root 4217 1月   3 14:42 1.txt
-rw-r--r-- 1 root root    0 1月   3 14:42 remove.txt
-rw-r--r-- 1 root root  566 1月   3 14:42 script.txt
查找当前目录下后缀为txt的目录拷贝到123目录 (-t将所有文件的参数复制至目标目录)

root@i-3pedyvud ~]# find ./ -type f -name "*.txt" | xargs -i cp {} -t ./123
[root@i-3pedyvud ~]# ll 123
总用量 12
-rw-r--r-- 1 root root 4217 1月   3 14:50 1.txt
-rw-r--r-- 1 root root    0 1月   3 14:50 remove.txt
-rw-r--r-- 1 root root  566 1月   3 14:50 script.txt
[root@i-3pedyvud ~]# 

查找当前目录下后缀为txt的目录拷贝到123目录 (-t将所有文件的参数复制至目标目录)

[root@i-3pedyvud ~]# cp $(find ./ -type f -name "*.txt") -t 123
[root@i-3pedyvud ~]# ll 123
总用量 12
-rw-r--r-- 1 root root 4217 1月   3 14:55 1.txt
-rw-r--r-- 1 root root    0 1月   3 14:55 remove.txt
-rw-r--r-- 1 root root  566 1月   3 14:55 script.txt
[root@i-3pedyvud ~]# 


查找当前目录下后缀为txt的目录拷贝到123目录 (-t将所有文件的参数复制至目标目录)

[root@i-3pedyvud ~]#  find ./ -type f -name "*.txt" -exec cp -t ./123 {} \;
[root@i-3pedyvud ~]# ll 123
总用量 12
-rw-r--r-- 1 root root 4217 1月   3 14:59 1.txt
-rw-r--r-- 1 root root    0 1月   3 14:59 remove.txt
-rw-r--r-- 1 root root  566 1月   3 14:59 script.txt
[root@i-3pedyvud ~]# 
 


 

exec解释:
-exec  参数后面跟的是command命令,它的终止是以;为结束标志的,所以这句命令后面的分号是不可缺少的,考虑到各个系统中分号会有不同的意义,所以前面加反斜杠。
{}   花括号代表前面find查找出来的文件名。
使用find时,只要把想要的操作写在一个文件里,就可以用exec来配合find查找,很方便的。在有些操作系统中只允许-exec选项执行诸如l s或ls -l这样的命令。大多数用户使用这一选项是为了查找旧文件并删除它们。建议在真正执行rm命令删除文件之前,最好先用ls命令看一下,确认它们是所要删除的文件。 exec选项后面跟随着所要执行的命令或脚本,然后是一对儿{ },一个空格和一个\,最后是一个分号。为了使用exec选项,必须要同时使用print选项。如果验证一下find命令,会发现该命令只输出从当前路径起的相对路径及文件名。

查找出大于10M的文件

find /root -type f -size 10M

注:关于 “ -size 10M ” 在大小单位上是允许 “+” “-” 操作的。即 “+10M” 大于10M, “-10M” 小于10M 。

#mtime参数的理解应该如下:
-mtime n 按照文件的更改时间来找文件,n为整数。
n表示文件更改时间距离为n天, -n表示文件更改时间距离在n天以内,+n表示文件更改时间距离在n天以前。
例如:
-mtime 0 表示文件修改时间距离当前为0天的文件,即距离当前时间不到1天(24小时)以内的文件。
-mtime 1 表示文件修改时间距离当前为1天的文件,即距离当前时间1天(24小时-48小时)的文件。
-mtime+1 表示文件修改时间为大于1天的文件,即距离当前时间2天(48小时)之外的文件
-mtime -1 表示文件修改时间为小于1天的文件,即距离当前时间1天(24小时)之内的文件

为什么-mtime+1 表示文件修改时间为大于1天的文件,即距离当前时间48小时之外的文件,而不是24小时之外的呢?
因为n值只能是整数,即比1大的最近的整数是2,所有-mtime+1不是比当前时间大于1天(24小时),而是比当前时间大于2天(48小时)。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值