Linux文件管理(二)

Linux文件管理(二)

上一次的文件管理(一)只能说是一些很基本的东西,还有一些内容也很常见,例如解压缩,文件查找、文件类型、甚至是文件权限等等。

一、文件解压缩

工具 后缀格式 解释
tar .tar 只打包
gzip .gz/.tar.gz/.tgz 普通压缩
bzip2 .bz2/.tar.bz2 压缩率更高,但是更慢
xz .xz/.tar.xz 压缩率最高,时间最慢
zip .zip 跨平台兼容性好

不同压缩方法都可以和tar 配合。

1.1 tar

Linux 下打包工具,可以将多个文件、文件夹打包成一个文件,便于文件的传输。

打包

1
tar -cvf .tar文件 待打包的文件1 待打包的文件2 ...

横杠 - 可省略

1
tar cvf .tar文件 待打包的文件1 待打包的文件2 ...
  • f 指定 .tar 文件
  • v 显示详细信息
  • c 代表是打包
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 显示打包前的文件列表
[root@centos7 ~]# ls
anaconda-ks.cfg dir1 init.sh nginx.rpm sshd_config

# 打包一堆文件
[root@centos7 ~]# tar cvf demo1.tar nginx.rpm sshd_config dir1/ init.sh anaconda-ks.cfg
nginx.rpm
sshd_config
dir1/
dir1/1.txt
init.sh
anaconda-ks.cfg
[root@centos7 ~]#

# 显示打包后的文件
[root@centos7 ~]# ls
anaconda-ks.cfg demo1.tar dir1 init.sh nginx.rpm sshd_config
[root@centos7 ~]#

解包

1
tar xvf .tar文件 -C 解包后放置的目录

1
tar -xvf .tar文件 -C 解包后放置的目录
  • x 代表这是解包
  • C 指定解包后文件的放置目录。强烈推荐,否则要么很烂、要么会覆盖当前目录的文件。
  • f 指定 .tar 文件
  • v 显示详细信息
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 创建目标文件夹
[root@centos7 ~]# mkdir target1
[root@centos7 ~]# ls
anaconda-ks.cfg demo1.tar dir1 init.sh nginx.rpm sshd_config target1

# 解包
[root@centos7 ~]# tar xvf demo1.tar -C target1/
nginx.rpm
sshd_config
dir1/
dir1/1.txt
init.sh
anaconda-ks.cfg

# 查看效果
[root@centos7 ~]# ls target1/
anaconda-ks.cfg dir1 init.sh nginx.rpm sshd_config
[root@centos7 ~]#

1.2 tar + gzip

tar 中以参数 z 体现。

压缩

1
tar zcvf 文件.tar.gz 文件1 文件2 ...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 压缩前
[root@centos7 ~]# ls
anaconda-ks.cfg dir1 init.sh nginx.rpm sshd_config

# 打包并压缩
[root@centos7 ~]# tar zcvf demo1.tar.gz init.sh nginx.rpm dir1/ sshd_config anaconda-ks.cfg
init.sh
nginx.rpm
dir1/
sshd_config
anaconda-ks.cfg

# 压缩后的效果
[root@centos7 ~]# ls
anaconda-ks.cfg demo1.tar.gz dir1 init.sh nginx.rpm sshd_config
[root@centos7 ~]#

解压

1
tar zxvf 文件.tar.gz -C 目标文件夹
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 创建目标文件夹
[root@centos7 ~]# mkdir tar
[root@centos7 ~]# ls
anaconda-ks.cfg demo1.tar.gz dir1 init.sh nginx.rpm sshd_config tar

# 解压并解包
[root@centos7 ~]# tar zxvf demo1.tar.gz -C tar
init.sh
nginx.rpm
dir1/
sshd_config
anaconda-ks.cfg

# 查看解压并解包后的效果
[root@centos7 ~]# ls tar/
anaconda-ks.cfg dir1 init.sh nginx.rpm sshd_config
[root@centos7 ~]#

1.3 tar + bz2

压缩率更高,但时间花费长。在 tar 的参数j 体现。需要先安装bzip2

安装

1
yum install -y bzip2

否则会报错

1
2
3
4
5
6
7
[root@centos7 ~]# tar jcvf demo1.tar.bz2 anaconda-ks.cfg  dir1/ init.sh nginx.rpm sshd_config 
tar (child): bzip2: Cannot exec: No such file or directory
tar (child): Error is not recoverable: exiting now
anaconda-ks.cfg
dir1/
init.sh
nginx.rpm

压缩

1
tar jcvf 文件.tar.bz2 文件1 文件2 ...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 压缩前效果
[root@centos7 ~]# ls
anaconda-ks.cfg dir1 init.sh nginx.rpm sshd_config

# 打包并压缩
[root@centos7 ~]# tar jcvf demo1.tar.bz2 anaconda-ks.cfg dir1/ init.sh nginx.rpm sshd_config
anaconda-ks.cfg
dir1/
init.sh
nginx.rpm
sshd_config

# 压缩后效果
[root@centos7 ~]# ls
anaconda-ks.cfg demo1.tar.bz2 dir1 init.sh nginx.rpm sshd_config
[root@centos7 ~]#

解压

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 解压前效果
[root@centos7 ~]# mkdir tar1
[root@centos7 ~]# ls
anaconda-ks.cfg demo1.tar.bz2 dir1 init.sh nginx.rpm sshd_config tar1
[root@centos7 ~]#

# 解压
[root@centos7 ~]# tar jxvf demo1.tar.bz2 -C tar1/
anaconda-ks.cfg
dir1/
init.sh
nginx.rpm
sshd_config

# 解压后效果
[root@centos7 ~]# ls tar1/
anaconda-ks.cfg dir1 init.sh nginx.rpm sshd_config
[root@centos7 ~]#

1.4 tar + xz

压缩率最高,但时间花费最长。在 tar 的参数J 体现。

压缩

1
tar Jcvf 文件.tar.xz 文件1 文件2 ...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 压缩前效果
[root@centos7 ~]# ls
anaconda-ks.cfg dir1 init.sh nginx.rpm sshd_config

# 压缩
[root@centos7 ~]# tar Jcvf demo1.tar.xz anaconda-ks.cfg dir1/ init.sh nginx.rpm sshd_config
anaconda-ks.cfg
dir1/
init.sh
nginx.rpm
sshd_config

# 压缩后效果
[root@centos7 ~]# ls
anaconda-ks.cfg demo1.tar.xz dir1 init.sh nginx.rpm sshd_config
[root@centos7 ~]#

解压

1
tar Jxvf 文件.tar.xz -C 目标文件夹
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 解压前效果
[root@centos7 ~]# mkdir tar2
[root@centos7 ~]# ls
anaconda-ks.cfg demo1.tar.xz dir1 init.sh nginx.rpm sshd_config tar2

# 解压
[root@centos7 ~]# tar Jxvf demo1.tar.xz -C tar2/
anaconda-ks.cfg
dir1/
init.sh
nginx.rpm
sshd_config

# 解压后效果
[root@centos7 ~]# ls tar2/
anaconda-ks.cfg dir1 init.sh nginx.rpm sshd_config
[root@centos7 ~]#

1.5 zip

最常见的压缩格式,跨平台兼容性好。需要安装 zipunzip

安装

1
yum install -y zip unzip

否则会显示

1
2
-bash: zip: command not found
-bash: unzip: command not found

压缩

1
zip 文件.zip 文件1 文件2 ...
1
2
3
4
5
6
7
8
9
10
11
[root@centos7 ~]# ls
anaconda-ks.cfg dir1 init.sh nginx.rpm sshd_config
[root@centos7 ~]# zip demo1.zip anaconda-ks.cfg dir1/ init.sh nginx.rpm sshd_config
adding: anaconda-ks.cfg (deflated 44%)
adding: dir1/ (stored 0%)
adding: init.sh (deflated 37%)
adding: nginx.rpm (deflated 3%)
adding: sshd_config (deflated 56%)
[root@centos7 ~]# ls
anaconda-ks.cfg demo1.zip dir1 init.sh nginx.rpm sshd_config
[root@centos7 ~]#

解压

1
unzip 文件.zip -d 目标文件夹
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 创建目标文件夹
[root@centos7 ~]# mkdir tar3
[root@centos7 ~]# ls
anaconda-ks.cfg demo1.zip dir1 init.sh nginx.rpm sshd_config tar3

# 解压
[root@centos7 ~]# unzip demo1.zip -d tar3/
Archive: demo1.zip
inflating: tar3/anaconda-ks.cfg
creating: tar3/dir1/
inflating: tar3/init.sh
inflating: tar3/nginx.rpm
inflating: tar3/sshd_config

# 解压后效果
[root@centos7 ~]# ls tar3/
anaconda-ks.cfg dir1 init.sh nginx.rpm sshd_config
[root@centos7 ~]#

1.6 rar

Liunx 上很少遇到 rar, 不过既然总结到这份上了,也加上了。

rar 是闭源软件,需要去官网下载

参数不能加- ,加上会报错

安装

1
2
3
4
5
# wget https://www.rarlab.com/rar/rarlinux-x64-712.tar.gz
wget https://www.rarlab.com/rar/rarlinux-x64-612.tar.gz
tar zxvpf rarlinux-x64-612.tar.gz
cd rar
make

centos7.9有点旧,新版本712 不能用,也不想升级各种库文件,容易出问题,所以使用旧版本替换的。

1
2
3
[root@centos7 ~]# rar
rar: /lib64/libstdc++.so.6: version `GLIBCXX_3.4.20' not found (required by rar)
rar: /lib64/libstdc++.so.6: version `GLIBCXX_3.4.21' not found (required by rar)

压缩

1
rar a 文件名 文件1 文件2 ...
  • a 代表压缩,会自动加 rar 后缀。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 压缩前效果
[root@centos7 ~]# ls
anaconda-ks.cfg dir1 init.sh nginx.rpm sshd_config

# 压缩
[root@centos7 ~]# rar a demo anaconda-ks.cfg dir1/ init.sh nginx.rpm sshd_config

RAR 6.12 Copyright (c) 1993-2022 Alexander Roshal 4 May 2022
Trial version Type 'rar -?' for help

Evaluation copy. Please register.

Creating archive demo.rar

Adding anaconda-ks.cfg OK
Adding dir1/111 OK
Adding init.sh OK
Adding nginx.rpm OK
Adding sshd_config OK
Done

# 压缩后效果
[root@centos7 ~]# ls
anaconda-ks.cfg demo.rar dir1 init.sh nginx.rpm sshd_config
[root@centos7 ~]#

解压

  1. 保留文件夹解压。常用。同时rarunrar 都可以解压。
1
2
unrar x 文件.rar -d 目标文件夹
rar x 文件.rar -d 目标文件夹
  • x 表示 extract with full paths,会按照压缩包内原来的目录结构来解压。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 创建目标文件夹
[root@centos7 ~]# mkdir tar6
[root@centos7 ~]# ls
anaconda-ks.cfg demo.rar dir1 init.sh nginx.rpm sshd_config tar6

# 解压
[root@centos7 ~]# unrar x demo.rar -d tar6/

UNRAR 6.12 freeware Copyright (c) 1993-2022 Alexander Roshal


Extracting from demo.rar

Extracting tar6/anaconda-ks.cfg OK
Creating tar6/dir1 OK
Extracting tar6/dir1/111 OK
Extracting tar6/init.sh OK
Extracting tar6/nginx.rpm OK
Extracting tar6/sshd_config OK
All OK

# 解压后效果
[root@centos7 ~]# ls tar6/
anaconda-ks.cfg dir1 init.sh nginx.rpm sshd_config
[root@centos7 ~]#
  1. 不保留文件夹,把文件都放到目标文件夹。
1
2
unrar e 文件名 -d 目标文件夹
rar e 文件名 -d 目标文件夹
  • e 表示 extract files without paths,即解压文件时不保留原来的目录结构
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 创建目标文件夹
[root@centos7 ~]# mkdir tar5
[root@centos7 ~]# ls
anaconda-ks.cfg demo.rar dir1 init.sh nginx.rpm sshd_config tar5

# 解压
[root@centos7 ~]# unrar e demo.rar -d tar5/

UNRAR 6.12 freeware Copyright (c) 1993-2022 Alexander Roshal


Extracting from demo.rar

Extracting tar5/anaconda-ks.cfg OK
Extracting tar5/111 OK
Extracting tar5/init.sh OK
Extracting tar5/nginx.rpm OK
Extracting tar5/sshd_config OK
All OK

# 解压后效果
[root@centos7 ~]# ls tar5/
111 anaconda-ks.cfg init.sh nginx.rpm sshd_config
[root@centos7 ~]#

查看包内的文件列表

1
2
rar l 文件.rar
unrar l 文件.rar
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
[root@centos7 ~]# rar l demo.rar 

RAR 6.12 Copyright (c) 1993-2022 Alexander Roshal 4 May 2022
Trial version Type 'rar -?' for help

Archive: demo.rar
Details: RAR 5

Attributes Size Date Time Name
----------- --------- ---------- ----- ----
-rw-r--r-- 543132 2021-01-18 21:09 nginx.rpm
-rw-r--r-- 575 2025-10-05 13:38 init.sh
----------- --------- ---------- ----- ----
543707 2

[root@centos7 ~]# unrar l demo.rar

UNRAR 6.12 freeware Copyright (c) 1993-2022 Alexander Roshal

Archive: demo.rar
Details: RAR 5

Attributes Size Date Time Name
----------- --------- ---------- ----- ----
-rw-r--r-- 543132 2021-01-18 21:09 nginx.rpm
-rw-r--r-- 575 2025-10-05 13:38 init.sh
----------- --------- ---------- ----- ----
543707 2

[root@centos7 ~]#

1.7 扩展tar

  1. 不解压查看包的文件列表
1
2
3
4
tar tf 文件的.tar
tar tzf 文件的.tar.gz
tar tjf 文件的.tar.bz2
tar tJf 文件的.tar.xz
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 准备好压缩包
tar cvf demo.tar nginx.rpm
tar zcvf demo.tar.gz nginx.rpm
tar jcvf demo.tar.bz2 nginx.rpm
tar Jcvf demo.tar.xz nginx.rpm

# 不解压查看内容
[root@centos7 ~]# tar tf demo.tar
nginx.rpm
[root@centos7 ~]# tar ztf demo.tar.gz
nginx.rpm
[root@centos7 ~]# tar jtf demo.tar.bz2
nginx.rpm
[root@centos7 ~]# tar Jtf demo.tar.xz
nginx.rpm
[root@centos7 ~]#
  1. 追加文件到 tar 包,tar里可以存在同名文件。
1
tar rf 文件.tar 追加的文件
1
2
3
4
[root@centos7 ~]# tar rf demo.tar anaconda-ks.cfg 
[root@centos7 ~]# tar tf demo.tar
nginx.rpm
anaconda-ks.cfg

只能追加文件到未压缩的 tar 包,而不能追加到 .tar.gz.tar.bz2.tar.xz。追加失败的效果

1
2
3
4
[root@centos7 ~]# tar zrf demo.tar.gz init.sh 
tar: Cannot update compressed archives
Try `tar --help' or `tar --usage' for more information.
[root@centos7 ~]#
  1. 替换文件
1
tar uf 文件.tar 替换的文件

如果文件的时间戳更新(数字更大),则会追加到后边;如果没有变化,则不追加。

  • 替换文件,时间戳未更新,所以没追加
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 查看包的内容
[root@centos7 ~]# tar tf demo.tar
nginx.rpm

# 替换init.sh
[root@centos7 ~]# tar uf demo.tar init.sh
[root@centos7 ~]# tar tf demo.tar
nginx.rpm
init.sh

# 二次替换,发现没效果
[root@centos7 ~]# tar uf demo.tar init.sh
[root@centos7 ~]# tar tf demo.tar
nginx.rpm
init.sh
[root@centos7 ~]#
  • 替换文件,更新时间戳,发现有同名文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 查看初始文件列表
[root@centos7 ~]# tar tf demo.tar
nginx.rpm

# 替换后追加,并查看效果
[root@centos7 ~]# tar uf demo.tar init.sh
[root@centos7 ~]# tar tf demo.tar
nginx.rpm
init.sh

# 刷新时间戳,二次追加、查看效果,发现存在同名文件。
[root@centos7 ~]# touch init.sh
[root@centos7 ~]# tar uf demo.tar init.sh
[root@centos7 ~]# tar tf demo.tar
nginx.rpm
init.sh
init.sh
[root@centos7 ~]#
  1. 删除包的部分文件
1
tar --delete -f 文件.tar 待删除的文件

f 参数不在第一个位置时需要加上 -

1
2
3
4
5
6
7
8
9
10
11
12
13
# 创建环境,打包两个文件
[root@centos7 ~]# tar cvf demo.tar nginx.rpm init.sh
nginx.rpm
init.sh
[root@centos7 ~]# tar tf demo.tar
nginx.rpm
init.sh

# 删除init.sh 文件,并查看新包的文件列表
[root@centos7 ~]# tar --delete -f demo.tar init.sh
[root@centos7 ~]# tar tf demo.tar
nginx.rpm
[root@centos7 ~]#
  1. 自动根据后缀压缩
1
2
3
4
tar acf 文件.tar 文件1 文件2 ...
tar acf 文件.tar.gz 文件1 文件2 ...
tar acf 文件.tar.bz2 文件1 文件2 ...
tar acf 文件.tar.xz 文件1 文件2 ...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 根据后缀自动选择
[root@centos7 ~]# ls
anaconda-ks.cfg dir1 init.sh nginx.rpm sshd_config
[root@centos7 ~]# tar acf demo.tar nginx.rpm
[root@centos7 ~]# tar acf demo.tar.gz nginx.rpm
[root@centos7 ~]# tar acf demo.tar.bz2 nginx.rpm
[root@centos7 ~]# tar acf demo.tar.xz nginx.rpm

# 查看效果
[root@centos7 ~]# file demo.tar*
demo.tar: POSIX tar archive (GNU)
demo.tar.bz2: bzip2 compressed data, block size = 900k
demo.tar.gz: gzip compressed data, from Unix, last modified: Sun Oct 5 13:57:48 2025
demo.tar.xz: XZ compressed data
[root@centos7 ~]#
  1. 自动根据后缀解压
1
2
3
4
tar xf 文件名.tar -C 目标文件夹
tar xf 文件名.tar.gz -C 目标文件夹
tar xf 文件名.tar.bz2 -C 目标文件夹
tar xf 文件名.tar.xz -C 目标文件夹
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
[root@centos7 ~]# mkdir demo{1..4}

# 自动根据后缀解压
[root@centos7 ~]# tar xf demo.tar -C demo1
[root@centos7 ~]# tar xf demo.tar.gz -C demo2
[root@centos7 ~]# tar xf demo.tar.bz2 -C demo3
[root@centos7 ~]# tar xf demo.tar.xz -C demo4

# 查看解压后内容
[root@centos7 ~]# ls -l demo{1..4}
demo1:
total 532
-rw-r--r-- 1 root root 543132 Jan 18 2021 nginx.rpm

demo2:
total 532
-rw-r--r-- 1 root root 543132 Jan 18 2021 nginx.rpm

demo3:
total 532
-rw-r--r-- 1 root root 543132 Jan 18 2021 nginx.rpm

demo4:
total 532
-rw-r--r-- 1 root root 543132 Jan 18 2021 nginx.rpm
[root@centos7 ~]#

1.8 扩展zip(加密、解密)

1
zip -r -e 文件.zip 文件1 文件2
  • -r 参数用于递归文件
  • -e 参数用于加密
1
2
3
4
5
[root@centos7 ~]# zip -e demo.zip nginx.rpm 
Enter password:
Verify password:
adding: nginx.rpm (deflated 3%)
[root@centos7 ~]#

1.9 7z

感觉不会遇到,不过想要彻底整理,就加上最基本的用法吧。

安装

  • redhat 系列
1
sudo yum install p7zip p7zip-plugins
  • debian 系列
1
2
sudo apt update
sudo apt install p7zip-full

压缩

1
7z a 文件.7z 文件1 文件2 ...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# 查看初始环境
[root@centos7 ~]# ls
anaconda-ks.cfg dir1 init.sh nginx.rpm sshd_config

# 压缩
[root@centos7 ~]# 7z a demo.7z nginx.rpm sshd_config

7-Zip [64] 16.02 : Copyright (c) 1999-2016 Igor Pavlov : 2016-05-21
p7zip Version 16.02 (locale=en_US.UTF-8,Utf16=on,HugeFiles=on,64 bits,2 CPUs Intel(R) Core(TM) i5-10200H CPU @ 2.40GHz (A0652),ASM,AES-NI)

Scanning the drive:
2 files, 546603 bytes (534 KiB)

Creating archive: demo.7z

Items to compress: 2


Files read from disk: 2
Archive size: 529545 bytes (518 KiB)
Everything is Ok

# 查看压缩内容
[root@centos7 ~]# ls -l demo.7z
-rw-r--r-- 1 root root 529545 Oct 5 14:20 demo.7z
[root@centos7 ~]#
  • 压缩为zip
1
7z a -tzip 文件.zip 文件1 文件2 ...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
[root@centos7 ~]# ls
anaconda-ks.cfg dir1 init.sh nginx.rpm sshd_config

# 压缩成zip
[root@centos7 ~]# 7z a -tzip demo.zip nginx.rpm init.sh

7-Zip [64] 16.02 : Copyright (c) 1999-2016 Igor Pavlov : 2016-05-21
p7zip Version 16.02 (locale=en_US.UTF-8,Utf16=on,HugeFiles=on,64 bits,2 CPUs Intel(R) Core(TM) i5-10200H CPU @ 2.40GHz (A0652),ASM,AES-NI)

Scanning the drive:
2 files, 543707 bytes (531 KiB)

Creating archive: demo.zip

Items to compress: 2


Files read from disk: 2
Archive size: 529283 bytes (517 KiB)
Everything is Ok

# 查看压缩后内容
[root@centos7 ~]# ls
anaconda-ks.cfg demo.zip dir1 init.sh nginx.rpm sshd_config
[root@centos7 ~]#

解压

注意-o 参数、目标文件夹 中间没有空格。

1
7z x 文件.7z -o目标文件夹
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# 查看初始环境
[root@centos7 ~]# ls
anaconda-ks.cfg demo.7z dir1 init.sh nginx.rpm sshd_config
[root@centos7 ~]# mkdir tar

# 解压到指定文件夹
[root@centos7 ~]# 7z x demo.7z -otar/

7-Zip [64] 16.02 : Copyright (c) 1999-2016 Igor Pavlov : 2016-05-21
p7zip Version 16.02 (locale=en_US.UTF-8,Utf16=on,HugeFiles=on,64 bits,2 CPUs Intel(R) Core(TM) i5-10200H CPU @ 2.40GHz (A0652),ASM,AES-NI)

Scanning the drive for archives:
1 file, 528078 bytes (516 KiB)

Extracting archive: demo.7z
--
Path = demo.7z
Type = 7z
Physical Size = 528078
Headers Size = 180
Method = LZMA2:768k
Solid = +
Blocks = 1

Everything is Ok

Files: 2
Size: 543707
Compressed: 528078

# 查看解压后内容
[root@centos7 ~]# ls tar/
init.sh nginx.rpm
[root@centos7 ~]#

不解压查看包的文件列表

1
7z l 文件.7z
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
[root@centos7 ~]# 7z l demo.7z 

7-Zip [64] 16.02 : Copyright (c) 1999-2016 Igor Pavlov : 2016-05-21
p7zip Version 16.02 (locale=en_US.UTF-8,Utf16=on,HugeFiles=on,64 bits,2 CPUs Intel(R) Core(TM) i5-10200H CPU @ 2.40GHz (A0652),ASM,AES-NI)

Scanning the drive for archives:
1 file, 528078 bytes (516 KiB)

Listing archive: demo.7z

--
Path = demo.7z
Type = 7z
Physical Size = 528078
Headers Size = 180
Method = LZMA2:768k
Solid = +
Blocks = 1

Date Time Attr Size Compressed Name
------------------- ----- ------------ ------------ ------------------------
2025-10-05 13:38:01 ....A 575 527898 init.sh
2021-01-18 21:09:20 ....A 543132 nginx.rpm
------------------- ----- ------------ ------------ ------------------------
2025-10-05 13:38:01 543707 527898 2 files
[root@centos7 ~]#

加密

给压缩包加上密码

1
2
7z a -p123456 文件.7z 文件1 文件2
7z a -p 文件.7z 文件1 文件2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
[root@centos7 ~]# ls
anaconda-ks.cfg dir1 init.sh nginx.rpm sshd_config tar

# 参数带密码,法一
[root@centos7 ~]# 7z a -p123456 file1.7z nginx.rpm anaconda-ks.cfg

7-Zip [64] 16.02 : Copyright (c) 1999-2016 Igor Pavlov : 2016-05-21
p7zip Version 16.02 (locale=en_US.UTF-8,Utf16=on,HugeFiles=on,64 bits,2 CPUs Intel(R) Core(TM) i5-10200H CPU @ 2.40GHz (A0652),ASM,AES-NI)

Scanning the drive:
2 files, 544624 bytes (532 KiB)

Creating archive: file1.7z

Items to compress: 2


Files read from disk: 2
Archive size: 528615 bytes (517 KiB)
Everything is Ok

# 手动设置密码,法二
[root@centos7 ~]# 7z a -p file2.7z nginx.rpm anaconda-ks.cfg

7-Zip [64] 16.02 : Copyright (c) 1999-2016 Igor Pavlov : 2016-05-21
p7zip Version 16.02 (locale=en_US.UTF-8,Utf16=on,HugeFiles=on,64 bits,2 CPUs Intel(R) Core(TM) i5-10200H CPU @ 2.40GHz (A0652),ASM,AES-NI)

Scanning the drive:
2 files, 544624 bytes (532 KiB)

Creating archive: file2.7z

Items to compress: 2


Enter password (will not be echoed):
Verify password (will not be echoed) :

Files read from disk: 2
Archive size: 528615 bytes (517 KiB)
Everything is Ok
[root@centos7 ~]#

二、文件查找

2.1 find

  1. 基础语法
1
find 要找的目录 查找选项1 查找参数1 查找选项2 查找参数2 查找选项3 查找参数3

2.1.1 通过文件名称查找

  1. 通过名字查找
1
find 要找的目录 -name 文件名称
1
2
3
4
5
6
[root@centos7 ~]# find / -name passwd
/etc/passwd
/etc/pam.d/passwd
/usr/bin/passwd
/usr/share/bash-completion/completions/passwd
[root@centos7 ~]#
  1. 忽略名字的大小写
1
find 要找的目录 -iname 文件名称
1
2
3
4
5
6
7
[root@centos7 ~]# touch fil{e,E}
[root@centos7 ~]# ls fil*
file filE
[root@centos7 ~]# find . -iname file
./file
./filE
[root@centos7 ~]#

2.1.2 通过文件大小查找

1
find 要找的目录 -size 文件大小
1
2
3
4
5
6
7
8
9
10
11
# 大于50M的文件,不包含50M
find / -size +50M

# 小于5M的文件,不包含5M
find / -size -5M

# 等于5M
find / -size 5M

# 可以拼接不同的条件,
find / \( -size 50M -o -size +50M \)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[root@centos7 ~]# find / \( -size 50M -o -size +50M \)
/boot/initramfs-0-rescue-fb975d108c4343c494c081818be2ebd7.img
/dev/1G.file
/proc/kcore
find: ‘/proc/11443/task/11443/fd/5’: No such file or directory
find: ‘/proc/11443/task/11443/fdinfo/5’: No such file or directory
find: ‘/proc/11443/fd/6’: No such file or directory
find: ‘/proc/11443/fdinfo/6’: No such file or directory
/sys/devices/pci0000:00/0000:00:0f.0/resource1_wc
/sys/devices/pci0000:00/0000:00:0f.0/resource1
/root/1G.file
/var/lib/rpm/Packages
/var/cache/yum/x86_64/7/epel/gen/filelists_db.sqlite
/var/cache/yum/x86_64/7/updates/gen/primary_db.sqlite
/var/cache/yum/x86_64/7/updates/gen/filelists_db.sqlite
/usr/lib/locale/locale-archive
[root@centos7 ~]#

2.1.3 通过文件类型查找

1
2
3
4
5
6
find 要找的目录 -type 文件类型

# 文件类型可选值
- f 普通文件
- d 目录
- l 链接
1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@centos7 ~]# mkdir passwd

# 查找任意类型的文件
[root@centos7 ~]# find / -name passwd
/etc/passwd
/etc/pam.d/passwd
/root/passwd
/usr/bin/passwd
/usr/share/bash-completion/completions/passwd

# 查找目录类型的文件
[root@centos7 ~]# find / -name passwd -type d
/root/passwd
[root@centos7 ~]#

2.1.4 通过文件所有者查找

1
find 要找的目录 -user 用户
1
2
3
4
5
6
7
8
9
10
11
12
# 查找qiankong用户的文件
[root@centos7 ~]# find / -user qiankong
find: ‘/proc/11661/task/11661/fd/5’: No such file or directory
find: ‘/proc/11661/task/11661/fdinfo/5’: No such file or directory
find: ‘/proc/11661/fd/6’: No such file or directory
find: ‘/proc/11661/fdinfo/6’: No such file or directory
/var/spool/mail/qiankong
/home/qiankong
/home/qiankong/.bash_logout
/home/qiankong/.bash_profile
/home/qiankong/.bashrc
[root@centos7 ~]#

2.1.5 通过文件的修改时间查找

1
find 要找的目录 -mtime 时间范围
1
2
3
4
5
# 查找小于3天的文件
find / -mtime -3

# 查找小于3分钟的文件
find / -mmin -3
1
2
3
4
5
6
7
8
9
10
11
12
13
# 查找3天内修改的文件
[root@centos7 ~]# find / -iname passwd -mtime -3
/etc/passwd
/root/passwd

# 查找20分钟内修改过的文件
[root@centos7 ~]# find . -mmin -20
.
./1G.file
./passwd
./file11
./123
[root@centos7 ~]#

2.1.6 通过文件权限查找

通过权限查找又分为三种

