0809文件查找grep find与归档压缩

08文件查找与归档压缩

目标:

1.find命令文件查找

2.grep对文件内容查找

3.tar等命令对文件压缩解压缩

一、文件查找

功能项命令实例作用
findfind 起始目录 查找类型 查找条件查找起始目录及所有子目录下的文件及文件夹
find . -name “hello.txt”查找当前目录下文件名为hello.txt的文件或文件夹
find . -name “hello”查找当前目录下文件名包含hello的文件或文件夹
find /home -name “bash”查找目录/home下文件名包含bash的文件或文件夹
find . -name “*”查找当前目录下多有的文件和文件夹(等同于ls -R)
find . -name “[h]*”查找当前目录下以h开头的文件或文件夹
find . -name “[h|f]*”查找当前目录下所有以h或者f开头的文件或文件夹
find . -name “[a-z]*”查找当前目录下所有以小写字母开头的文件或文件夹
find . -name “[A-Z]*”查找当前目录下所有以大写字母开头的文件或文件夹
find . -name “[a-Z]*”查找当前目录下所有以字母开头的文件或文件夹
find . -name “[h-w]*”查找当前目录下所有以字母h-w开头的文件或文件夹
find . -name “[0-9]*”查找当前目录下所有以数字开头的文件或文件夹
find . -name “[5-8]*”查找当前目录下所有以数字5-8开头的文件或文件夹
find . -name “h?ello*”查找当前目录下所有以h后面带一个字符加llo开头的文件或文件夹
find . -name “a-h*”查找当前目录下所有不以a-h开头的文件或文件夹
find . -name ‘\’查找当前目录下所有包含特殊字符\的文件(注意单引号)
find . -perm 777查找当前目录下权限为777的文件或文件夹
find . -path “./test” -prune -o -name “hello”查找当前目录下除test目录的其他所有目录中包含hello的文件或文件夹
find . -user mary查找当前目录下文件所有者为mary的文件或者文件夹
find . -group dev查找当前目录下文件或文件夹所在组为dev的内容
find . -mtime -3查找当前目录下在3天内更新过的文件或文件夹
find . -mtime+3查找当前目录下在3天前更新过的文件或文件夹
find . -newer hello.txt查找当前目录下比hello.txt新的文件或文件夹
find . ! -newer hello.txt查找当前目录下比hello.txt旧的文件或文件夹
find . -type d查找当前目录下所有的文件夹(普通的文件的类型为f),了解Linux文件类型:1)f:普通文件,如文本文件、可执行文件 2)d:目录 3)l:软连接文件 4)c:字符设备,如终端,磁带机等 5)b:块设备,如光盘,硬盘等
find . -type |查找当前目录下所有的软连接文件
find . -size 602c查找当前目录下文件大侠为602字节的文件,可用单位为:c----byte k----Kilobytes M----Megabytes G----Gigabytes
find . -size +602c查找当前目录下文件大小大于602字节的文件(用-表明小于)
find . -name “hello*” -exec ls -l {} ;查找当前目录下所有以hello开头的文件并将其细节显示出来,如果查找出了目录,那么注意目录会被ls -l列出来
find . -name “hello*” -exec rm {} ;查找当前目录下所有以hello开头的文件并将其删除
find . -name “hello*” |xrags ls -l查找当前目录下所有以hello开头的文件并将其细节显示出来
linux命令中的 |管道前面的输出就是后面的输入
[root@mycentos var]# echo -e "Hello\nWoniu"
Hello
Woniu
[root@mycentos var]# echo "Hello\nWoniu"
Hello\nWoniu
[root@mycentos etc]# find . -perm 777
链接文件相当于快捷方式
[root@mycentos etc]# ll ./pam.d/system-auth
lrwxrwxrwx. 1 root root 14 10月 12 01:45 ./pam.d/system-auth -> system-auth-ac
一天内更新过的
[root@mycentos etc]# find /var/log -mtime -1
/var/log/tallylog
/var/log/lastlog
/var/log/wtmp
/var/log/audit/audit.log
/var/log/messages
/var/log/secure
/var/log/cron

[root@mycentos ~]# uname -a
Linux mycentos 3.10.0-1160.el7.x86_64 #1 SMP Mon Oct 19 16:18:59 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
[root@mycentos ~]# ll /etc/centos-release
-rw-r--r--. 1 root root 37 10月 23 2020 /etc/centos-release
[root@mycentos ~]# head -n 13 /etc/passwd | tail -n 7
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin

二、内容查找

功能项命令实例作用
grepgrep [选项] 匹配模式 目标文件基于行对目标文件的内容进行查找
grep “root” /etc/passwd查到/etc/passwd文件中包含root的行
grep -n “root” /etc/passwd查找到/etc/passwd文件中包含root的行并输出行号
grep “^ma” /etc/passwd查找以ma为行首的行
grep “bash$” /etc/passwd查找以bash为行尾的行
grep “1” /etc/passwd查找以r或d为行首的行
grep -v “内容”排除包含"内容"的行
ls | grep test从ls的输出中过滤出包含test的文件名
grep -r games /etc在/etc目录下查找所有包含games的文件
find与grep结合find ./ -name “*” | xargs grep word遍历某个目录下所有文件包含word的文件,与上一条命令功能类似
wcwc -l统计文件行数或输出的个数,-c或–bytes或–chars只显示Bytes数。-l或–lines显示行数,-w或–words只显示字数,如果不指定参数,则统计所有信息
[root@mycentos ~]# echo 'nihao aattt' > rootfile.txt 
[root@mycentos ~]# find ./ -name "r*" 
./rootfile.txt
[root@mycentos ~]# find ./ -name "r*" | xargs grep nihao
nihao aattt
[root@mycentos ~]# find ./ -name "r*" | grep txt
./rootfile.txt

三、文件归档(archive)压缩

功能项命令实例作用
tar /gziptar -cvf hello.tar hello.txt将hello.txt归档合并成hello.tar
c代表归档 f指定文件 v显示归档信息
tar -cvf test.tar /opt/test将目录/opt/test归档命名成test.tar
tar -tf test.tar将归档文件test.tar中的文件显示出来
tar -xvf test.tar提取归档文件中的内容
gzip hello.tar将归档文件hello.tar 压缩成hello.tar.gz
gzip -d hello.tar.gz解压缩文件成hello.tar
tar -zcvf hello.tar.gz hello.txt将hello.txt归档并压缩成hello.tar.gz
tar -zxvf hello.tar.gz解压缩
zip /unzipzip hello.zip hello.txt将hello.txt压缩并命名承诺hello.zip
zip -r test.zip /opt/tes将/opt/test目录压缩
unzip -v hello.zip查看压缩文件hello.zip中的文件信息
unzip hello.zip解压缩hell.zip

  1. r|d ↩︎

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值