Linux查找和压缩文件:find、which、whereis、tar

1,077 阅读10分钟

1 查找文件

1.1 find

采用递归方式,根据目标的名称、类型、大小等不同属性进行精细查找。

命令的特点:

  • 精确查找
  • 实时查找
  • 支持查找条件很多
  • 各表达式之间使用逻辑运算符, “-a”表示而且(and),“-o”表示 或者(or)

find命令格式:

find [OPTION]...  [查找路径] [查找条件] [处理动作]

查找路径:指定具体目标路径;不指定则默认为当前目录。

查找条件:可以对文件名、大小、类型、权限等标准进行查找;默认为找出指定路径下的所有文件。

处理动作:对符合条件的文件做操作,默认输出至屏幕(print)。

处理动作:

  • -ls 对查找到的文件列出属性信息

  • -delete 对查找到的文件进行删除

  • -exec COMMAND {} \;

    对查找到的每个文件执行由COMMAND指定的命令,{}: 用于引用查找到的文件名称自身。

  • -ok COMMAND {} \;

    对查找到的每个文件执行由COMMAND指定的命令,对于每个文件执行命令之前,都会交互式要求用户确认。

常用可选项:

查找类型关键字说明
按名称查找-name根据目标文件的名称进行查找,允许使用“*”及“?”通配符; 如果名字使用通配符,需要加” “来查询
按文件大小查找-size根据目标文件的大小进行查找 一般使用“+”、“-”号设置超过或小于指定的大小作为查找条件 常用的容量单位包括 kB(注意 k 是小写)、MB、GB
按文件属主/属组查找-user/-group根据文件是否属于目标用户进行查找
查找无属主/属组的文件-nouser/-nogroup查找无属主/属组的文件
按文件类型查找-type根据文件的类型进行查找 文件类型包括普通文件(f)、目录(d)、块设备文件(b)、字符设备文件(c)等
按inode号查找-inum根据文件inode号查找
按权限查找-perm按文件权限查找
最大搜索目录深度-maxdepth将你的文件以分级的形式查找,,最多搜索到某级目录
最小搜索目录深度-mindepth将你的文件以分级的形式查找,最少搜索到某级目录
按三种时间查询-三种时间-atime、-mtime、-ctime

示例:

1)按文件名称查找 -name

[root@test1 opt]# find /etc/ -name passwd     //在/etc/目录下查找名字叫passwd的文件
/etc/pam.d/passwd
/etc/passwd
​
[root@test1 opt]# find /etc/ -name "*.conf"   //查找所有.conf 结尾的 ,不要在当前路径下找自己路径下的文件
/etc/resolv.conf
/etc/fonts/conf.d/57-dejavu-serif.conf
/etc/fonts/conf.d/65-1-vlgothic-gothic.conf
/etc/fonts/conf.d/31-cantarell.conf
/etc/fonts/conf.d/65-0-lohit-nepali.conf
/etc/fonts/conf.d/59-liberation-mono.conf
/etc/fonts/conf.d/65-0-lohit-bengali.conf
​
[root@localhost boot]# find ./ -name "vm*"    //查找vm开头的文件
./vmlinuz-3.10.0-693.el7.x86_64
./vmlinuz-0-rescue-869778d6675742a5968d2ea8c0e087b2

2)按文件属主查找 -user、-nouser

[root@test1 opt]# find /mnt -user root    //查找属主为root的文件
/mnt
/mnt/abc
/mnt/ad
/mnt/ad/ad2
/mnt/ad/ad2/bbbbb.txt
/mnt/zhangsanlianjie

​
[root@localhost opt]# find /home -user zhangsan -ls    //查找/home下属于zhangsan的文件并列出属性
18892062    0 drwx------   3 zhangsan zhangsan       78 2月  8 03:02 /home/zhangsan
26604086    0 drwxr-xr-x   4 zhangsan zhangsan       39 1月 18 17:29 /home/zhangsan/.mozilla
 35529    0 drwxr-xr-x   2 zhangsan zhangsan        6 6月 10  2014 /home/zhangsan/.mozilla/extensions
8922083    0 drwxr-xr-x   2 zhangsan zhangsan        6 6月 10  2014 /home/zhangsan/.mozilla/plugins
18892063    4 -rw-r--r--   1 zhangsan zhangsan       18 8月  3  2017 /home/zhangsan/.bash_logout
16797782    4 -rw-r--r--   1 zhangsan zhangsan      193 8月  3  2017 /home/zhangsan/.bash_profile
18875334    4 -rw-r--r--   1 zhangsan zhangsan      231 8月  3  2017 /home/zhangsan/.bashrc
​
​
[root@localhost opt]# useradd hehe     //创建用户hehe
[root@localhost opt]# userdel hehe     //删除用户hehe,因为没有加-r,与用户相关的文件和目录没有被删除
[root@localhost opt]# ls /home/
hehe  mysql  zhangsan
[root@localhost opt]# ll /home/
总用量 0
drwx------. 3     1002     1002 78 8月  26 09:27 hehe
drwx------. 3 mysql    mysql    78 8月  25 11:45 mysql
drwx------. 3 zhangsan zhangsan 78 8月   6 20:57 zhangsan
[root@localhost opt]# find /home/ -nouser     //查找home下的无主文件
/home/hehe
/home/hehe/.mozilla
/home/hehe/.mozilla/extensions
/home/hehe/.mozilla/plugins
/home/hehe/.bash_logout
/home/hehe/.bash_profile
/home/hehe/.bashrc