名称 解释
-perm 644 精确匹配 文件、文件夹权限必须正好是644
-perm -644 大于匹配 文件、文件夹权限必须大于等于644
-perm /644 或匹配 文件所有者>=6、所属组>=4、其他人>=4,这三个权限任意满足一个即可
  • 精确匹配
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[root@centos7 ~]# find / -perm 4755 -ls
find: ‘/proc/39926/task/39926/fd/5’: No such file or directory
find: ‘/proc/39926/task/39926/fdinfo/5’: No such file or directory
find: ‘/proc/39926/fd/6’: No such file or directory
find: ‘/proc/39926/fdinfo/6’: No such file or directory
100693392 32 -rwsr-xr-x 1 root root 32096 Oct 31 2018 /usr/bin/fusermount
100831774 76 -rwsr-xr-x 1 root root 73888 Aug 9 2019 /usr/bin/chage
100831775 80 -rwsr-xr-x 1 root root 78408 Aug 9 2019 /usr/bin/gpasswd
100831810 44 -rwsr-xr-x 1 root root 41936 Aug 9 2019 /usr/bin/newgrp
100878966 32 -rwsr-xr-x 1 root root 32128 Oct 1 2020 /usr/bin/su
100878951 44 -rwsr-xr-x 1 root root 44264 Oct 1 2020 /usr/bin/mount
100878970 32 -rwsr-xr-x 1 root root 31984 Oct 1 2020 /usr/bin/umount
101133689 24 -rwsr-xr-x 1 root root 23576 Apr 1 2020 /usr/bin/pkexec
101178499 60 -rwsr-xr-x 1 root root 57656 Aug 9 2019 /usr/bin/crontab
101300037 28 -rwsr-xr-x 1 root root 27856 Apr 1 2020 /usr/bin/passwd
212859 36 -rwsr-xr-x 1 root root 36272 Apr 1 2020 /usr/sbin/unix_chkpwd
212857 12 -rwsr-xr-x 1 root root 11232 Apr 1 2020 /usr/sbin/pam_timestamp_check
288571 12 -rwsr-xr-x 1 root root 11296 Oct 13 2020 /usr/sbin/usernetctl
288838 116 -rwsr-xr-x 1 root root 117432 Oct 14 2021 /usr/sbin/mount.nfs
67582495 16 -rwsr-xr-x 1 root root 15432 Apr 1 2020 /usr/lib/polkit-1/polkit-agent-helper-1
[root@centos7 ~]#
  • 大于匹配
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[root@centos7 ~]# find / -perm -4444 -ls
find: ‘/proc/40055/task/40055/fd/5’: No such file or directory
find: ‘/proc/40055/task/40055/fdinfo/5’: No such file or directory
find: ‘/proc/40055/fd/6’: No such file or directory
find: ‘/proc/40055/fdinfo/6’: No such file or directory
100693392 32 -rwsr-xr-x 1 root root 32096 Oct 31 2018 /usr/bin/fusermount
100831774 76 -rwsr-xr-x 1 root root 73888 Aug 9 2019 /usr/bin/chage
100831775 80 -rwsr-xr-x 1 root root 78408 Aug 9 2019 /usr/bin/gpasswd
100831810 44 -rwsr-xr-x 1 root root 41936 Aug 9 2019 /usr/bin/newgrp
100878966 32 -rwsr-xr-x 1 root root 32128 Oct 1 2020 /usr/bin/su
100878951 44 -rwsr-xr-x 1 root root 44264 Oct 1 2020 /usr/bin/mount
100878970 32 -rwsr-xr-x 1 root root 31984 Oct 1 2020 /usr/bin/umount
101133689 24 -rwsr-xr-x 1 root root 23576 Apr 1 2020 /usr/bin/pkexec
101178499 60 -rwsr-xr-x 1 root root 57656 Aug 9 2019 /usr/bin/crontab
101300037 28 -rwsr-xr-x 1 root root 27856 Apr 1 2020 /usr/bin/passwd
212859 36 -rwsr-xr-x 1 root root 36272 Apr 1 2020 /usr/sbin/unix_chkpwd
212857 12 -rwsr-xr-x 1 root root 11232 Apr 1 2020 /usr/sbin/pam_timestamp_check
288571 12 -rwsr-xr-x 1 root root 11296 Oct 13 2020 /usr/sbin/usernetctl
288838 116 -rwsr-xr-x 1 root root 117432 Oct 14 2021 /usr/sbin/mount.nfs
67582495 16 -rwsr-xr-x 1 root root 15432 Apr 1 2020 /usr/lib/polkit-1/polkit-agent-helper-1
[root@centos7 ~]#
  • 或匹配
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[root@centos7 ~]# find / -perm /2000 -ls
find: ‘/proc/40148/task/40148/fd/5’: No such file or directory
find: ‘/proc/40148/task/40148/fdinfo/5’: No such file or directory
find: ‘/proc/40148/fd/6’: No such file or directory
find: ‘/proc/40148/fdinfo/6’: No such file or directory
8060 0 drwxr-sr-x 3 root systemd-journal 60 Oct 2 17:36 /run/log/journal
8061 0 drwxr-s--- 2 root systemd-journal 60 Oct 2 17:36 /run/log/journal/fb975d108c4343c494c081818be2ebd7
101633618 0 drwxr-sr-x 2 root OPS 84 Oct 6 17:39 /root/dir1
100693303 16 -r-xr-sr-x 1 root tty 15344 Jun 10 2014 /usr/bin/wall
100878976 20 -rwxr-sr-x 1 root tty 19544 Oct 1 2020 /usr/bin/write
101298631 376 ---x--s--x 1 root nobody 382216 Aug 9 2019 /usr/bin/ssh-agent
101258760 40 -rwx--s--x 1 root slocate 40520 Apr 11 2018 /usr/bin/locate
288566 12 -rwxr-sr-x 1 root root 11224 Oct 13 2020 /usr/sbin/netreport
387916 216 -rwxr-sr-x 1 root postdrop 218560 Apr 1 2020 /usr/sbin/postdrop
387923 260 -rwxr-sr-x 1 root postdrop 264128 Apr 1 2020 /usr/sbin/postqueue
33973020 12 -rwx--s--x 1 root utmp 11192 Jun 10 2014 /usr/libexec/utempter/utempter
34121133 456 ---x--s--x 1 root ssh_keys 465760 Aug 9 2019 /usr/libexec/openssh/ssh-keysign
[root@centos7 ~]#

2.2 locate

基于数据库的查找,更快,但是需要提前更新数据库,不然最新的文件查不到。

安装

  • redhat 系列
1
yum install mlocate -y
  • debian 系列
1

查找

1
locate 文件名
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[root@centos7 ~]# locate useradd
/etc/default/useradd
/usr/sbin/luseradd
/usr/sbin/useradd
/usr/share/bash-completion/completions/useradd
/usr/share/man/de/man8/useradd.8.gz
/usr/share/man/fr/man8/useradd.8.gz
/usr/share/man/id/man8/useradd.8.gz
/usr/share/man/it/man8/useradd.8.gz
/usr/share/man/ja/man8/useradd.8.gz
/usr/share/man/man1/luseradd.1.gz
/usr/share/man/man8/useradd.8.gz
/usr/share/man/ru/man8/useradd.8.gz
/usr/share/man/tr/man8/useradd.8.gz
/usr/share/man/zh_CN/man8/useradd.8.gz
/usr/share/man/zh_TW/man8/useradd.8.gz
[root@centos7 ~]#

更新数据库

1
updatedb

2.3 扩展find

find找到后的动作

2.3.1 找到后的动作-ls

1
find 要找的目录 查找条件 查找参数 -ls
1
2
3
4
5
6
7
8
9
[root@centos7 ~]# find . -mmin -60 -ls
67146817 4 dr-xr-x--- 6 root root 4096 Oct 5 15:20 .
67195137 0 -rw-r--r-- 1 root root 0 Oct 5 15:00 ./file
67195138 0 -rw-r--r-- 1 root root 0 Oct 5 15:00 ./filE
67195139 1048576 -rw-r--r-- 1 root root 1073741824 Oct 5 15:06 ./1G.file
101258747 0 drwxr-xr-x 2 root root 6 Oct 5 15:21 ./passwd
67195140 0 -rw-r--r-- 1 root root 0 Oct 5 15:18 ./file11
67195143 0 -rw-r--r-- 1 root root 0 Oct 5 15:20 ./123
[root@centos7 ~]#

