详解 Linux文件查找、打包压缩
grep:文件内容过滤
find:文件查找,针对文件名;locate:文件查询,依赖数据库
which:命令查询
2,命令文件查找
查找ls 命令的位置 # whic
Linux文件查找、打包压缩详解文件查找1,简介 grep:文件内容过滤 find:文件查找,针对文件名;locate:文件查询,依赖数据库 which:命令查询 2,命令文件查找 查找ls 命令的位置 # which ls //从PATH环境变量 或者 # whereis vim 一.文件查找工具locate和find 1.locate 数据库:/var/lib/mlocate/mlocate.db
注意: 在mnt目录中的文件无法用locate搜索。 因为配置文件中有排除查找路径。 /etc/updatedb.conf 第一行PRUNE_BIND_MOUNTS="yes"的意思是:是否进行限制搜索。 第二行是排除检索的文件系统类型,即列出的文件系统类型不进行检索。 第二行表示对哪些后缀的文件排除检索linux 压缩,也就是列在这里面的后缀的文件跳过不进行检索。不同后缀之间用空格隔开。 第四行是排除检索的路径,即列出的路径下的文件和子文件夹均跳过不进行检索。updatedb之后使用locate仍然找不到想要文件 2.find
2.1查找路径:指定具体目标路径;默认为当前目录 指定的查找标准,可以文件名、大小、类型、权限等标准进行;默认为找出指定路径下的所有文件 按文件名 [root@tianyun ~]# find /etc -name "ifcfg-ens32" [root@tianyun ~]# find /etc -iname "ifcfg-ens32" //-i忽略大小写 [root@tianyun ~]# find /etc -iname "ifcfg-ens*" 按文件大小 [root@tianyun ~]# find /etc -size +5M //大于5M [root@tianyun ~]# find /etc -size 5M //等于5M [root@tianyun ~]# find /etc -size -5M //小于5M [root@tianyun ~]# find /etc -size +5M -ls //-ls找到的处理动作 指定查找的目录深度 -maxdepth levels -mindepth levels [root@tianyun ~]# find / -maxdepth 3 -a -name "ifcfg-en" [root@tianyun ~]# find / -maxdepth 4 -a -name "ifcfg-en" 按时间找(atime,mtime,ctime) 时间的概念 atime:(access time)显示的是文件中的数据最后被访问的时间,比如系统的进程直接使用或通过一些命令和脚本间接使用。(执行一些可执行文件或脚本) mtime: (modify time)显示的是文件内容被修改的最后时间,比如用vi编辑时就会被改变。(也就是Block的内容) ctime: (change time)显示的是文件的权限、拥有者、所属的组、链接数发生改变时的时间。当然当内容改变时也会随之改变(即inode内容发生改变和Block内容发生改变时)* * *案例* 按时间找(atime,mtime,ctime): [root@tianyun ~]# find /etc -mtime +5 //修改时间超过5天 [root@tianyun ~]# find /etc -mtime 5 //修改时间等于5天 [root@tianyun ~]# find /etc -mtime -5 //修改时间5天以内 按文件属性、属组找 [root@tianyun ~]# find /home -user jack //属主是jack的文件 [root@tianyun ~]# find /home -group hr //属组是hr组的文件 [root@tianyun ~]# find /home -user jack -group hr //和 [root@tianyun ~]# find /home -user jack -a -group hr //-a和 [root@tianyun ~]# find /home -user jack -o -group hr //-o或 [root@tianyun ~]# find /home -nouser [root@tianyun ~]# find /home -nogroup [root@tianyun ~]# find /home -nouser -o -nogroup 按文件类型(了解) [root@tianyun ~]# find /dev -type f //f普通 [root@tianyun ~]# find /dev -type d //d目录 [root@tianyun ~]# find /dev -type l //l链接 [root@tianyun ~]# find /dev -type b //b块设备 [root@tianyun ~]# find /dev -type c //c字符设备 [root@tianyun ~]# find /dev -type s //s套接字 [root@tianyun ~]# find /dev -type p //p管道文件 按文件权限 普通权限 [root@tianyun ~]# find . -perm 644 -ls //精确权限 [root@tianyun ~]# find . -perm -644 -ls //包含权限即可*** *特殊权限* [root@tianyun ~]# find /usr/bin /usr/sbin -perm -4000 -ls //包含set uid [root@tianyun ~]# find /usr/bin /usr/sbin -perm -2000 -ls //包含set gid [root@tianyun ~]# find /usr/bin /usr/sbin -perm -1000 -ls //包含sticky 按正则表达式(了解) -regex pattern [root@tianyun ~]# find /etc/ -regex '.*ifcfg-ens3[0-9]' [0-9] 任意一个数字 找到后处理的动作ACTIONS 类型 -print 打印, 默认选项 -ls -delete -exec 后面跟自定义的shell命令 -ok 后面跟自定义的shell命令*** *示例* # find /etc -name "ifcfg" # find /etc -name "ifcfg" -print # find /etc -name "ifcfg" -ls # find /etc -name "775" -delete /775.txt是自定义文件 # find /etc -name "ifcfg" -exec cp -rvf {} /tmp \; //不提示 # find /etc -name "ifcfg" -ok cp -rvf {} /tmp \; //提示 # find /etc -name "775*" -exec rm -rf {} \; 处理动作:对符合条件的文件做操作,默认输出至屏幕
2.4由于很多命令不支持管道|来传递参数,xargs用于产生某个命令的参数,xargs
二,文件打包及压缩1,简介 tar命令是Unix/Linux系统中备份文件的可靠方法,几乎可以工作于任何环境中,它的使用权限是所有用户。 建议针对目录 2,打包,压缩 语法:tar 选项 压缩包名称 源文件 ===打包,压缩=== # tar -czf etc-gzip.tar.gz /etc///z是gzip # tar -cjf etc-bzip.tar.bz /etc///j是bzip # tar -cJf etc-xzip.tar.xz /etc///J是xzip 观察三个包的体积。 # ll -h etc* -rw-r--r--. 1 root root 11M 10月 14 10:07 etc-gzip.tar.gz -rw-r--r--. 1 root root 8.9M 10月 14 10:08 etc-bzip.tar.bz -rw-r--r--. 1 root root 7.6M 10月 14 10:08 etc-xzip.tar.xz 压缩速度和压缩体积成反比。 file etc.tar.gz 3,压缩,解包 ===解压,解包=== # tar -tf sys.tar.xz//t查看f文件名 # tar -xzvf etc1.tar.gz //x解压z调gzip,还可以自动判断 # tar -xvf etc1.tar.gz //无需指定解压工具,tar会自动判断 # tar -xvf etc2.tar.bz2 -C /tmp //-C重定向到//tmp目录 终极大法: # tar xf etc3.tar.xz //简单粗暴 (编辑:威海站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |