栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

Linux文件管理:文件查找与打包压缩

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Linux文件管理:文件查找与打包压缩

文章目录
  • 文件查找
    • 简介
      • which
      • find
      • locate
  • 命令文件查找
      • 使用which
      • 使用locate
      • 使用find
  • 文件压缩打包
    • 打包
    • 压缩
    • 解压

文件查找 简介 which

Linux which命令用于查找文件。

which指令会在环境变量$PATH设置的目录里查找符合条件的文件。

语法:

which  文件

参数:

  • n<文件名长度>  指定文件名长度,指定的长度必须大于或等于所有文件中最长的文件名。
  • p<文件名长度>  与-n参数相同,但此处的<文件名长度>包括了文件的路径。
  • w  指定输出时栏位的宽度。
  • V  显示版本信息。

语法:

find

Linux find 命令用来在指定目录下查找文件。任何位于参数之前的字符串都将被视为欲查找的目录名。如果使用该命令时,不设置任何参数,则 find 命令将在当前目录下查找子目录与文件。并且将查找到的子目录和文件全部进行显示。

find   path   -option    expression    action;
find   路径    -选项      描述			动作

路径:就是从哪里开始找

find 根据下列规则判断 path 和 expression,在命令列上第一个 - ( ) , ! 之前的部份为 path,之后的是 expression。如果 path 是空字串则使用目前路径,如果 expression 是空字串则使用 -print 为预设 expression。

locate

Linux locate命令用于查找符合条件的文档,他会去保存文档和目录名称的数据库内,查找合乎范本样式条件的文档或目录。

语法:locate 文件
会找到含有文件名相关的所有文件

locate 与 find 不同: find 是去硬盘找,locate 只在 /var/lib/slocate 资料库中找。
locate 的速度比 find 快,它并不是真的查找,而是查数据库,一般文件数据库在 /var/lib/slocate/slocate.db 中,所以 locate 的查找并不是实时的,而是以数据库的更新为准,一般是系统自己维护,也可以手工升级数据库 ,命令为:updatedb

命令文件查找 使用which


alias:别名,使用命令cp其实就是输入cp -i。使用ls时会对一些文件显示颜色,就是因为它又一个color的关键字。别名系统

alias使用方法:

alias  别名=“命令(可加选项)”

例:alias ch="ls --color=auto -l"

使用locate

使用find
  1. 按文件名字
    引号是转义符,防止一些符号有特殊含义
[root@tianyun ~]# find /etc -name "ifcfg-eth0"
[root@tianyun ~]# find /etc -iname "ifcfg-eth0" //-i 忽略大小写
[root@tianyun ~]# find /etc -iname "ifcfg-eth*"


Linux中严格区分大小写,查找文件时使用iname选项就不会区分大小写来查找文件

不知道完全文件名可使用*来进匹配

  1. 按文件大小
    寻找大于5M的文件,属组的右边就是文件的大小单位是字节
[root@tianyun ~]# find /etc -size +5M //大于 5M
[root@tianyun ~]# find /etc -size 5M
[root@tianyun ~]# find /etc -size -5M
[root@tianyun ~]# find /etc -size +5M -ls //-ls 找到的处理动作


寻找等于5M的文件,会有误差,但是是在5M左右

寻找小于5M的文件,小于5M的文件过多就显示前七行

  1. 指定查找的目录深度,/根是第一级目录,/etc是第二级目录,以此类推
    maxdepth,直到第四级目录才找到文件
[root@tianyun ~]# find / -maxdepth 3 -a -name "ifcfg-eth0"


4. 按文件属组、属主找
uesr 和 group

[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
[root@tianyun ~]# find /home -user jack -o -group hr

  1. 按文件类型
[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 管道文件


  1. 按文件权限
    -ls就是添加的动作,根据需求添加
  2. 找到文件后的处理动作
    找到文件后默认是看,带有一个-print动作
[root@tianyun ~]# find /etc -name "ifcfg*"
[root@tianyun ~]# find /etc -name "ifcfg*" -print
[root@tianyun ~]# find /etc -name "ifcfg*" -ls
[root@tianyun ~]# find /etc -name "ifcfg*" -exec cp -rvf {} /tmp ;
[root@tianyun ~]# find /etc -name "ifcfg*" -ok cp -rvf {} /tmp ;

找到后删除

找到后复制,-ok是一个中转符号在他后面携带一些特殊参数,cp命令需要:cp 源文件 目标文件)=,所以{}就是代表源文件。;就是结束符

文件压缩打包

Linux tar(英文全拼:tape archive )命令用于备份文件。

tar 是用来建立,还原备份文件的工具程序,它可以加入,解开备份文件内的文件。

tar命令是Uinx/Linux系统中备份文件的可靠方法,几乎可以工作于任何工作环境中,它的使用权限是所有用户,建议针对目录。

打包

语法:

tar -cf   打包后的文件名   源文件
-c创建文件  -f文件名 ,文件后最好带上后缀
# tar -cf test.tar   //打包 a.c文件为test.tar

压缩

语法

tar -cfz   压缩后的文件名   源文件
-z是一种压缩工具 gzip,压缩文件最好带上后缀
# tar -czvf test.tar.gz a.c   //压缩 a.c文件为test.tar.gz

[root@yangs ~]# tar -czf etc1.tar.gz/etc //-z 调用 gzip
[root@yangs ~]# tar -cjf etc2.tar.bz2 /etc //-j 调用 bzip2
[root@yangs ~]# tar -cJf etc3.tar.xz/etc //-J 调用 xz
[root@yangs ~]# 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
解压

语法:

tar -xf 压缩包 
# tar -xf test.tar.gz a.c   //解压test.tar.gz文件
解压到当前目录下

[root@yangs ~]# tar -tf sys.tar.xz
[root@yangs ~]# tar -xzvf etc1.tar.gz 
[root@yangs ~]# tar -xvf etc1.tar.gz //无需指定解压工具,tar 会自动判断
[root@yangs ~]# tar -xvf etc2.tar.bz2 -C /tmp //-C 重定向到//tmp 目录
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/914013.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号