2.3.2 找到后的动作-exec

1
2
find 要找的目录 查找条件 查找参数 -exec 自定义命令 {} \;
find 要找的目录 查找条件 查找参数 -exec 自定义命令 {} +
  • {} 代表找到的文件
  • \; 固定写法,会将文件依次提交给自定义的命令
  • + 会将文件一次性提交给自定义的命令
1
2
3
4
5
6
[root@centos7 ~]# find / -name passwd -exec ls -lh {} \;
-rw-r--r-- 1 root root 1.5K Oct 6 17:46 /etc/passwd
-rw-r--r--. 1 root root 188 Apr 1 2020 /etc/pam.d/passwd
-rwsr-xr-x. 1 root root 28K Apr 1 2020 /usr/bin/passwd
-rw-r--r--. 1 root root 514 Apr 1 2020 /usr/share/bash-completion/completions/passwd
[root@centos7 ~]#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# \; 方式传递参数
[root@centos7 ~]# touch file{1..5000}
[root@centos7 ~]# time find . -name "file*" -exec rm -rf {} \;

real 0m2.731s
user 0m2.021s
sys 0m0.787s
[root@centos7 ~]#

# + 方式传递参数
[root@centos7 ~]# touch file{1..5000}
[root@centos7 ~]# time find . -name "file*" -exec rm -rf {} +

real 0m0.056s
user 0m0.003s
sys 0m0.053s
[root@centos7 ~]#

2.3.3 使用管道传递|-xargs

可以配合管道传递给其他命令,但是有些命令不支持管道输入,例如 lsrm cp 等,此时可以使用xargs 来转接一下。

错误示范

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# ls 无法接受管道输入
[root@centos7 ~]# touch file{1..10}
[root@centos7 ~]# find . -name "file*" -type f | ls -l
total 544
-rw-rw-r-- 1 root qiankong 1492 Sep 28 16:07 anaconda-ks.cfg
-rw-r--r-- 1 root root 0 Oct 6 21:40 file1
-rw-r--r-- 1 root root 0 Oct 6 21:40 file10
-rw-r--r-- 1 root root 0 Oct 6 21:40 file2
-rw-r--r-- 1 root root 0 Oct 6 21:40 file3
-rw-r--r-- 1 root root 0 Oct 6 21:40 file4
-rw-r--r-- 1 root root 0 Oct 6 21:40 file5
-rw-r--r-- 1 root root 0 Oct 6 21:40 file6
-rw-r--r-- 1 root root 0 Oct 6 21:40 file7
-rw-r--r-- 1 root root 0 Oct 6 21:40 file8
-rw-r--r-- 1 root root 0 Oct 6 21:40 file9
-rw-r--r-- 1 root root 575 Oct 5 13:38 init.sh
-rw-r--r-- 1 root root 543132 Jan 18 2021 nginx.rpm
-rw------- 1 root root 3471 Oct 4 22:59 sshd_config
[root@centos7 ~]#

# rm 无法接受管道输入
[root@centos7 ~]# find . -name "file*" -type f | rm -rf
[root@centos7 ~]# ls
anaconda-ks.cfg file1 file10 file2 file3 file4 file5 file6 file7 file8 file9 init.sh nginx.rpm sshd_config
[root@centos7 ~]#

xargs转接

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[root@centos7 ~]# touch file{1..10}
[root@centos7 ~]#

# xargs传递给ls
[root@centos7 ~]# find . -name "file*" | xargs ls -lh
-rw-r--r-- 1 root root 0 Oct 6 21:42 ./file1
-rw-r--r-- 1 root root 0 Oct 6 21:42 ./file10
-rw-r--r-- 1 root root 0 Oct 6 21:42 ./file2
-rw-r--r-- 1 root root 0 Oct 6 21:42 ./file3
-rw-r--r-- 1 root root 0 Oct 6 21:42 ./file4
-rw-r--r-- 1 root root 0 Oct 6 21:42 ./file5
-rw-r--r-- 1 root root 0 Oct 6 21:42 ./file6
-rw-r--r-- 1 root root 0 Oct 6 21:42 ./file7
-rw-r--r-- 1 root root 0 Oct 6 21:42 ./file8
-rw-r--r-- 1 root root 0 Oct 6 21:42 ./file9
[root@centos7 ~]#

# xargs传递给rm
[root@centos7 ~]# find . -name "file*" | xargs rm -rf
[root@centos7 ~]#
[root@centos7 ~]# ls
anaconda-ks.cfg init.sh nginx.rpm sshd_config
[root@centos7 ~]#

有些命令有多个输入,此时xargs 如何判断给到哪个参数?

  • I 大写 i, 后接一个标识符
1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@centos7 ~]# mkdir dir1
[root@centos7 ~]# touch file{1..10}
[root@centos7 ~]#

# 发现参数传递给了第二个位置
[root@centos7 ~]# find . -name "file*" |xargs cp dir1
cp: target ‘./file10’ is not a directory
[root@centos7 ~]#

# 标识一个 ID,可以是任意字符,用来代表管道的输入
[root@centos7 ~]# find . -name "file*" |xargs -I ID cp ID dir1
[root@centos7 ~]# ls dir1/
file1 file10 file2 file3 file4 file5 file6 file7 file8 file9
[root@centos7 ~]#

特殊名称的文件

  • -0 数字零,有些文件名称比较特殊,可以使用-0 来正确处理

典型代表 file 2 \n.txt,没有-0 参数就不会成功移动。find 也需要 print0

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# 不带-0参数,转移失败
[root@centos7 ~]# touch "file 2 \n .txt"
[root@centos7 ~]# ls . dir1/
.:
anaconda-ks.cfg dir1 file 2 \n .txt init.sh nginx.rpm sshd_config

dir1/:
[root@centos7 ~]# find . -name "file*" -print0 | xargs -I haha mv haha dir1/
xargs: WARNING: a NUL character occurred in the input. It cannot be passed through in the argument list. Did you mean to use the --null option?
mv: cannot stat ‘./file 2 n .txt’: No such file or directory
[root@centos7 ~]# ls . dir1/
.:
anaconda-ks.cfg dir1 file 2 \n .txt init.sh nginx.rpm sshd_config

dir1/:
[root@centos7 ~]#



