find命令

语法

find [路径] [类型] [动作]

-name

*.conf   结尾
.conf*   开头
*conf*   包含
.conf    精确查找

[root@oldboy ~]# find   查找当前目录下的所有目录或文件,不加目录默认显示当前目录
.     #
./.viminfo
./.certs
./.certs/qq.crt

[root@localhost ~]# find /etc/ -name '*.sh'
/etc/profile.d/colorgrep.sh
/etc/profile.d/colorls.sh
/etc/profile.d/which2.sh
/etc/profile.d/less.sh
/etc/profile.d/256term.sh
/etc/profile.d/lang.sh
/etc/profile.d/vim.sh
/etc/dhcp/dhclient-exit-hooks.d/azure-cloud.sh
/etc/kernel/postinst.d/51-dracut-rescue-postinst.sh
[root@localhost ~]# find ./ ! -name 'file9'  -a -name 'file*'|xargs rm -f
[root@oldboy ~]# yum list | grep ro*  过滤 r ro roo rop 的行,打印出来,单独一个o的行不过滤出来,不以ro开头的也会过滤出来
find /etc/ ! -type d   查找/etc 目录下的文件(普通文件,链接文件,隐藏文件) (不等于-type f)  
//创建文件
touch /etc/sysconfig/network-scripts/{ifcfg-eth1,IFCFG-ETH1}
//查找/etc目录下包含ifcfg-eth0名称的文件
[root@zls ~]# find /etc -name "ifcfg-eth1" (必须加个单引号或者双引号)
//-i 忽略大小写
[root@zls ~]# find /etc -iname "ifcfg-eth1"   是
//查找/etc目录下包含ifcfg-eth名称所有文件
[root@zls ~]# find /etc/ -name "ifcfg-eth*"
[root@zls ~]# find /etc -iname "ifcfg-eth*"   开头
//查找包含eth的文件
[root@localhost opt]# find /opt/ -name '*eth*'   包含
[root@localhost opt]# find /opt/ -iname '*eth*'
find /root/dir1 ! (-name 'file5' -o -name 'file9' )

-size

-size n[cwbkMG]
              `b'    block

              `c'    bytes 字节

              `w'    words 单词

              `k'    kb(小)  (默认)

              `M'    MB(大M)

              `G'    GB (大)
              
              -o       或者
              -a       并且
              ! -size  排除     

-type

f:文件
d:目录
l:软连接
s:socket
p:管道文件
b:块设备
c:字符设备

find / -type f
find / -type d
find / -type b

find / -type f -ls

find / -type f|head -5|xargs ls -l
[root@oldboy opt]# find /opt/ -type d |xargs rm -rf  删除了本目录
[root@oldboy opt]# ll
total 0
[root@oldboy opt]# cd /opt
-bash: cd: /opt: No such file or directory

-mtime

-mtime
1.时间列出来,从昨天数,要第几天的就数到第几天(-atime 7)
2.今天a - 几天b以前    (-atime 7)(时间分割段)
那么可以知道,在linux中是文件是没有*创建时间*的,只是如果刚刚创建一个文件,毋庸置疑它的三个时间是都等于创建时间的,就像刚才创建的test文件,我们看到它的三个时间是相等的。那么在linux上这三个时间分别代表着什么,随着什么而改变:

>修改时间:文件的内容被最后一次*修改的时间*,我们经常用的 ls -l命令显示出来的文件时间就是这个时间,当用vim对文件进行编辑之后保存,它的mtime就会相应的改变;

>访问时间:对文件进行一次读操作,它的访问时间就会改变。例如像:cat、more等操作,但是像之前的state还有ls命令对atime是不会有影响的;

>状态时间:当文件的状态被改变的时候,状态时间就会随之改变,例如当使用chmod、chown等改变文件属性的操作是会改变文件的ctime的

[root@oldboy ~]# stat 7   可以查看一个文件或者目录的详细信息
  File: ‘7’
  Size: 6         	Blocks: 8          IO Block: 4096   regular file
Device: 803h/2051d	Inode: 33575759    Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2020-04-30 03:16:00.556560787 +0800
Modify: 2020-04-30 03:15:55.853560562 +0800
Change: 2020-04-30 03:15:55.853560562 +0800
 Birth: -

条件语句

-a:and 和,并且
-o:or 或者
!:取反 (同类型的用括号括起来)

