Linux基础学习之Day8-文件查找

Linux基础学习之从入门到精通Day8

文件查找

grep: 文件内容过滤
find: 文件查找,针对文件名

一、命令文件

#which ls //从PATH环境变量 (echo $PATH)
#whereis vim

[root@localhost ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/usr/local/htop/bin/:/root/bin

二、任意文件

A. locate (查询的数据库: /var/lib/mlocate/mlocate.db)
计划任务:每天自动更新数据库 /etc/cron.daily/mlocate.cron
手动更新数据库:updatedb
# locate ifcfg-eth0
# locate ifcfg-enp0s25
locate

updatedb后才能查找到 非常麻烦 不建议使用
如果没有 locate 使用YUM直接安装是不行的。 要查一下在哪个包里 yum provides locate -→ mlocate → 直接YUM mlocate即可
B. find
find [options] [path…] [expression] [action]

=expression= 熟用*通配符
按文件名:

[root@~]# find /etc -name “ifcfg-eth0” name 名字 后面-print 已省略
[root@~]# find /etc -iname “ifcfg-eth0” //-i忽略大小写
[root@~]# find /etc -iname “ifcfg-eth*”

按文件大小:

[root@~]# find /etc -size +5M //大于5M
[root@~]# find /etc -size 5M
[root@~]# find /etc -size -5M
[root@~]# find /etc -size +5M -ls //-ls找到的处理动作 不是平时用的ls
ll - h 查看大小

指定查找的目录深度:

-maxdepth levels
-mindepth levels
[root@~]# find / -maxdepth 3 -a -name “ifcfg-eth0” maxdepth 3 最大3层 a要满足2个条件 并且

按时间找(atime,mtime,ctime):

[root@~]# find /etc -mtime +5 //修改时间超过5天
[root@~]# find /etc -mtime 5 //修改时间等于5天
[root@~]# find /etc -mtime -5 //修改时间5天以内

按文件类型:

[root@~]# find /dev -type f //f普通
[root@~]# find /dev -type d //d目录
[root@~]# find /dev -type l //l链接
[root@~]# find /dev -type b //b块设备
[root@~]# find /dev -type c //c字符设备
[root@~]# find /dev -type s //s套接字
[root@~]# find /dev -type p //p管道文件

按文件权限:

[root@~]# find . -perm 644 -ls .是当前目录 精确查找644 *一般都是精确

[root@~]# find . -perm -644 -ls -是包含到意思

带不带- 自己对比一下 查看。 带-表示只要6就可以
[root@~]# find . -perm -600 -ls
[root@~]# find . -perm -222 -ls //全局可写
[root@~]# find /usr/bin /usr/sbin -perm -4000 -ls //包含set uid
[root@~]# find /usr/bin /usr/sbin -perm -2000 -ls //包含set gid
[root@~]# find /usr/bin /usr/sbin -perm -1000 -ls //包含sticky

找到后处理的动作 ACTIONS: (默认动作-print)

-print
-ls
-delete
-exec 后面跟自定义的shell命令
-ok 后面跟自定义的shell命令
[root@~]# find /etc -name “ifcfg*” 后可以加|xargs -h
[root@~]# find /etc -name “ifcfg*” -print
[root@~]# find /etc -name “ifcfg*” -ls
[root@~]# find /etc -name “ifcfg*” -exec cp -rvf {} /tmp ;
[root@~]# find /etc -name “ifcfg*” -ok cp -rvf {} /tmp ;
exec命令用于调用并执行指令的命令

查找带root带文件 复制到tmp下

find /etc -name “root*” -exec cp -rf {} /tmp ; 复制到当前文件下 /tmp换成.
[root@~]# find /etc -name “ifcfg*” -exec rm -rf {} ; exec为执行一条shell命令 {}为前面的东西; 格式
[root@~]# find /etc -name “ifcfg*” -delete

扩展知识:find结合xargs

[root@~]# find . -name “yang*.txt” |xargs rm -rf !!!!!!!!!!!!!重点 找到之后删除处理xargs 参数传递处理找出后删除
[root@~]# find /etc -name “ifcfg-eth0” |xargs -I {} cp -rf {} /var/tmp

案例: 分别找出file5 和除了file5的文件

[root@~]# mkdir dir1
[root@~]# touch dir1/file{1…20}

[root@~]# find /root/dir1 -name “file5”
[root@~]# find /root/dir1 ! -name “file5” !为取反

[root@~]# find /root/dir1 -name “file5” -o -name “file9” 即是file5又是file9
/root/dir1/file5
/root/dir1/file9

扩展 不常用

[root@~]# find /root/dir1 -name “file5” -o -name “file9” -ls
1466515 0 -rw-r–r-- 1 root root 0 6月 5 11:15 /root/dir1/file9

[root@~]# find /root/dir1 -name “file5” -ls -o -name “file9” -ls
1466499 0 -rw-r–r-- 1 root root 0 6月 5 11:15 /root/dir1/file5
1466515 0 -rw-r–r-- 1 root root 0 6月 5 11:15 /root/dir1/file9

[root@~]# find /root/dir1 ( -name “file5” -o -name “file9” ) -ls \为转译 不加会报错
1466499 0 -rw-r–r-- 1 root root 0 6月 5 11:15 /root/dir1/file5
1466515 0 -rw-r–r-- 1 root root 0 6月 5 11:15 /root/dir1/file9

[root@localhost ~]# find /root/dir1 ( -name “file5” -o -name “file9” ) -exec rm -rvf {} ;
removed ‘/root/dir1/file5’
removed ‘/root/dir1/file9’

-exec和xargs的区别
f i n d . − n a m e ′ c o r e ′ − t y p e f − e x e c r m / ; 时, f i n d − e x e c 命令会对每个匹配的文件执行一个单独的 r m 操作( e x e c u t e a s e p a r a t e r m f o r e a c h o n e ) , 正如你手动敲入下面命令: r m . / b i n / c o r e r m . / s o u r c e / s h o p p i n g c a r t / c o r e r m . / b a c k u p s / c o r e . . . 但是使用这种方式,如果有 100 个文件匹配了,那么就需要启 100 个进程,一个进程处理一个 r m 命令。一般来说,其越多进程,意味着越耗性能。我们可以换个思路,我们将要删除文件当作参数传递给 r m 不就可以了吗?也就是说: r m . / b i n / c o r e r m . / s o u r c e / s h o p p i n g c a r t / c o r e r m . / b a c k u p s / c o r e . . . 改成: r m . / b i n / c o r e . / s o u r c e / s h o p p i n g c a r t / c o r e . / b a c k u p s / c o r e 但是前提是后面的命令必须支持多参数。相有些命令,比如 u n z i p ,就不支持输入多个 j a r 包,所以必须用 − e x e c 。 x a r g s ,顾名思义,是对参数进行处理的命令。它的任务就是将输入行转换成下一个命令的参数列表。因此上面的 f i n d − e x e c 命令可以改写成: f i n d . − n a m e ′ c o r e ′ − t y p e f − p r i n t ∣ x a r g s r m W i t h t h i s a p p r o a c h , x a r g s b u n d l e s t o g e t h e r a s m a n y f i l e n a m e a r g u m e n t s a s p o s s i b l e f o r s u b m i s s i o n t o e a c h i n v o c a t i o n o f r m t h a t ′ s n e e d e d , i n c o m p l i a n c e w i t h t h e O S ′ s m a x i m u m a l l o w e d s i z e f o r a n a r g u m e n t l i s t . T h i s m e a n s x a r g s i s g u a r a n t e e d n o t o n l y t o h a n d l e a l l t h e a r g u m e n t s , b u t a l s o t o u s e t h e s m a l l e s t p o s s i b l e n u m b e r o f p r o c e s s e s i n d o i n g s o . F o r e x a m p l e , i f e a c h c o m m a n d c a n h a n d l e 100 a r g u m e n t s , a n d t h e r e a r e 110 f i l e n a m e s t o p r o c e s s , t h e r e w i l l b e t w o i n v o c a t i o n s o f t h e c o m m a n d , r e s p e c t i v e l y h a n d l i n g 100 a n d 10 a r g u m e n t s . 其中操作系统允许的最大参数长度由如下命令得到: f o r r e s t @ u b u n t u :   find . -name 'core' -type f -exec rm {} /; 时,find -exec 命令会对每个匹配的文件执行一个单独的rm操作(execute a separate rm for each one), 正如你手动敲入下面命令: rm ./bin/core rm ./source/shopping_cart/core rm ./backups/core ...但是使用这种方式,如果有100个文件匹配了,那么就需要启100个进程,一个进程处理一个rm命令。一般来说,其越多进程,意味着越耗性能。我们可以换个思路,我们将要删除文件当作参数传递给rm不就可以了吗?也就是说: rm ./bin/core rm ./source/shopping_cart/core rm ./backups/core ...改成: rm ./bin/core ./source/shopping_cart/core ./backups/core但是前提是后面的命令必须支持多参数。相有些命令,比如unzip,就不支持输入多个jar包,所以必须用-exec。 xargs,顾名思义,是对参数进行处理的命令。它的任务就是将输入行转换成下一个命令的参数列表。因此上面的find -exec命令可以改写成: find . -name 'core' -type f -print | xargs rmWith this approach, xargs bundles together as many filename arguments as possible for submission to each invocation of rm that's needed, in compliance with the OS's maximum allowed size for an argument list. This means xargs is guaranteed not only to handle all the arguments, but also to use the smallest possible number of processes in doing so. For example, if each command can handle 100 arguments, and there are 110 filenames to process, there will be two invocations of the command, respectively handling 100 and 10 arguments. 其中操作系统允许的最大参数长度由如下命令得到: forrest@ubuntu:~ find.namecoretypefexecrm/;时,findexec命令会对每个匹配的文件执行一个单独的rm操作(executeaseparatermforeachone,正如你手动敲入下面命令:rm./bin/corerm./source/shoppingcart/corerm./backups/core...但是使用这种方式,如果有100个文件匹配了,那么就需要启100个进程,一个进程处理一个rm命令。一般来说,其越多进程,意味着越耗性能。我们可以换个思路,我们将要删除文件当作参数传递给rm不就可以了吗?也就是说:rm./bin/corerm./source/shoppingcart/corerm./backups/core...改成:rm./bin/core./source/shoppingcart/core./backups/core但是前提是后面的命令必须支持多参数。相有些命令,比如unzip,就不支持输入多个jar包,所以必须用execxargs,顾名思义,是对参数进行处理的命令。它的任务就是将输入行转换成下一个命令的参数列表。因此上面的findexec命令可以改写成:find.namecoretypefprintxargsrmWiththisapproach,xargsbundlestogetherasmanyfilenameargumentsaspossibleforsubmissiontoeachinvocationofrmthatsneeded,incompliancewiththeOSsmaximumallowedsizeforanargumentlist.Thismeansxargsisguaranteednotonlytohandleallthearguments,butalsotousethesmallestpossiblenumberofprocessesindoingso.Forexample,ifeachcommandcanhandle100arguments,andthereare110filenamestoprocess,therewillbetwoinvocationsofthecommand,respectivelyhandling100and10arguments.其中操作系统允许的最大参数长度由如下命令得到:forrest@ubuntu:  getconf ARG_MAX
2097152这意味着xargs保证不会因为参数过多而挂掉。所以目前看来唯一需要保证的就是后面的命令支持多参数。比如前面说过的unzip,就不支持多参数,如果你使用xargs xxx.jar

相比之下,也不难看出各自的缺点
1、exec 每处理一个文件或者目录,它都需要启动一次命令,效率不好;
2、exec 格式麻烦,必须用 {} 做文件的代位符,必须用 ; 作为命令的结束符,书写不便。
3、xargs 不能操作文件名有空格的文件;

综上,如果要使用的命令支持一次处理多个文件,并且也知道这些文件里没有带空格的文件,
那么使用 xargs比较方便; 否则,就要用 exec了。

文件打包及压缩

建议针对目录

Demo: 复制未打包的文件到远程主机
[root@yangs ~]# du -sh /etc
39M /etc
[root@yangs ~]# time scp -r /etc root@192.168.5.32:/tmp //将/etc目录…

=打包,压缩=

[root@~]# tar -czf etc1.tar.gz /etc //-z 调用gzip : tar removeing 非报错
[root@~]# tar -cjf etc2.tar.bz2 /etc //-j 调用bzip2
[root@~]# tar -cJf etc3.tar.xz /etc //-J 调用xz
[root@~]# ll -h etc*
-rw-r–r–. 1 root root 8.7M 3月 12 00:08 etc1.tar.gz
-rw-r–r–. 1 root root 7.5M 3月 12 00:08 etc2.tar.bz2
-rw-r–r–. 1 root root 4.8M 3月 12 00:09 etc3.tar.xz

=解压,解包=

[root@~]# tar -tf sys.tar.xz
[root@~]# tar -xzvf etc1.tar.gz
[root@ ~]# tar -xvf etc1.tar.gz //无需指定解压工具,tar会自动判断
[root@~]# tar -xvf etc2.tar.bz2 -C /tmp //-C重定向到//tmp目录
[root@~]# tar xf etc3.tar.xz !!!!!!!!!!!!!!!自动寻找 记住这个就好了

file 可查看

==解压zip

[root@~]# unzip xxx.zip

压缩打包

gzip bzip2
[root@localhost tmp]# dd if=/dev/zero of=/tmp/data bs=100M count=2
in file out file block size count
[root@localhost tmp]# du -sh data -s 总共 -h 最大单位
[root@localhost tmp]# gzip data
[root@localhost tmp]# gunzip data.gz
[root@localhost tmp]# bzip2 data
[root@localhost tmp]# bunzip2 data.bz2

tar
[root@localhost tmp]# tar -cvf boot.tar /boot/ -c 创建 -v 显示过程 -f 文件
[root@localhost tmp]# tar -tvf boot.tar -t 查看
[root@localhost tmp]# tar -rvf boot.tar /root/install.log -r 追加打包
[root@localhost tmp]# tar -xvf boot.tar

[root@localhost tmp]# tar -cvf /home/boot.tar /boot/ 指定包存放路径
[root@localhost tmp]# tar -xvf /tmp/boot.tar -C /home/ 指定解压目录

调用gzip压缩

[root@localhost tmp]# tar -zcvf boot.tar.gz /boot/
[root@localhost tmp]# tar -ztvf boot.tar.gz
[root@localhost tmp]# tar -zxvf boot.tar.gz

调用bzip2压缩

[root@localhost tmp]# tar -jcvf boot.tar.bz2 /boot/
[root@localhost tmp]# tar -jtvf boot.tar.bz2
[root@localhost tmp]# tar -jxvf boot.tar.bz2

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值