Linux基本操作(9)-文件查找、文件压缩、文件解压

一、文件查找

1、find:文件查找,针对文件名

1)格式:find [path...] [options]  [expression] [action]
                 命令 路径       选项             表达式        动作

2)按文件名:-i是忽略大小**
① [root@localhost jiaofan]# find /home/jiaofan -name “dir1”

[root@localhost jiaofan]# find /home/jiaofan  -name  "dir1"
/home/jiaofan/dir3/dir1
/home/jiaofan/jiaofan/dir1
[root@localhost jiaofan]# 

② [root@localhost jiaofan]# find /home/jiaofan -iname “dir1”

[root@localhost jiaofan]# find /home/jiaofan  -iname  "dir1"
/home/jiaofan/dir3/dir1
/home/jiaofan/jiaofan/dir1
[root@localhost jiaofan]#

③ [root@localhost ~]# find /etc -iname “hos*”

 [root@localhost jiaofan]# find /home/jiaofan  -iname  "w*"
/home/jiaofan/w.txt
[root@localhost jiaofan]# 

3)按文件大小查找
①文件大于5M:[root@localhost ~]# find /etc -size +5M

[root@localhost jiaofan]# find  /etc  -size  +5M
/etc/udev/hwdb.bin
[root@localhost jiaofan]# 

②文件等于5M:[root@localhost ~]# find /etc -size 5M
③文件小于5M:[root@localhost ~]# find /etc -size -5M
4)指定查找的目录深度:[root@localhost ~]# find / -maxdepth 4 -a(and) -name “ifcfg-en*”

 [root@localhost jiaofan]# find  / -maxdepth  4  -a -name  "ifcfg-en*"
find: ‘/proc/5108’: 没有那个文件或目录
/etc/sysconfig/network-scripts/ifcfg-ens33
/home/jiaofan/ifcfg-ens33
[root@localhost jiaofan]# 

5)按文件属主、属组查找:
① [root@localhost ~]# find /home -user jiao1 //属主是jiao1的文件

[root@localhost jiaofan]# find  /home  -user  jiao1
/home/jiaofan/no.txt
/home/jiao1
[root@localhost jiaofan]# 

② [root@localhost ~]# find /home -group zu1 //属组是zu1组的文件

[root@localhost jiaofan]# find  /home  -group  zu1
/home/jiaofan/no.txt
[root@localhost jiaofan]# 

6)按文件类型查找:[root@localhost ~]# find /tmp -type*
① f普通文件、b快设备文件、d目录、p管道、l 链接
7)按文件权限查找

[root@localhost jiaofan]# find . -perm 644 -ls
51513786    4 -rw-r--r--   1 jiaofan  jiaofan        18 8月  8  2019 ./.bash_logout
[root@localhost jiaofan]# 

8)找到后处理的动作action
①[root@localhost ~]# find . -perm 715 -print
[root@localhost ~]# find . -perm 715 -ls
②[root@localhost ~]# find /etc -name “775*” -delete
③[root@localhost ~]# find /etc -name “ifcfg*” -ok cp -rvf {} /tmp ;

2、which:找命令

[root@localhost ~]# which wim
/usr/bin/which: no wim in (/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:/root/bin)
[root@localhost ~]# 

3、locate:文件查找,依赖数据库,必须知道文件名字。

1)locate仅仅在开机时刷新数据库,所以刚刚创建的无法用locate查找

[root@localhost ~]# touch /home/jiaofan/wabcde.txt
[root@localhost ~]# locate  wabcde.txt
[root@localhost ~]# 

2)updatedb:更新数据库,之后可查看文件刚刚建立的文件。

[root@localhost ~]# touch /home/jiaofan/wabcde.txt
[root@localhost ~]# locate  wabcde.txt
[root@localhost ~]# updatedb
[root@localhost ~]# locate  wabcde.txt
/home/jiaofan/wabcde.txt
[root@localhost ~]# 

3)locate数据库文件不会查找/tmp临时文件等文件

[root@localhost ~]# touch /tmp/wabcd.txt
[root@localhost ~]# updatedb
[root@localhost ~]# locate  wabcd.txt
[root@localhost ~]# 

二、文件压缩打包

1、打包、压缩:tar 选项 压缩包名称 源文件
2、压缩jiaofan文件夹:-cf 压缩 -v显示压缩过程

[root@localhost home]# tar -cf jiaofan.tar  /home/jiaofan
tar: 从成员名中删除开头的“/”
[root@localhost home]# ls
jiaofan  jiaofan.tar
[root@localhost home]# 

3、区别: -z -j -J 这三个只是压缩之后大小不同,压缩时间不同。

[root@localhost home]# tar -czf jiaofan.z.tar  jiaofan
[root@localhost home]# tar -cjf jiaofan.j.tar  jiaofan
[root@localhost home]# tar -cJf jiaofan.J.tar  jiaofan
[root@localhost home]# ll -h | grep jiaofan.
drwx------. 26 jiaofan jiaofan 4.0K 3月  26 10:06 jiaofan
-rw-r--r--.  1 root    root    3.0M 3月  26 15:56 jiaofan.j.tar
-rw-r--r--.  1 root    root    789K 3月  26 15:57 jiaofan.J.tar
-rw-r--r--.  1 root    root    3.9M 3月  26 15:56 jiaofan.z.tar
[root@localhost home]# 

4、解压jiaofan文件夹

[root@localhost jiao1]# ls
jiaofan.tar
[root@localhost jiao1]# tar -xf jiaofan.tar 
[root@localhost jiao1]# ls
jiaofan  jiaofan.tar
[root@localhost jiao1]# 
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值