find / -type f -name ' [1-2].txt ' -o   (-type d -name '*a*'  )
find /  ( -type f -name ' 1.txt ' -o -name  '2.txt'  ) -o   (-type d -name '*a*'  )   转义括号,注意空格
find / -type f -name 1.txt -o -name 2.txt -o ( -type d -name '*a*' )
find / ( -name '1.txt' -o -name '2.txt' -o -type d -name '*a*' )

!:取反  相同类型取反可以合并( -a -o)     find /etc  ! -name *.sh -o ! -name s*
find /etc ! ( -name *.sh -a -name s* )   find /etc  -name *.sh -o -name s*
find /etc ! ( -name *.sh -o -name s* )   find /etc  -name *.sh -a -name s*
                                        find /etc  ! -name *.sh -a ! -name s*
( -name 必须加个单引号或者双引号)(转义括号)

find-根据用户查找文件

忽略/proc 进程文件,进程号随时可能会变化
find: ‘/proc/23447/task/23447/fd/6’: No such file or directory(自己进程用过的文件。不用管)

-user:查找属主是X个用户
-group:查找属组是X个组
-nouser:查找没有用户的文件 删除用户
-nogroup:查找没有组 的文件 删除组

[root@localhost opt]# find ./ -user zls|xargs ls -l  查看文件
-rw-r--r--. 1 zls qiandao 0 Apr 16 00:36 ./file1
-rw-r--r--. 1 zls zls     0 Apr 16 00:36 ./file3
[root@oldboy opt]# find /etc/ -nogroup |xargs ls -ld  查看目录或文件
drwxr-xr-x 2 54644 54644 6 Apr 30 00:12 /etc/ooo

find-根据深度查找

-maxdepth 数字

[root@oldboy ~]# find /etc/ -maxdepth 1 -type f -name '*.conf*'
[root@oldboy ~]# find /etc/ -type f -name '*.conf*' -maxdepth 1  除了警告,结果一样

[root@oldboy opt]# find /opt/ -maxdepth 1 | xargs ls -l

find-根据文件权限查找

## 精确查找
[root@localhost opt]# find /opt/ -perm 644

## 包含指定权限的文件
root@localhost opt]# find /opt/ -perm -222   -2--2--2-

root@localhost opt]# find /opt/ -perm /222   有一个就会找出来

root@localhost opt]# find /opt/ -perm /4000  确定某一位

root@localhost opt]# find /opt/ -perm /200   确定某一位

[root@oldboy ~]# find / -perm /4 -ls  3位都得有4 才会被找出来= -444

-222:and
/222:or
 4   不存在
-4 = -444
/4 = -444

  [root@oldboy opt]# find /opt/ -perm /000 | wc -l   文件或目录总数量
find: warning: you have specified a mode pattern /000 (which is equivalent to /000). The meaning of -perm /000 has now been changed to be consistent with -perm -000; that is, while it used to match no files, it now matches all files.
56  (-1)(排除opt目录)

[root@oldboy opt]# find /opt/ -perm /400 -ls  必须有一个r
[root@oldboy opt]# find /opt/ -perm /200 -ls  必须有一个w
[root@oldboy opt]# find /opt/ -perm /100 -ls  必须有一个x

动作

-delete (只能删除空目录,也就是目录下没用目录或文件)  rm
[root@localhost opt]# find /opt/ -type d ! -name 'opt'|xargs rm -fr
[root@oldboy opt]# find /opt/ -type f -delete (全部)

[root@oldboy opt]# rm -rf 'date +%F-%R_444_888_file.txt'  删除名字中有空格的文件,必须要用引号,不然会以为删除两个文件
drwxr-xr-x 2 root root 6 Apr 30 00:35 date +%F-%R_444_888_file.txt
[root@oldboy opt]# ll -d /opt/      root无视属主权限位
drwxr-xr-x. 2 root root 33 Apr 30 03:14 /opt/
[root@oldboy opt]# find /opt/ -type d   查找目录会显示当前目录
/opt/
[root@oldboy opt]# find /opt/ -type d |xargs rm -rf 在目录下删除该目录
[root@oldboy opt]# ll  (opt也没了)
total 0