# 带-0参数,转移成功
[root@centos7 ~]# find . -name "file*" -print0 | xargs -0 -I haha mv haha dir1/
[root@centos7 ~]# ls . dir1/
.:
anaconda-ks.cfg dir1 init.sh nginx.rpm sshd_config

dir1/:
file 2 \n .txt
[root@centos7 ~]#

三、查看文件、命令的类型

3.1 type

1
type 命令
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 是一个二进制文件
[root@centos7 ~]# type passwd
passwd is /usr/bin/passwd

# 是shell内置的命令
[root@centos7 ~]# type cd
cd is a shell builtin

# 是别名
[root@centos7 ~]# type ll
ll is aliased to `ls -l --color=auto'

# 是关键字
[root@centos7 ~]# type if
if is a shell keyword
[root@centos7 ~]#

3.2 file

1
file 文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# 文本文件
[root@centos7 ~]# file /etc/passwd
/etc/passwd: ASCII text
[root@centos7 ~]#

# 二进制文件
[root@centos7 ~]# file /usr/bin/passwd
/usr/bin/passwd: setuid ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=dee1b9ab6618c6bfb84a14f85ba258c742cf4aec, stripped
[root@centos7 ~]#

# 各种压缩文件
[root@centos7 ~]# file demo*
demo.7z: 7-zip archive data, version 0.4
demo.rar: RAR archive data, v39,
demo.tar: POSIX tar archive (GNU)
demo.tar.bz2: bzip2 compressed data, block size = 900k
demo.tar.gz: gzip compressed data, from Unix, last modified: Sun Oct 5 15:42:37 2025
demo.tar.xz: XZ compressed data
[root@centos7 ~]#

# rpm安装包
[root@centos7 ~]# file nginx.rpm
nginx.rpm: RPM v3.0 bin i386/x86_64 nginx-1:1.12.2-2.el7
[root@centos7 ~]#

# 目录
[root@centos7 ~]# file /
/: directory
[root@centos7 ~]#

# 链接文件
[root@centos7 ~]# file /etc/sysconfig/selinux
/etc/sysconfig/selinux: symbolic link to `../selinux/config'
[root@centos7 ~]#

# 块设备文件
[root@centos7 ~]# file /dev/sda
/dev/sda: block special
[root@centos7 ~]#

# 字符设备文件
[root@centos7 ~]# file /dev/zero
/dev/zero: character special
[root@centos7 ~]# file /dev/pts/0
/dev/pts/0: character special

# 图像文件
[root@centos7 ~]# file 1.png
1.png: PNG image data, 1536 x 1024, 8-bit/color RGB, non-interlaced
[root@centos7 ~]#

# 图标文件
[root@centos7 ~]# file favicon.ico
favicon.ico: MS Windows icon resource - 1 icon
[root@centos7 ~]#

# 视频文件,mp4
[root@centos7 ~]# file fuclaude.mp4
fuclaude.mp4: ISO Media, MPEG v4 system, version 1
[root@centos7 ~]#

四、文件时间

文件默认有好几种时间,一般说的时间是指修改时间

  • 修改时间 mtime

  • 访问时间 atime

  • 状态改变时间 ctime,例如状态、所有者等的改变

  • 创建时间 crtime,(不是所有文件系统都有)

4.1 查看文件的时间

1
stat 文件名称 
1
2
3
4
5
6
7
8
9
10
[root@centos7 ~]# stat 1G.file 
File: ‘1G.file’
Size: 1073741824 Blocks: 2097152 IO Block: 4096 regular file
Device: fd00h/64768d Inode: 67195139 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2025-10-05 15:06:31.695198991 +0800
Modify: 2025-10-05 15:06:31.836198992 +0800
Change: 2025-10-05 15:06:31.836198992 +0800
Birth: -
[root@centos7 ~]#

4.2 修改文件的访问时间、修改时间

  1. 刷新为当前时间
1
touch 文件名
1
2
3
4
5
6
7
8
9
10
11
12
13
[root@centos7 ~]# touch 1G.file 
[root@centos7 ~]# stat 1G.file
File: ‘1G.file’
Size: 1073741824 Blocks: 2097152 IO Block: 4096 regular file
Device: fd00h/64768d Inode: 67195139 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2025-10-05 15:55:09.566228817 +0800
Modify: 2025-10-05 15:55:09.566228817 +0800
Change: 2025-10-05 15:55:09.566228817 +0800
Birth: -
[root@centos7 ~]# date
Sun Oct 5 15:55:14 CST 2025
[root@centos7 ~]#
  1. 修改为指定时间,(修改时间、访问时间)
1
touch -t 时间 文件名
1
2
3
4
5
6
7
8
9
10
11
12
13
[root@centos7 ~]# touch -t 202009181230 1G.file
[root@centos7 ~]# stat 1G.file
File: ‘1G.file’
Size: 1073741824 Blocks: 2097152 IO Block: 4096 regular file
Device: fd00h/64768d Inode: 67195139 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2020-09-18 12:30:00.000000000 +0800
Modify: 2020-09-18 12:30:00.000000000 +0800
Change: 2025-10-05 15:56:38.892229730 +0800
Birth: -
[root@centos7 ~]# date
Sun Oct 5 15:56:47 CST 2025
[root@centos7 ~]#
  1. 单独更新修改时间
1
touch -t 时间 -m 文件名
1
2
3
4
5
6
7
8
9
10
11
[root@centos7 ~]# touch -t 201001011200 -m 1G.file 
[root@centos7 ~]# stat 1G.file
File: ‘1G.file’
Size: 1073741824 Blocks: 2097152 IO Block: 4096 regular file
Device: fd00h/64768d Inode: 67195139 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2020-09-18 12:30:00.000000000 +0800
Modify: 2010-01-01 12:00:00.000000000 +0800
Change: 2025-10-05 15:58:37.743230945 +0800
Birth: -
[root@centos7 ~]#
  1. 单独更新访问时间
1
touch -t 时间 -a 文件名
1
2
3
4
5
6
7
8
9
10
11
12
[root@centos7 ~]# touch -t 200109091000 -a 1G.file 
[root@centos7 ~]# stat 1G.file
File: ‘1G.file’
Size: 1073741824 Blocks: 2097152 IO Block: 4096 regular file
Device: fd00h/64768d Inode: 67195139 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2001-09-09 10:00:00.000000000 +0800
Modify: 2010-01-01 12:00:00.000000000 +0800
Change: 2025-10-05 15:59:38.924231570 +0800
Birth: -
[root@centos7 ~]#

五、封面图

封面图