3)按文件类型查找 -type

[root@localhost opt]# find /boot -type d    //查找/boot下的目录文件
/boot
/boot/efi
/boot/efi/EFI
/boot/efi/EFI/centos
/boot/grub2
/boot/grub2/i386-pc
/boot/grub2/locale
/boot/grub2/fonts
/boot/grub

4)按目录层级查找

[root@localhost boot]# find /tmp/test -maxdepth 2  -mindepth 2   //只向下看第二级目录
/tmp/test/aaa/1.txt
/tmp/test/aaa/2.txt
/tmp/test/bbb/3.txt

5)按文件权限查找 -perm

[root@localhost opt]# find -perm 644   //查找属主有读写权限、属组和其他人只有读权限的文件
./100.img
./99.img
./a.txt 

6)按文件大小查找 -size

[root@test1 opt]# find -size 1k     //查找(0,1]k的文件
./rh
./rh/aaa
[root@localhost boot]# find /boot/ -size -10M -a -name "*img"   //查找9M以内(含9M)、且名称结尾为img的文件
/boot/grub2/i386-pc/core.img
/boot/grub2/i386-pc/boot.img
[root@localhost opt]# find -size 1G    //查找1G的文件会把所有都显示出来,这是因为1G表示0G到1G之间,不含0G,即(0,1]。
.
./100.img
./99.img
​
​
#按大小查找新建两个文件:
[root@localhost opt]# dd if=/dev/zero of=99.img bs=99M count=1
[root@localhost opt]# dd if=/dev/zero of=100.img bs=100M count=1
[root@localhost opt]# ls
100.img  99.img
[root@localhost opt]# find -size 100M
./100.img
[root@localhost opt]# find /opt/ -size 100M
/opt/100.img
[root@localhost opt]# ll 99.img 
-rw-r--r--. 1 root root 103809024 8月  26 10:02 99.img
[root@localhost opt]# echo >>99.img 
[root@localhost opt]# ll 99.img 
-rw-r--r--. 1 root root 103809025 8月  26 10:03 99.img
[root@localhost opt]# find /opt/ -size 100M   //这是因为linux中的100M不是正好100M,是99M开始到100M,不包括99M,即(99-100]
/opt/100.img
/opt/99.img

注意:按文件大小查找时,大小范围需注意

  • find -size 1G:查找的是从0G到1G,不包括0G,即(0,1]。
  • find -size 2G:查找的是从1G到2G,不包括1G,即(1,2]。
  • find -size 1024M:查找的是从1023M到1024M,不包括1023M,即(1023,1024]。

思考:find -size 1024M 和 1G 一样么?

  • 1024M 表示 1023M ~ 1024M,不包括 1023M。

  • 1G 表示 0G ~1G,不包括 0G。

    故使用 find -size 1024M 查找更加精准。

思考:10k、-10k、+10k的大小范围分别是多少?

  • -size 10k :表示9k到10k,包括10k、不包括9k,即(9,10]。
  • -size -10k :表示9k以内,包括9k,即 [0,9]。
  • -size +10k :表示10k以上,不包括10k,即(10,+∞)。

7)查找到文件后并做其他处理

# 找到img结尾的文件并删除的三种方式:
[root@localhost opt]# find -name "*.img" -delete               
[root@localhost opt]# find -name "*.img" -exec rm -rf {} \;
[root@localhost opt]# find -name "*.img" | xargs rm -rf

​
[root@localhost opt]# find -name "*.txt"  -ok mv {} /mnt \;   //查找.txt的文件并移动到/mnt目录下,对于每个文件执行命令之前,都会交互式要求用户确认
[root@localhost opt]# find -name "*.txt"  -exec mv {} /mnt \;   //查找.txt的文件并移动到/mnt目录下

8)按时间查找:

#以“天”为单位:
find  -atime  [+/-]#      //按读取时间查找
#     //表示[#,#+1)
+#    //表示[#+1,+∞)
-#    //表示[0,#)
​
find  -mtime            //按文件内容改变时间查找
find  -ctime            //按元数据改变时间查找
​
#以“分钟”为单位:
-amin
-mmin
-cmin
​
find -mtime 10      //10天到11天,[10,11)
find -mtime +10     //代表11天以上,[11,+∞)
find -mtime -10     //10天以内,[0,10)

9)比较“或”与“且”的优先级

[root@localhost opt]# find /etc/ -type d -o -type l |wc -l    //查找/etc目录下的目录和链接文件并统计数量
1008
[root@localhost opt]# find /etc/ -type d -o -type l -ls |wc -l   //查找/etc目录下的目录和链接文件,并显示链接文件的详细信息后统计数量
274 
[root@localhost opt]# find /etc/ -type d -o -type l -a -ls |wc -l    //此条命令与上条命令对比实际隐藏了一个-a(且),先执行了-type 1 -a -ls,处理动作只显示了链接文件
274
[root@localhost opt]# find /etc/ ( -type d -o -type l ) -a -ls |wc -l  //如果“或”和“且”实在需要一起显示,可以使用括号和转义符
1008