-ls 
[root@oldboy ~]# find /opt/ -perm 644 -ls  前面是inode ,多条件可能会显示一部分
16777289    0 -rw-r--r--   1 root     root            0 Apr  1 00:00 /opt/2020-04-01_file.txt
[root@oldboy ~]# find /opt/ -perm 644 |xargs ls
/opt/2020-04-01_file.txt  /opt/2020-04-09_file.txt  /opt/2020-04-17_file.txt  /opt/2020-04-25_file.txt
[root@oldboy ~]# find /opt/ -perm 644 |xargs ls -l
-rw-r--r-- 1 root root 0 Apr  1 00:00 /opt/2020-04-01_file.txt
[root@oldboy opt]# toucfind /opt/ -perm 644 |xargs ls -li  (显示所有文件的inode)
16777289 -rw-r--r-- 1 root root 0 Apr 30 03:14 /opt/2020-04-01_file.txt

[root@oldboy opt]# find . -mtime +10 |xargs chmod 755  批量授权文件
[root@oldboy opt]# find . -mtime -10 |xargs chmod 222

-ok
语法: -ok ;     多次询问是否复制,或者移动
-exec
语法: -exec ;

## 拷贝找到的文件到/tmp下  批量拷贝(cp -rpat )(cp 文件如下)
拷贝目录不能用第一种方法
[root@localhost opt]# find /opt/ -mtime +5 |xargs cp -t /tmp/

[root@localhost opt]# find /opt/ -mtime +5 |xargs -I {} cp {} /tmp/ (i不=I)
[root@localhost opt]# find /opt/ -mtime +5 |xargs -i  cp {} /opt/syy3.txt

[root@localhost opt]# find /opt/ -name '*.txt' -ok  cp {} /tmp/ ;
[root@localhost opt]# find /opt/ -mtime +5 -exec cp {} /tmp/ ;

[root@localhost opt]# find /opt/ -mtime +5 |xargs mv -t /tmp/

< cp ... /opt/2020-04-01_file.txt > ? y   (-ok 询问多次是否cp)
< cp ... /opt/2020-04-02_file.txt > ? y
< cp ... /opt/2020-04-03_file.txt > ? y
< cp ... /opt/2020-04-04_file.txt > ? y
< cp ... /opt/2020-04-05_file.txt > ? y
< cp ... /opt/zls.txt > ? y

## find 结合xargs  批量操作
#拷贝
find  / -type f |xargs cp -t /tmp
#查看详细信息
find  / -type f |xargs ls -l
#替换
find  / -type f |xargs sed -i 's###g'   
sed 's###g'     不是真正的替换(显示替换的结果)
sed -i 's###g'  这才是真正的替换(显示替换的结果)
xargs 把文件名转化成数据流,不转化的话只替换文件名,转化的话只替换文件内容

#移动
find  / -type f |xargs mv -t /tmp
#删除
find  / -type f |xargs rm -fr

相关命令

rename:
-n 先不改名,显示执行后的结果
-f 强制执行,名重复就覆盖
? 任意字符
* 表示一个或一串任意字符
【常用正则表达式符号说明】
^    匹配输入的开始位置
$    匹配输入的结尾
.    匹配除换行符外的任意字符
+    匹配前一个字符一次或多次 例如,"zo+"可以匹配"zoo",但不匹配"z"
[a-z]    表示某个范围内的字符,例如,"[a-z]"匹配"a"与"z"之间的任何一个小写字母字符。
[^m-z]    否定的字符区间。与不在指定区间内的字符匹配。

[root@localhost www]# rename 's/.sh/.php/' *   (分隔符 / #)(s)     
#将当前目录下.sh后缀的文件,变成.php

[root@localhost www]# rename 's/$/.bak/' /home/www/*.php     
#给www目录下的.php加上bak后缀

[root@localhost www]# rename 's/^/bak_/' *.bin     
#给当前目录下的.bin后缀文件加上bak_前缀

[root@localhost www]# rename 's/.bin$//' *       
#批量删除当前目录下所有文件的.bin 后缀

[root@localhost www]# rename 's/A-Z/a-z/' *      
#修改当前目录所有文件名为小写

tr 现在的  以后的  (不是真正的替换)(一对一替换)(echo xx|tr 1 2 )(只能替换眼前的)
sed 's#现在的#以后的#g' (先查找,再替换)
原文地址:https://www.cnblogs.com/syy1757528181/p/12813527.html