Linux文件管理(二)
编辑
5
2025-10-05

Linux文件管理(二)
上一次的文件管理(一)只能说是一些很基本的东西,还有一些内容也很常见,例如解压缩,文件查找、文件类型、甚至是文件权限等等。
一、文件解压缩
工具 | 后缀格式 | 解释 |
---|---|---|
tar | .tar | 只打包 |
gzip | .gz/.tar.gz/.tgz | 普通压缩 |
bzip2 | .bz2/.tar.bz2 | 压缩率更高,但是更慢 |
xz | .xz/.tar.xz | 压缩率最高,时间最慢 |
zip | .zip | 跨平台兼容性好 |
不同压缩方法都可以和tar
配合。
1.1 tar
Linux
下打包工具,可以将多个文件、文件夹打包成一个文件,便于文件的传输。
打包
tar -cvf .tar文件 待打包的文件1 待打包的文件2 ...
横杠 -
可省略
tar cvf .tar文件 待打包的文件1 待打包的文件2 ...
f
指定.tar
文件v
显示详细信息c
代表是打包
# 显示打包前的文件列表
[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 ~]#
解包
tar xvf .tar文件 -C 解包后放置的目录
或
tar -xvf .tar文件 -C 解包后放置的目录
x
代表这是解包C
指定解包后文件的放置目录。强烈推荐,否则要么很烂、要么会覆盖当前目录的文件。f
指定.tar
文件v
显示详细信息
# 创建目标文件夹
[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
体现。
压缩
tar zcvf 文件.tar.gz 文件1 文件2 ...
# 压缩前
[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 ~]#
解压
tar zxvf 文件.tar.gz -C 目标文件夹
# 创建目标文件夹
[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
安装
yum install -y bzip2
否则会报错
[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
压缩
tar jcvf 文件.tar.bz2 文件1 文件2 ...
# 压缩前效果
[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 ~]#
解压
# 解压前效果
[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
体现。
压缩
tar Jcvf 文件.tar.xz 文件1 文件2 ...
# 压缩前效果
[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 ~]#
解压
tar Jxvf 文件.tar.xz -C 目标文件夹
# 解压前效果
[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
最常见的压缩格式,跨平台兼容性好。需要安装 zip
,unzip
安装
yum install -y zip unzip
否则会显示
-bash: zip: command not found
-bash: unzip: command not found
压缩
zip 文件.zip 文件1 文件2 ...
[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 ~]#
解压
unzip 文件.zip -d 目标文件夹
# 创建目标文件夹
[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
是闭源软件,需要去官网下载
参数不能加-
,加上会报错
安装
# 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
不能用,也不想升级各种库文件,容易出问题,所以使用旧版本替换的。
[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)
压缩
rar a 文件名 文件1 文件2 ...
a
代表压缩,会自动加rar
后缀。
# 压缩前效果
[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 ~]#
解压
- 保留文件夹解压。常用。同时
rar
和unrar
都可以解压。
unrar x 文件.rar -d 目标文件夹
rar x 文件.rar -d 目标文件夹
x
表示 extract with full paths,会按照压缩包内原来的目录结构来解压。
# 创建目标文件夹
[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 ~]#
- 不保留文件夹,把文件都放到目标文件夹。
unrar e 文件名 -d 目标文件夹
rar e 文件名 -d 目标文件夹
e
表示 extract files without paths,即解压文件时不保留原来的目录结构。
# 创建目标文件夹
[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 ~]#
查看包内的文件列表
rar l 文件.rar
unrar l 文件.rar
[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
- 不解压查看包的文件列表
tar tf 文件的.tar
tar tzf 文件的.tar.gz
tar tjf 文件的.tar.bz2
tar tJf 文件的.tar.xz
# 准备好压缩包
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 ~]#
- 追加文件到
tar
包,tar里可以存在同名文件。
tar rf 文件.tar 追加的文件
[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
。追加失败的效果
[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 ~]#
- 替换文件
tar uf 文件.tar 替换的文件
如果文件的时间戳更新(数字更大),则会追加到后边;如果没有变化,则不追加。
- 替换文件,时间戳未更新,所以没追加
# 查看包的内容
[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 ~]#
- 替换文件,更新时间戳,发现有同名文件
# 查看初始文件列表
[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 ~]#
- 删除包的部分文件
tar --delete -f 文件.tar 待删除的文件
f
参数不在第一个位置时需要加上 -
# 创建环境,打包两个文件
[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 ~]#
- 自动根据后缀压缩
tar acf 文件.tar 文件1 文件2 ...
tar acf 文件.tar.gz 文件1 文件2 ...
tar acf 文件.tar.bz2 文件1 文件2 ...
tar acf 文件.tar.xz 文件1 文件2 ...
# 根据后缀自动选择
[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 ~]#
- 自动根据后缀解压
tar xf 文件名.tar -C 目标文件夹
tar xf 文件名.tar.gz -C 目标文件夹
tar xf 文件名.tar.bz2 -C 目标文件夹
tar xf 文件名.tar.xz -C 目标文件夹
[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(加密、解密)
zip -r -e 文件.zip 文件1 文件2
-r
参数用于递归文件-e
参数用于加密
[root@centos7 ~]# zip -e demo.zip nginx.rpm
Enter password:
Verify password:
adding: nginx.rpm (deflated 3%)
[root@centos7 ~]#
1.9 7z
感觉不会遇到,不过想要彻底整理,就加上最基本的用法吧。
安装
redhat
系列
sudo yum install p7zip p7zip-plugins
debian
系列
sudo apt update
sudo apt install p7zip-full
压缩
7z a 文件.7z 文件1 文件2 ...
# 查看初始环境
[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
7z a -tzip 文件.zip 文件1 文件2 ...
[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
参数、目标文件夹 中间没有空格。
7z x 文件.7z -o目标文件夹
# 查看初始环境
[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 ~]#
不解压查看包的文件列表
7z l 文件.7z
[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 ~]#
加密
给压缩包加上密码
7z a -p123456 文件.7z 文件1 文件2
7z a -p 文件.7z 文件1 文件2
[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
- 基础语法
find 要找的目录 查找选项1 查找参数1 查找选项2 查找参数2 查找选项3 查找参数3
2.1.1 通过文件名称查找
- 通过名字查找
find 要找的目录 -name 文件名称
[root@centos7 ~]# find / -name passwd
/etc/passwd
/etc/pam.d/passwd
/usr/bin/passwd
/usr/share/bash-completion/completions/passwd
[root@centos7 ~]#
- 忽略名字的大小写
find 要找的目录 -iname 文件名称
[root@centos7 ~]# touch fil{e,E}
[root@centos7 ~]# ls fil*
file filE
[root@centos7 ~]# find . -iname file
./file
./filE
[root@centos7 ~]#
2.1.2 通过文件大小查找
find 要找的目录 -size 文件大小
# 大于50M的文件,不包含50M
find / -size +50M
# 小于5M的文件,不包含5M
find / -size -5M
# 等于5M
find / -size 5M
# 可以拼接不同的条件,
find / \( -size 50M -o -size +50M \)
[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 通过文件类型查找
find 要找的目录 -type 文件类型
# 文件类型可选值
- f 普通文件
- d 目录
- l 链接
[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 通过文件所有者查找
find 要找的目录 -user 用户
# 查找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 通过文件的修改时间查找
find 要找的目录 -mtime 时间范围
# 查找小于3天的文件
find / -mtime -3
# 查找小于3分钟的文件
find / -mmin -3
# 查找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 找到后列出来
find 要找的目录 查找条件 查找参数 -ls
[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.2 locate
基于数据库的查找,更快,但是需要提前更新数据库,不然最新的文件查不到。
安装
redhat
系列
yum install mlocate -y
debian
系列
查找
locate 文件名
[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 ~]#
更新数据库
updatedb
三、查看文件、命令的类型
3.1 type
type 命令
# 是一个二进制文件
[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
file 文件
# 文本文件
[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 查看文件的时间
stat 文件名称
[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 修改文件的访问时间、修改时间
- 刷新为当前时间
touch 文件名
[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 ~]#
- 修改为指定时间,(修改时间、访问时间)
touch -t 时间 文件名
[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 ~]#
- 单独更新修改时间
touch -t 时间 -m 文件名
[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 ~]#
- 单独更新访问时间
touch -t 时间 -a 文件名
[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 ~]#
五、封面图
- 0
- 0
-
分享