10)反向查找,使用-not或!

[root@localhost d02]# ll      //查看目录下的文件及其属性
总用量 0
-rw-r--r--. 1 root root 0 2月  16 08:56 1.txt
-rw-r--r--. 1 root root 0 2月  16 08:56 2.txt
drwxr-xr-x. 2 root root 6 2月  16 08:55 dir0201
drwxr-xr-x. 2 root root 6 2月  16 08:55 dir0202
-rw-r--r--. 1 root root 0 2月  16 08:56 file01
-rw-r--r--. 1 root root 0 2月  16 08:56 file02
[root@localhost dir02]# find -not -type d   //使用-not,查找不是文件类型不是目录的文件
./1.txt
./2.txt
./file01
./file02
[root@localhost d02]# find ! -type d   //也可以使用!来代替-not,进行反向查找
./1.txt
./2.txt
./file01
./file02
[root@localhost d02]# find ! -type d ! -name "*.txt"   //查找不是目录且名称不以.txt结尾的文件
./file01
./file02

11)找到/mnt目录下以f开头的文件,并全部加上后缀.bak

 find /mnt -name f* -exec mv {} {}.bak \;

12)找到文件并删除的三种方式:

 find -name "*.img" -delete               
 find -name "*.img" -exec rm -rf {} \;
 find -name "*.img" | xargs rm -rf

1.2 which

which 用来查看命令的位置。 在PATH变量指定的路径中,搜索某个系统命令的位置 。

命令格式:

which 命令/程序名

示例:

[root@localhost boot]# which ls    //查看ls命令文件的位置
alias ls='ls --color=auto'
        /usr/bin/ls

1.3 whereis

whereis 用来查找命令的具体位置。可以查看到二进制文件(参数-b)、man说明文件(参数-m)和源代码文件(参数-s)。如果省略参数,则返回所有信息。

示例:

[root@localhost ~]# whereis ls       //whereis 可以查找命令具体的位置所在
ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz /usr/share/man/man1p/ls.1p.gz

2 压缩文件

2.1 gzip和bzip2

gzip和bzip2都是压缩软件,比如windows里的好压和360压缩或微软自带的等等。

相同点:

  • 只能压缩文件 , 不能压缩目录
  • 默认压缩后会删除源文件。(bzip2可以使用-k保留源文件)

区别:

  • gzip比bzip2的压缩速度快,而bzip2的压缩率高于gzip。

命令格式:

  • 压缩:

gzip [-9] 文件名…

bzip2 [-9] 文件名...

-1~9 指定压缩级别, 数字越大压缩级别越高 。-1最快压缩,-9最大压缩 。

  • 解压缩:

gzip -d .gz格式的压缩文件;

bzip2 -d .bz2格式的压缩文件

示例:

[root@localhost data]# ls
f1  f2
[root@localhost data]# bzip2 f1    //压缩后源文件被删除
[root@localhost data]# ls
f1.bz2  f2
[root@localhost data]# bzip2 -k f2    //使用-k保留源文件
[root@localhost data]# ls
f1.bz2  f2  f2.bz2
[root@localhost data]# bzip2 -d f1.bz2     //解压缩
[root@localhost data]# ls
f1  f2  f2.bz2

2.2 归档tar 文件夹

tar (tape archive) 磁带归档,磁带便宜磁带机很贵,慢永久保存。

命令格式:

tar [选项] ... 归档文件名(压缩包名字) 源文件或目录 tar [选项] ... 归档文件名 [-C 目标目录]

选项说明:

  • -c:创建(Create).tar 格式的包文件
  • -x:解开.tar 格式的包文件
  • -C:解压时指定释放的目标文件夹 指定目录
  • -f:表示使用归档文件(一般都要带上表示使用tar)
  • -v:输出详细信息(Verbose)
  • -t:列出归档内容
  • -j:调用 bzip2 程序进行压缩或解压
  • -z:调用 gzip 程序进行压缩或解压

示例:

[root@localhost data]# tar -zcvf vm.tar.gz f1 f2 f3   //将三个文件归档后调用gzip程序压缩成vm.tar.gz
f1
f2
f3
[root@localhost data]# tar -jcvf vm.tar.bz f1 f2 f3   //将三个文件归档后调用bzip2程序压缩成vm.tar.bz2
f1
f2
f3
​
[root@localhost data]# tar -zxvf vm.tar.gz -C /opt    //将vm.tar.gz文件解压缩到/opt目录下
f1
f2
f3
​
[root@localhost data]# tar -tf ff.tar.gzip    //列出归档内容
file01
file02
​
[root@localhost data]# tar -tvf /tmp/ceshi/ff.tar   //详细列举归档文件中的所有文件(包括属性信息)
-rwxr--r-- root/root        87 2022-01-21 17:37 file01
-rw-r--r-- user01/hr         0 2022-01-19 17:01 file02
​