文件查找及打包

一 文件查找

grep: 文件内容过滤
[root@localhost ~]# grep 'root' /etc/passwd		//从/etc/passwd文件中过滤root字段
find: 文件查找,针对文件名

1.1 命令文件 # which ls //从PATH环境变量 (echo $PATH)

[root@localhost ~]# whereis vim 	//只能查找系统本身的二进制程序文件

[root@localhost ~]# echo $PATH 	//显示环境变量	/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

1.2、任意文件

A. locate

(查询的数据库: /var/lib/mlocate/mlocate.db)

手动更新数据库:updatedb

updatedb后才能查找到 非常麻烦 不建议使用 如果没有 locate 使用YUM直接安装是不行的。

要查一下在哪个包里

[root@localhost ~]# yum provides locate 			//查找locate安装包
[root@localhost ~]# yum -y install mlocate
[root@localhost ~]# updatedb							//更新数据库
[root@localhost ~]# locate	a.txt				//查找a.txt(只要文件有a.txt就会被查找到)

二 find详解

find 目录 条件选项 表达式

=expression= 熟用*通配符

2.1按文件名:
[root@localhost ~]# find /etc -name "ifcfg-ens33"      	//以名字的方式查找 
[root@localhost ~]# find /etc -iname "ifcfg-ens33"		    //-i忽略大小写
[root@localhost ~]# find /etc -iname "ifcfg-ens*"
2.2按文件大小:
[root@localhost ~]# find /etc -size +5M					    //大于5M
[root@localhost ~]# find /etc -size 5M						//等于5M
[root@localhost ~]# find /etc -size -5M
[root@localhost ~]# find /etc -size +5M -ls				   //-ls找到的处理动作,不是平时用的ls
2.3指定查找的目录深度:
-maxdepth levels
-mindepth levels
[root@youngfit ~]# find / -maxdepth 3 -a  -name "ifcfg-ens33"           //maxdepth 3   最大3层    a要满足2个条件 并且
按时间找(atime,mtime,ctime)[root@localhost ~]# find /etc -mtime +5					    //修改时间超过5天
[root@localhost ~]# find /etc -mtime 5						//修改时间等于5天
[root@localhost ~]# find /etc -mtime -5					    //修改时间5天以内
2.4按文件类型:
[root@localhost ~]# find /dev -type f						   //f普通
[root@localhost ~]# find /dev -type d						   //d目录
[root@localhost ~]# find /dev -type l						   //l链接
[root@localhost ~]# find /dev -type b						   //b块设备
[root@localhost ~]# find /dev -type c						   //c字符设备
[root@localhost ~]# find /dev -type s						   //s套接字 
2.5按文件权限:
[root@localhost ~]# find . -perm 644 -ls            .是当前目录    精确查找644  

[root@youngfit ~]# find . -perm -644 -ls     -是包含到意思 
带不带- 自己对比一下   查看。       带-表示只要有6就可以

[root@localhost ~]# find . -perm -600 -ls
[root@localhost ~]# find . -perm -222 -ls                  //可写
[root@localhost ~]# find /usr/bin  -perm -4000 -ls		  //包含set uid
[root@localhost ~]# find /usr/bin  -perm -2000 -ls		  //包含set gid
[root@localhost ~]# find /usr/bin  -perm -1000 -ls		  //包含sticky	
找到后处理的动作 ACTIONS:

-name "ifcfg*" | xargs

-name "ifcfg*" -print

-name "ifcfg*" -ls
[root@localhost ~]# find /etc -name "ifcfg*" -exec cp -rvf {} /tmp \; 

exec命令用于调用并执行指令的命令  查找带root带文件   复制到tmp下 

[root@localhost ~]# find /etc -name “root*”  -exec cp -rf {} /tmp \;              
 复制到当前文件下        /tmp换成. 

[root@localhost ~]# find /etc -name "ifcfg*" -exec rm -rf {} \;    exec为执行一条shell命令      {}为前面的东西   \; 格式

