文件管理
touch:无中生有,万象更新
#当一个文件不存在时,touch可以创建一个空文件
#当一个文件存在时,touch这个文件可以更新文件的时间
[root@zyq ~]# cd /opt/
[root@zyq opt]# touch zzz
[root@zyq opt]# ll
total 0
-rw-r--r--. 1 root root 0 Jun 28 11:23 zzz
[root@zyq opt]# touch zzz
[root@zyq opt]# ll
total 0
-rw-r--r--. 1 root root 0 Jun 28 11:24 zzz
[root@zyq opt]#
stat:查看文件的详细信息,包括文件名、时间戳
[root@zyq opt]# ls
zzz
[root@zyq opt]# stat zzz
File: zzz
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: fd00h/64768d Inode: 50701281 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:usr_t:s0
Access: 2023-06-28 11:24:28.259746622 +0800
Modify: 2023-06-28 11:24:28.259746622 +0800
Change: 2023-06-28 11:24:28.259746622 +0800
Birth: 2023-06-28 11:23:54.816445269 +0800
[root@zyq opt]#
rm:remove删除文件
例:
-r 删除目录时必须带上-r选项
[root@zyq ~]# rm -r /zzz/
rm: descend into directory '/zzz/'? yy^H
rm: descend into directory '/zzz/www'? y
rm: descend into directory '/zzz/www/hhh'? y
rm: remove directory '/zzz/www/hhh/1234'? y
rm: remove directory '/zzz/www/hhh'? y
rm: remove directory '/zzz/www'? y
rm: remove directory '/zzz/'? y
[root@zyq ~]# y
-f 删除文件或目录时,不再询问是否删除
# 注意事项:删除文件时一定不要带-r选项,以免误删
[root@zyq opt]# ls
zzz
[root@zyq opt]# rm -f zzz
[root@zyq opt]# ls
[root@zyq opt]#
cp:copy复制
例:
-a 打包复制,常用于备份
-r 递归复制,复制目录时必须使用此选项
[root@zyq ~]# ls
1 2 3 4 5 6 7 8 9 anaconda-ks.cfg
[root@zyq ~]# cp -r {1..3} /opt/
[root@zyq ~]# ls /opt/
1 2 3
[root@zyq ~]#
-p 复制时保留原权限
# 注意事项:一对一时,源文件和目标文件类型必须一致,对一时,最后一个参数必须是目录
mv:move移动
例:
剪切,把一个文件剪切到另一个目录中
[root@zyq ~]# ls
1 2 anaconda-ks.cfg
[root@zyq ~]# ls 1/
test
[root@zyq ~]# mv 1/test 2/
[root@zyq ~]# ls 2/
test
[root@zyq ~]#
重命名,在同一个目录中将文件名字改掉
[root@zyq ~]# ls
1 2 anaconda-ks.cfg
[root@zyq ~]# mv 1 zzz
[root@zyq ~]# ls
2 anaconda-ks.cfg zzz
[root@zyq ~]#
压缩、解压缩命令
压缩格式
gz、bz2、xz、zip、Z
gzip
gzip #压缩后的文件以.gz结尾
[root@zyq ~]# ls
2 5 6 7 8 9 anaconda-ks.cfg zzz
[root@zyq ~]# gzip 5
[root@zyq ~]# ls
2 5.gz 6 7 8 9 anaconda-ks.cfg zzz
[root@zyq ~]#
-d #解压缩,解压完成后会删除原文件
[root@zyq ~]# ls
2 5.gz 6 7 8 9 anaconda-ks.cfg zzz
[root@zyq ~]# gzip -d 5.gz
[root@zyq ~]# ls
2 5 6 7 8 9 anaconda-ks.cfg zzz
[root@zyq ~]#
-c #将结果输出至标准输出
gunzip # 解压
[root@zyq ~]# ls
2 5.gz 6 7 8 9 anaconda-ks.cfg zzz
[root@zyq ~]# gunzip 5.gz
[root@zyq ~]# ls
2 5 6 7 8 9 anaconda-ks.cfg zzz
[root@zyq ~]#
zcat #不解压的情况下查看文本文件的内容
[root@zyq ~]# echo '123456' > 5
[root@zyq ~]# gzip 5
[root@zyq ~]# ls
2 5.gz 6 7 8 9 anaconda-ks.cfg zzz
[root@zyq ~]# zcat 5.gz
123456
[root@zyq ~]#
bzip2
bzip2 #压缩后的文件以.bz2结尾
#bzip2是一款比gzip有着更大压缩比的压缩工具,使用格式近似
[root@zyq ~]# which bzip2 #系统中没有此命令
/usr/bin/which: no bzip2 in (/root/.local/bin:/root/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin)
#挂载后下载
[root@zyq ~]# mount /dev/cdrom /mnt/
mount: /mnt: /dev/sr0 already mounted on /mnt.
[root@zyq ~]# ls /mnt/BaseOS/Packages/bzip2-
bzip2-1.0.8-8.el9.x86_64.rpm bzip2-libs-1.0.8-8.el9.i686.rpm bzip2-libs-1.0.8-8.el9.x86_64.rpm
[root@zyq ~]# rpm -ivh /mnt/BaseOS/Packages/bzip2-1.0.8-8.el9.x86_64.rpm
warning: /mnt/BaseOS/Packages/bzip2-1.0.8-8.el9.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID fd431d51: NOKEY
Verifying... ################################# [100%]
Preparing... ################################# [100%]
Updating / installing...
1:bzip2-1.0.8-8.el9 ################################# [100%]
[root@zyq ~]#
-d #解压缩,解压完成后会删除原文件
-# ##用1-9代替,指定压缩比,默认为6
-k #keep,压缩时保留原文件
bunzip #解压
bzcat #不解压的情况下查看文本文件的内容
xz
xz #压缩后的文件以.xz结尾
#比bzip2有着更大压缩比的压缩工具,使用格式近似
-d #解压缩,解压完成后会删除原文件
unxz #解压
xzcat #不解压的情况下查看文本文件的内容
zip
zip #既归档又压缩的工具。z