1)find 按照时间查找
-mtime
-ctime
-atime
语法格式: 经常用来处理日志(日志不重要 或者只保留7天的日志)
find /oldboy -type f -mtime +n(n是数字 代表天的意思)
+7 7天前被修改过的文件
-7 7天内被修改过的文件
-mtime 1 文件的修改时间为距离当前时间1天 24-48小时的文件
-mtime 0 文件的修改时间为距离当前时间不到1天 24小时之内的文件
-mtime +1 文件的修改时间为距离当前时间2天 48小时以前的文件
-mtime -1 文件的修改时间为距离当前时间小于1天的文件 24小时内的文件
[root@oldboyedu-lnb ~]# touch 1day.txt
[root@oldboyedu-lnb ~]# ll
total 0
-rw-r--r-- 1 root root 0 Aug 5 09:16 1day.txt
-rw-r--r-- 1 root root 0 Jul 25 00:00 oldboy.txt
drwxr-xr-x 2 root root 36 Aug 4 11:50 test
-rw-r--r-- 1 root root 0 Jul 25 00:00 test.txt
[root@oldboyedu-lnb ~]# find ./ -type f -mtime +7
./test.txt
./oldboy.txt
[root@oldboyedu-lnb ~]# find ./ -type f -mtime -7
./1day.txt
[root@oldboyedu-lnb ~]# find ./ -type f -mtime 0
./1day.txt
[root@oldboyedu-lnb ~]# find ./ -type f -mtime -1
./1day.txt
笔试题: 查找/var/log/data/下修改时间为7天前文件并删除
find ./ -type f -mtime +7|xargs rm
find ./ -type f -mtime +7|xargs -i cp {} /tmp
!在find中取反
[root@oldboyedu-lnb test]# find ./ ! -type f # 查找文件 取反后查找 除文件外所有类型
find查找inode删除
[root@oldboyedu-lnb ~]# rm -f `find ./ -inum 33575024`
通配符
shell的内置功能
查找文件名 而不是查看过滤文件内容
Linux的命令都支持通配符
! find和awk 取反 表示调用历史命令
$()和`` 相同
环境准备:
mkdir /oldboy/20200118
cd /oldboy/20200118
touch stu{00…10}.txt oldboy{00…10}.log
ll
touch oldboy.txt stu.txt
表示任何字符串/文本(0或多个)
1.找出以stu开头的文件
[root@oldboyedu-lnb 20200118]# ll stu
-rw-r–r-- 1 root root 0 Aug 5 10:04 stu00.txt
2.找出以stu开头并且以.txt结尾的文件
[root@oldboyedu-lnb 20200118]# touch stu{1…5}.log
[root@oldboyedu-lnb 20200118]# ll stu*.txt
-rw-r–r-- 1 root root 0 Aug 5 10:04 stu00.txt
3.查找所有以.txt 结尾的文件
[root@oldboyedu-lnb 20200118]# ll *.txt
-rw-r–r-- 1 root root 0 Aug 5 10:04 oldboy.txt
-rw-r–r-- 1 root root 0 Aug 5 10:04 stu00.txt
[root@oldboyedu-lnb 20200118]# ll *txt
-rw-r–r-- 1 root root 0 Aug 5 10:04 oldboy.txt
4.使用find查找.txt结尾的文件
[root@oldboyedu-lnb 20200118]# find ./ -type f -name “*.txt”
./stu00.txt
./stu01.txt
[root@oldboyedu-lnb 20200118]# find -name “*.txt”
./stu00.txt
./stu01.txt
5.使用find查找以stu开头的文件、查找以stu开头并且以.log 结尾的文件
[root@oldboyedu-lnb 20200118]# find -name “stu*”
./stu00.txt
./stu01.txt
[root@oldboyedu-lnb 20200118]# find -name “stu*.log”
./stu1.log
./stu2.log
6.找出当前目录下面 文件名中包含(只要有就行)oldboy的文件 —模糊
[root@oldboyedu-lnb 20200118]# ll oldboy
-rw-r–r-- 1 root root 0 Aug 5 10:16 awk-oldboy
-rw-r–r-- 1 root root 0 Aug 5 10:16 lidao-oldboy.txt
-rw-r–r-- 1 root root 0 Aug 5 10:04 oldboy00.log
[root@oldboyedu-lnb 20200118]# find -name “oldboy”
./oldboy10.log
./oldboy.txt
./awk-oldboy
./lidao-oldboy.txt
?任何一个字符/文本/字母
1.匹配st后任意单个字符并以.txt结尾的文件
[root@oldboyedu-lnb 20200118]# ll st?.txt
-rw-r–r-- 1 root root 0 Aug 5 10:04 stu.txt
[root@oldboyedu-lnb 20200118]# ll st??.log
-rw-r–r-- 1 root root 0 Aug 5 10:07 stu1.log
-rw-r–r-- 1 root root 0 Aug 5 10:07 stu2.log
[root@oldboyedu-lnb 20200118]# ll /bin/???
wc 命令 # 重要 常用
[root@oldboyedu-lnb 20200118]# cat 1.txt
12345678
123
12345
[root@oldboyedu-lnb 20200118]# cat 1.txt|wc -L
8
[root@oldboyedu-lnb 20200118]# cat 1.txt|wc -l
3
3.[abcd] 表示一个整体一个筐,这个筐里面有4个情况 表示
查找文件名称包含a的 或者包含b的 或者包含c的…
查找当前目录下所有包含1234的文件
[root@oldboyedu-lnb 20200118]# ll stu[1234].log
-rw-r–r-- 1 root root 0 Aug 5 10:07 stu1.log
-rw-r–r-- 1 root root 0 Aug 5 10:07 stu2.log
-rw-r–r-- 1 root root 0 Aug 5 10:07 stu3.log
-rw-r–r-- 1 root root 0 Aug 5 10:07 stu4.log
[root@oldboyedu-lnb 20200118]# ll stu[1-4].log
-rw-r–r-- 1 root root 0 Aug 5 10:07 stu1.log
-rw-r–r-- 1 root root 0 Aug 5 10:07 stu2.log
[root@oldboyedu-lnb 20200118]# ll stu?[0123456789].txt
-rw-r–r-- 1 root root 0 Aug 5 10:04 stu00.txt
-rw-r–r-- 1 root root 0 Aug 5 10:04 stu01.txt
[root@oldboyedu-lnb 20200118]# ll stu[01][0123456789].txt
-rw-r–r-- 1 root root 0 Aug 5 10:04 stu00.txt
-rw-r–r-- 1 root root 0 Aug 5 10:04 stu01.txt
[root@oldboyedu-lnb 20200118]# ls stu0[0-3]
ls: cannot access stu0[0-3]: No such file or directory
[root@oldboyedu-lnb 20200118]# ls stu0[0-3]*
stu00.txt stu01.txt stu02.txt stu03.txt
[root@oldboyedu-lnb 20200118]# ll stu0[0-3]*
-rw-r–r-- 1 root root 0 Aug 5 10:04 stu00.txt
-rw-r–r-- 1 root root 0 Aug 5 10:04 stu01.txt
-rw-r–r-- 1 root root 0 Aug 5 10:04 stu02.txt
-rw-r–r-- 1 root root 0 Aug 5 10:04 stu03.txt
4.找出文件以s或o开头。
[root@oldboyedu-lnb 20200118]# ll s*
-rw-r–r-- 1 root root 0 Aug 5 10:04 stu00.txt
[root@oldboyedu-lnb 20200118]# ll o*
-rw-r–r-- 1 root root 0 Aug 5 10:04 oldboy00.log
5.{} 生成序列(一连串的文本)
复制所有的.tar.gz 和.txt 结尾的文件到家目录下
[root@oldboyedu-lnb 20200118]# echo {.tar.gz,.txt}
all.tar.gz 1.txt lidao-oldboy.txt oldboy.txt stu00.txt stu01.txt stu02.txt stu03.txt stu04.txt stu05.txt stu06.txt stu07.txt stu08.txt stu09.txt stu10.txt stu.txt
[root@oldboyedu-lnb 20200118]# ll ~
total 0
drwxr-xr-x 2 root root 6 Aug 5 10:48 alex
drwxr-xr-x 2 root root 54 Aug 5 10:49 test
[root@oldboyedu-lnb 20200118]# cp {.tar.gz,.txt} ~
[root@oldboyedu-lnb 20200118]# #输出stu01…10 oldboy01…10 alex01…10
[root@oldboyedu-lnb 20200118]# echo {stu,oldboy,alex}{01…10}
stu01 stu02 stu03 stu04 stu05 stu06 stu07 stu08 stu09 stu10 oldboy01 oldboy02 oldboy03 oldboy04 oldboy05 oldboy06 oldboy07 oldboy08 oldboy09 oldboy10 alex01 alex02 alex03 alex04 alex05 alex06 alex07 alex08 alex09 alex10
6.[!abcd] ! ^表示非
[root@oldboyedu-lnb 20200118]# ll stu0[!0-8].txt
-rw-r–r-- 1 root root 0 Aug 5 10:04 stu09.txt
[root@oldboyedu-lnb 20200118]# ll stu[1-5].txt
ls: cannot access stu[1-5].txt: No such file or directory
[root@oldboyedu-lnb 20200118]# ll stu[1-5].log
-rw-r–r-- 1 root root 0 Aug 5 10:07 stu1.log
-rw-r–r-- 1 root root 0 Aug 5 10:07 stu2.log
-rw-r–r-- 1 root root 0 Aug 5 10:07 stu3.log
-rw-r–r-- 1 root root 0 Aug 5 10:07 stu4.log
-rw-r–r-- 1 root root 0 Aug 5 10:07 stu5.log
[root@oldboyedu-lnb 20200118]# ll stu[!1-5].log
ls: cannot access stu[!1-5].log: No such file or directory
[root@oldboyedu-lnb 20200118]# ll stu[!1-4].log
-rw-r–r-- 1 root root 0 Aug 5 10:07 stu5.log
对中扩号内的包含s或t或u的进行取反
[root@oldboyedu-lnb 20200118]# ll [!stu].txt
-rw-r–r-- 1 root root 19 Aug 5 10:27 1.txt
-rw-r–r-- 1 root root 0 Aug 5 10:16 lidao-oldboy.txt
-rw-r–r-- 1 root root 0 Aug 5 10:16 oldboy.txt
[root@oldboyedu-lnb 20200118]# touch s.txt tttt.txt
[root@oldboyedu-lnb 20200118]# ll [!stu].txt
-rw-r–r-- 1 root root 19 Aug 5 10:27 1.txt
-rw-r–r-- 1 root root 0 Aug 5 10:16 lidao-oldboy.txt
-rw-r–r-- 1 root root 0 Aug 5 10:16 oldboy.txt
[root@oldboyedu-lnb 20200118]# ll stu0[^0-8].txt
-rw-r–r-- 1 root root 0 Aug 5 10:04 stu09.txt
[^^] 第一个代表取反 第二个普通字符
[root@oldboyedu-lnb 20200118]# ll stu0[^0-8].txt
-rw-r–r-- 1 root root 0 Aug 5 10:04 stu09.txt
-rw-r–r-- 1 root root 0 Aug 5 11:05 stu0^.txt
[root@oldboyedu-lnb 20200118]# ll stu0[^^0-8].txt
-rw-r–r-- 1 root root 0 Aug 5 10:04 stu09.txt
7.[]和{}的区别
常用的:
()小括号 正则中表示一个整体
[]中括号
{}大括号(花括号)
使用{}序列的方式查看文件名 {} 是…表示区间序列 [] 是 - 表示区间序列
[root@oldboyedu-lnb 20200118]# ll stu0{0…9}*
-rw-r–r-- 1 root root 0 Aug 5 10:04 stu00.txt
-rw-r–r-- 1 root root 0 Aug 5 10:04 stu01.txt
-rw-r–r-- 1 root root 0 Aug 5 10:04 stu02.txt
[root@oldboyedu-lnb 20200118]# ll stu0[0-9]*
-rw-r–r-- 1 root root 0 Aug 5 10:04 stu00.txt
-rw-r–r-- 1 root root 0 Aug 5 10:04 stu01.txt
. 当前的目录
… 上一级目录
.file 隐藏文件 点和文件中间没有空格
=== 1> 接收正确的输出结果 到空 到文本中
===2 >> 追加
2> 错误的输出结果 先清空
2>> 错误的输出结果 追加
< tr
<< cat
把结果定向到空
>1.txt 2>&1
>1.txt 2>1.txt
&>1.txt
[root@oldboyedu-lnb 20200118]# cat >>oldboy.txt< oldboy $ 调用变量 大部分命令都可以直接调用变量 而不需要先echo [root@oldboyedu-lnb 20200118]# echo test ‘’ 所见即所得 不会解析变量的内容 [root@oldboyedu-lnb 20200118]# time= [root@oldboyedu-lnb 20200118]# echo ‘$LANG $(which mkdir) {a…z}’ [root@oldboyedu-lnb 20200118]# ll “stu*.txt” PS:通配符不支持双引号 在find中支持 通配符 所有的linux命令都支持通配符
oldgirl
EOF
[root@oldboyedu-lnb 20200118]# cat oldboy.txt
oldboy
oldgirl
[root@oldboyedu-lnb 20200118]# echo $PS1
[\u@\h \W]$
[root@oldboyedu-lnb 20200118]# echo $LANG
en_US.UTF-8
[root@oldboyedu-lnb 20200118]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
test
[root@oldboyedu-lnb 20200118]# dir=test
[root@oldboyedu-lnb 20200118]# touch $dir
[root@oldboyedu-lnb 20200118]# ll test
-rw-r–r-- 1 root root 0 Aug 5 11:37 test
“” 和不加引号 都会解析变量的内容
`` 引用命令的结果 $() 里面必须是可执行命令
; 分隔多个命令,没有逻辑联系 只是一步一步执行 find中的分号是结尾的意思 ;
[root@oldboyedu-lnb 20200118]# ll which cat
-rwxr-xr-x. 1 root root 54080 Apr 11 2018 /usr/sbin/cat
[root@oldboyedu-lnb 20200118]# ll $(which cat)
-rwxr-xr-x. 1 root root 54080 Apr 11 2018 /usr/sbin/catdate +%F
[root@oldboyedu-lnb 20200118]#
[root@oldboyedu-lnb 20200118]# echo KaTeX parse error: Expected 'EOF', got '#' at position 48: …u-lnb 20200118]#̲ [root@oldboy…time-alex"
[root@oldboyedu-lnb 20200118]# echo KaTeX parse error: Expected 'EOF', got '#' at position 53: …u-lnb 20200118]#̲ test='time-alex’
[root@oldboyedu-lnb 20200118]# echo $test
$time-alex
$LANG KaTeX parse error: Expected 'EOF', got '#' at position 53: …u-lnb 20200118]#̲ echo "LANG $(which mkdir) {a…z}"
en_US.UTF-8 /usr/bin/mkdir {a…z}
[root@oldboyedu-lnb 20200118]# echo $LANG $(which mkdir) {a…z}
en_US.UTF-8 /usr/bin/mkdir a b c d e f g h i j k l m n o p q r s t u v w x y z
-rw-r–r-- 1 root root 0 Aug 5 11:54 stu*.txt
shell内置的功能
作用: 查找文件名或目录名称
*
?
[]
{}
[^]
[!]
单引号 双引号 反引号 不加引号
find -type
-size
-mtime
-name
-iname
-inum
-maxdepth
…
正则表达式: 用来过滤文件内容的