[root@localhost ~]# find /etc -name "ifcfg*" -delete  扩展知识:find结合xargs*

[root@localhost ~]# find . -name "yang*.txt" |xargs rm -rf    
!!!!!!!!!!!!!重点 找到之后删除处理xargs 参数传递处理找出后删除

[root@youngfit ~]# find /etc -name "ifcfg-eth0" |xargs -I {} cp -rf {} /var/tmp

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

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

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

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

三-exec和xargs的区别

#find . -name ‘core’ -type f -exec rm {} ; 时,find -exec 命令会对每个匹配的文件执行一个单独的rm操作(execute a separate rm for each one), 正如你手动敲入下面命令:

[root@localhost ~]# rm ./bin/core
[root@localhost ~]# rm ./source/shopping_cart/core
[root@localhost ~]# rm ./backups/core

但是使用这种方式,如果有100个文件匹配了,那么就需要启100个进程,一个进程处理一个rm命令。一般来说,其越多进程,意味着越耗性能。我们可以换个思路,我们将要删除文件当作参数传递给rm不就可以了吗?也就是说

[root@localhost ~]# rm ./bin/core
[root@localhost ~]# rm ./source/shopping_cart/core 
[root@localhost ~]# 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

相比之下,也不难看出各自的缺点

1、exec 每处理一个文件或者目录,它都需要启动一次命令,效率不好; 
2、exec 格式麻烦,必须用 {} 做文件的代位符,必须用 \; 作为命令的结束符,书写不便。 
3、综上,如果要使用的命令支持一次处理多个文件, 那么使用 xargs比较方便; 
4、xargs不能操作文件名有空格的文件;

四 打包压缩:

window打包压缩工具:

    .rar     .zip
    winrar zip 7zip 好压

linux打包压缩工具:

.tar.gz      .tar.bz2     .zip       
gzip  bzip2(只压缩) 和 tar(打包)

tar打包,解包:
#tar cvf file.tar 被打包的文件 …
#tar xvf file.tar -C /home //-C 指定解压路径

[root@localhost ~]# tar -cvzf dir2.tar dir2			//压缩目录dir2,将压缩包命名为dir2.tar
[root@localhost ~]# tar -xvf dir2.tar  -C /home	//解压缩,到/home下面

打包压缩一起:

[root@localhost ~]# tar -cvzf file.tar.gz  源文件 ...		//较大
[root@localhost ~]# tar -cvJf file.tar.xz 源文件 ...		//较小

不解压查看压缩包内的信息内容

[root@localhost ~]# tar -tf dir1.tar.gz 

gzip压缩 解压:
#gzip 源文件 …
#gzip -d 压缩文件
#gunzip 压缩文件

[root@localhost ~]# gzip file100 		//压缩file100,压缩文件以.gz结尾
[root@localhost ~]# gzip -dv file100.gz		//解压缩
[root@localhost ~]# gunzip	file100.gz		//解压缩
[root@localhost ~]# gzip -c file100 >/home/file100.gz	//压缩到指定位置(注意以.gz结尾)
[root@localhost ~]# gunzip -c file100.gz >/opt/file100 //解压到指定位置(解压出的名字可以自定义)

bzip2命令的压缩与解压
#bzip2 源文件 …
#bzip2 -d 压缩文件
#bunzip2 压缩文件

[root@localhost ~]# bzip2	file100 		//压缩file100,压缩文件以.bz2结尾
[root@localhost ~]# bzip2	-d 	file100.bz2  	//解压缩
[root@localhost ~]# bunzip2		file100 	//解压缩
[root@localhost ~]# bzip2 -c file100  >/opt/file100.bz2	//压缩到指定位置(注意以.bz2结尾)
[root@localhost ~]# bunzip2 -c file100.bz2 >/home/file100  //解压到指定位置(解压出的名字可以自定义)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值