08文件查找与归档压缩
目标:
1.find命令文件查找
2.grep对文件内容查找
3.tar等命令对文件压缩解压缩
一、文件查找
功能项 | 命令实例 | 作用 |
---|---|---|
find | find 起始目录 查找类型 查找条件 | 查找起始目录及所有子目录下的文件及文件夹 |
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
二、内容查找
功能项 | 命令实例 | 作用 |
---|---|---|
grep | grep [选项] 匹配模式 目标文件 | 基于行对目标文件的内容进行查找 |
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的文件,与上一条命令功能类似 |
wc | wc -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 /gzip | tar -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 /unzip | zip 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 |
r|d ↩︎