DC's blog DC's blog
首页
  • 计算机基础
  • linux基础
  • mysql
  • git
  • 数据结构与算法
  • axure
  • english
  • docker
  • opp
  • oop
  • 网络并发编程
  • 不基础的py基础
  • 设计模式
  • html
  • css
  • javascript
  • jquery
  • UI
  • 第一次学vue
  • 第二次学vue
  • Django
  • drf
  • drf_re
  • 温故知新
  • flask
  • 前后端不分离

    • BBS
    • 订单系统
    • CRM
  • 前后端部分分离

    • pear-admin-flask
    • pear-admin-django
  • 前后端分离

    • 供应链系统
  • 理论基础
  • py数据分析包
  • 机器学习
  • 深度学习
  • 华中科大的网课
  • cursor
  • deepseek
  • 杂文
  • 罗老师语录
  • 关于我

    • me
  • 分类
  • 归档
GitHub (opens new window)

DC

愿我一生欢喜,不为世俗所及.
首页
  • 计算机基础
  • linux基础
  • mysql
  • git
  • 数据结构与算法
  • axure
  • english
  • docker
  • opp
  • oop
  • 网络并发编程
  • 不基础的py基础
  • 设计模式
  • html
  • css
  • javascript
  • jquery
  • UI
  • 第一次学vue
  • 第二次学vue
  • Django
  • drf
  • drf_re
  • 温故知新
  • flask
  • 前后端不分离

    • BBS
    • 订单系统
    • CRM
  • 前后端部分分离

    • pear-admin-flask
    • pear-admin-django
  • 前后端分离

    • 供应链系统
  • 理论基础
  • py数据分析包
  • 机器学习
  • 深度学习
  • 华中科大的网课
  • cursor
  • deepseek
  • 杂文
  • 罗老师语录
  • 关于我

    • me
  • 分类
  • 归档
GitHub (opens new window)
  • 计算机基础

  • linux基础

    • 准备工作
    • 初识shell与基础常用命令
    • 文件管理之基础命令
    • 文件管理之四大模式
    • 文件管理之命令补充
      • find
        • 按文件名
        • 按文件大小
        • 文件查找深度
        • 按时间查找
        • 按文件属主、属组
        • 按文件类型
        • inode号、文件权限
        • find结合xargs
      • 上传与下载
        • wget下载
        • curl测试
        • sz/rz
      • 输出/入与重定向
        • > >>输出重定向
        • < <<输入重定向
      • 字符处理命令
        • sort 排序
        • uniq 去重
        • cut 分割
        • tr 替换删除
        • wc 统计
      • tar/zip
        • 打包压缩文件
        • 解压缩
    • 文件管理之文件系统
    • 权限管理之用户与组
    • 权限管理之文件权限
    • 权限管理之ACL
    • 权限管理之su与sudo
    • 软件包管理之rpm
    • 软件包管理之yum
    • 查看进程
    • 管理进程
    • 存储管理之传统磁盘管理
    • 存储管理之LVM
    • 网络管理
    • shell基础
    • crond计划任务
    • 系统优化
  • mysql

  • git

  • 数据结构与算法

  • axure

  • english

  • docker

  • IT_Need
  • linux基础
DC
2022-10-08
目录

文件管理之命令补充

# find

文件查找命令

find 目录 [-name;-size;-maxdepth;-mtime;-user;-group;-type;-inum;-perm] ...
找到后的处理动作 -print -ls -ok -delete -exec

# 按文件名

会在指定的目录下递归查找..
"ifcfg-ens33"确切的文件名;"ifcfg-*"以 *ifcfg- * 开头的文件名
-i 忽略文件名的大小写.

[root@localhost opt]# find /etc -name "ifcfg-ens33"
/etc/sysconfig/network-scripts/ifcfg-ens33
[root@localhost opt]# find /etc -name "ifcfg-*"
/etc/sysconfig/network-scripts/ifcfg-lo
/etc/sysconfig/network-scripts/ifcfg-ens33
[root@localhost opt]# find /etc -iname "ifcfg-ens33"
/etc/sysconfig/network-scripts/ifcfg-ens33
1
2
3
4
5
6
7

# 按文件大小

find man ; / -size 查看帮助信息

find /etc -size 3M          # 文件大于3M 等同于 find /etc -size 3M -print
find /etc -size +3M         # 文件等于3M 默认后面会加 -print
find /etc -size -3M         # 文件小于3M
find /etc -size +3M -ls     # 找到文件后 -ls 显示文件的详细信息

【了解】
若查找的文件大小以b为单位,注意这里的b不是字节之意,这里的1b为512个字节.
不写单位,默认也是以b为大小单位.
512bytes即机械磁盘读取数据的最小单位1个扇区的大小.. 通俗点,1b为1个扇区.
若查找命令为 find . -size 2b
那么只要此文件占据两个扇区的大小,大小处于512bytes-1024bytes都会被查找到.

【扩展 进行验证】造一个1024bytes和513字节的文件
"""
if inputfile输入文件 /dev/zero此设备文件往外产生0
of outputfile输出文件
bs blocksize块大小 默认单位是字节
count 有多少块
注意:输出文件是没有内容的,这是一种虚拟的概念,只是将这个文件填充到指定大小.
"""
[root@localhost opt]# dd if=/dev/zero of=b.txt bs=1024 count=1
记录了1+0 的读入
记录了1+0 的写出
1024字节(1.0 kB)已复制,0.00648155 秒,158 kB/秒
[root@localhost opt]# dd if=/dev/zero of=c.txt bs=513 count=1
记录了1+0 的读入
记录了1+0 的写出
513字节(513 B)已复制,0.000407585 秒,1.3 MB/秒
[root@localhost opt]# cat b.txt
[root@localhost opt]# ll
总用量 8
-rw-r--r-- 1 root root 1024 7月  14 11:07 b.txt
-rw-r--r-- 1 root root  513 7月  14 11:11 c.txt
[root@localhost opt]# find . -size 2 -ls
35589993    4 -rw-r--r--   1 root     root         1024 7月 14 11:07 ./b.txt
35589994    4 -rw-r--r--   1 root     root          513 7月 14 11:11 ./c.txt
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

# 文件查找深度

默认会递归查找指定目录下的所有层级.. 可通过 -maxdepth levels 设置层级.

# /根目录	;  -maxdepth 5层级 ; -a并且 	; -name "ifcfg-*" 文件名
# 可不加-a,默认就是-a. 并列关系同时成立.
# 补充:	-o 或者. 条件成立一个即可
[root@localhost opt]# find / -maxdepth 5 -a  -name "ifcfg-*"
/etc/sysconfig/network-scripts/ifcfg-lo
/etc/sysconfig/network-scripts/ifcfg-ens33
1
2
3
4
5
6

# 按时间查找

atime 最近访问; mtime 最近更改; ctime最近改动.
Ps: 在后续权限部分会详细讲解什么的变化会导致这三种时间的改动.

find /etc -mtime +3       # 修改时间超过3天
find /etc -mtime 3        # 修改时间等于3天
find /etc -mtime -3       # 修改时间3天以内
1
2
3

# 按文件属主、属组

find /home -user egon               # 属主是egon的文件
find /home -group it                # 属组是it组的文件
find /home -user egon -group it	 等同于 	find /home -user egon -a -group it
find /home -user egon -o -group it

"""
每创建一个用户,会在/etc/passwd添加用户的属主信息,在/etc/group添加属组信息.
"""
find /home -nouser        # 用户还存在	但在/etc/passwd中删除了记录
find /home -nogroup       # 用户还存在	但在/etc/group中删除了记录

【补充】`id root` 查看root用户的信息
[root@localhost opt]# id root
uid=0(root) gid=0(root) 组=0(root)
[root@localhost opt]# id dc
uid=1001(dc) gid=1001(dc) 组=1001(dc)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

# 按文件类型

find /dev -type f                       # f普通文件
find /dev -type d                       # d目录
find /dev -type l                       # l链接
find /dev -type b                       # b块设备
find /dev -type c                       # c字符设备
find /dev -type s                       # s套接字 通常以.sock结尾
find /dev -type p                       # p管道文件
1
2
3
4
5
6
7

# inode号、文件权限

find / -inum 1811         # 根据inode号查找: -inum n
find . -perm -600 -ls     # 按文件权限: -perm
1
2

# find结合xargs

xargs的参考文档: https://zhuanlan.zhihu.com/p/340804463 !!!!!!

▲ 删除没有属主和属组的文件
"""
rm -rf 本身是不支持从管道里取东西的
需要加上参数 xargs 从管道里读取参数交给 rm -rf命令
"""
find /home -nouser -nogroup | xargs rm -rf
"""
等同于 以下三条命令皆可
find /home -nouser -nogroup -delete
find /home -nouser -nogroup -exec rm -rf {} \;  # 非交互式 注意-exec慎用!
find /home -nouser -nogroup -ok rm -rf {} \;    # 交互式(每个文件都要问是否确定删除)
注意: 
		这里的 cp rm 最后 \; 是固定写法.
"""

▲ 将找到的文件移动、复制、更改权限
"""
xargs -I 负责从管道里把结果拿出来,采集好丢到{}花括号里
这样的话,只要用花括号就代表用管道左边命令的结果
"""
[root@localhost opt]# find /etc -name "ifcfg-*" | xargs -I {} cp -rf {} /opt
[root@localhost opt]# ls
ifcfg-ens33  ifcfg-lo
"""
等同于
find /etc -name "ifcfg-*" -exec cp -rf {} /opt \;
"""

find /test -name "ifcfg-ens33" | xargs -I {} mv {} /ttt
find /ttt -name "ifcfg*" | xargs -I {} chmod 666 {}
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

# 上传与下载

# wget下载

wget -O 本地路径 远程包链接地址
未指定本地路径,默认是下载到当前目录;指定本地路径可以指定下载软件的命名.

Ps: 浏览器在官网点击就能下载的软件包连接、右键复制的图片链接都能通过wget下载..

wget http://nginx.org/download/nginx-1.22.0.tar.gz
wget -O /opt/nginx.tar.gz  http://nginx.org/download/nginx-1.22.0.tar.gz

[root@localhost opt]# wget -O ./1.jpg https://pics5.baidu.com/feed/6a600c338744ebf8ef9d643e6a39b9226159a708.jpeg?token=64b2865816ad2b3b0e49faf1fd43d8d5
[root@localhost opt]# ls
1.jpg  nginx-1.22.0.tar.gz  nginx.tar.gz

"""
如果wget下载提示无法建立SSL连接,则加上选项--no-check-certificate!!!
wget --no-check-certificate -O 本地路径 远程包链接地址 
"""
1
2
3
4
5
6
7
8
9
10
11

# curl测试

curl命令是一个利用URL规则在命令行下工作的文件传输工具.

curl的主要功能用于测试!
下载:curl -o 文件路径 远程链接 下载成功后,文件会保存本地.
<测试>:curl 远程链接 会显示下载的内容,但文件不会保存到本地!不占用本地硬盘空间.

[root@localhost opt]# curl -o ./2.jpg https://pics5.baidu.com/feed/6a600c338744ebf8ef9d643e6a39b9226159a708.jpeg?token=64b2865816ad2b3b0e49faf1fd43d8d5
[root@localhost opt]# curl -o  /opt/nginx2.tar.gz  http://nginx.org/download/nginx-1.22.0.tar.gz
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 1048k  100 1048k    0     0   191k      0  0:00:05  0:00:05 --:--:--  232k
[root@localhost opt]# ls
1.jpg  2.jpg  nginx-1.22.0.tar.gz  nginx2.tar.gz  nginx.tar.gz

"""
如果curl遇到下载提示无法建立SSL链接,使用-k选项或者--insecure
curl -k -o 本地路径 远程包链接地址
"""

【压力测试】一个客户端不断的向百度网页发送请求.
悄悄话: 上网就是一个不断上传(注册,支付)下载(打开网页)的过程!
[root@localhost opt]# while true; do curl https://www.baidu.com/; done
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

# sz/rz

需要先下载依赖的软件包 yum install lrzsz -y
sz,rz 是Linux/Unix同 Windows 进行文件传输的命令行工具

""" sz  linux ==> windows
▲ 将linux服务器上选定的文件下载/发送到windows端
注意哦,有个前提,使用此命令的所在终端要有弹出图形窗口的机制.
所以windows上的xshll使用此命令可以,但linux服务器上的终端使用就会出错.
"""
[root@localhost opt]# sz nginx.tar.gz


""" rz	windows ==> linux
▲ 运行该命令会弹出一个文件选择窗口,从本地选择文件上传到linux服务器.
如果linux主机上目标文件名已经存在,则上传失败,可以用-E选项解决 
rz -E 重命名传入文件,新文件名将添加一个点和一个数字(0..999)
"""
[root@localhost opt]# rz
1
2
3
4
5
6
7
8
9
10
11
12
13
14

# 输出/入与重定向

# > >>输出重定向

正常输出是把 内容 输出到显示器上,而输出重定向是 把内容输出到文件中
> 覆盖; >>追加. 1> 2> &>

ps: 1 2指的是文件描述符 详见19_系统优化.md ulimit 部分的内容

[root@localhost opt]# ls
[root@localhost opt]# ifconfig > a.txt
[root@localhost opt]# head -5 a.txt 
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.16.150.131  netmask 255.255.255.0  broadcast 172.16.150.255
        inet6 fe80::20c:29ff:fe90:303e  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:90:30:3e  txqueuelen 1000  (Ethernet)
        RX packets 164409  bytes 161330384 (153.8 MiB)
[root@localhost opt]# echo "123" >> a.txt
[root@localhost opt]# pwd >> a.txt 
[root@localhost opt]# tail -2 a.txt 
123
/opt
[root@localhost opt]# echo "haha" > a.txt
[root@localhost opt]# cat a.txt 
haha
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

一条命令的执行产生的结果内容可能是正确的也有可能是错误的.
因而在linux中, 0 代表标准输入; 1 代表标准正确输出; 2 代表标准错误输出.

"""
命令错误结果是不能直接输出到文件中的. 命令正确的结果可以.
即单独写 > 相当于是 1>
"""
[root@localhost opt]# ifconfig ensxx
ensxx: error fetching interface information: Device not found
[root@localhost opt]# ifconfig ensxx > b.txt 
ensxx: error fetching interface information: Device not found
1
2
3
4
5
6
7
8

▲ 将命令正确的结果输到a.txt里,错误的结果输到b.txt里 
注意: 1> 2>     1和2与>中间不能有空格!

达到输出结果分流的效果.
[root@localhost opt]# ifconfig ensxx 1>a.txt 2>b.txt 
[root@localhost opt]# cat a.txt 
[root@localhost opt]# cat b.txt 
ensxx: error fetching interface information: Device not found
1
2
3
4
5

▲ 将命令不管正确还是错误的结果都输出到同一个文件中

Ps: 还有一种写法能达到一样的效果(了解即可) `ifconfig ensxx > c.txt 2&>1`
[root@localhost opt]# ifconfig ensxx &> c.txt 
[root@localhost opt]# pwd &>> c.txt 
[root@localhost opt]# cat c.txt 
ensxx: error fetching interface information: Device not found
/opt
1
2
3
4
5
6

Q: 什么时候使用 &> &>>? 
A: 1> 只需要此命令完成某个操作,并且验证后很清楚的知道此命令的结果是没有问题;
        当把此命令放到脚本程序里时,就不需要输出结果啦.
        eg ls &>/dev/null 直接 丢到黑洞文件 里.
     2> 一堆命令的结果(脚本文件/程序) &>> 输出到日志文件里,方便查看.

# < <<输入重定向

[root@localhost opt]# cat /etc/hostname
localhost.localdomain
[root@localhost opt]# echo 111 > d.txt
[root@localhost opt]# cat d.txt 
111

▼ 表示往d.txt里覆盖的内容来自/etc/hostname文件
	等同于 cat /etc/hostname > d.txt
[root@localhost opt]# cat > d.txt < /etc/hostname
[root@localhost opt]# cat d.txt 
localhost.localdomain

▼ 表示往d.txt里追加的内容来自/etc/hostname文件
	等同于 cat /etc/hostname >> d.txt
[root@localhost opt]# cat >> d.txt < /etc/hostname
[root@localhost opt]# cat d.txt 
localhost.localdomain
localhost.localdomain

▼ 等待键盘的输入
	EOF表示结束输入,这个可以自己设定. 建议用EOF.
[root@localhost opt]# cat > d.txt << EOF
> 111
> 222
> EOF
[root@localhost opt]# cat d.txt 
111
222
[root@localhost opt]# cat >> d.txt << AAA
> 333
> 444
> AAA
[root@localhost opt]# cat d.txt 
111
222
333
444
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

# 字符处理命令

# sort 排序

用于将文件内容加以排序

-t      指定分割符, 默认是以空格为分隔符
-n      按照数值的大小排序     指定后11是数值11,而不是字符串11 
-k      以某列进行排序 k2就是第二列
-r      以相反的顺序来排序

[无序数据准备]

[root@localhost opt]# cat >> file.txt <<EOF
> b:3
> c:2
> a:4
> d:1
> f:11
> EOF
[root@localhost opt]# cat file.txt
b:3
c:2
a:4
d:1
f:11
1
2
3
4
5
6
7
8
9
10
11
12
13

[默认以ACSII字符表比大小]
每个字符都会对应一个十进制数字,在计算机里会转换成二进制体现.
字符串第一个字符比较出来了,就不会比较第二个啦.. python中也是.

[root@localhost opt]# sort file.txt 
a:4
b:3
c:2
d:1
f:11
1
2
3
4
5
6

[以冒号右边的数值比大小] 
若不指定-n,会被当作字符串,用ACSII码比较.

[root@localhost opt]# sort -t ":" -n -k2 file.txt 
d:1
c:2
b:3
a:4
f:11
[root@localhost opt]# sort -t ":" -k2 file.txt 
d:1
f:11
c:2
b:3
a:4
1
2
3
4
5
6
7
8
9
10
11
12

[-r反向排序]

[root@localhost opt]# sort -t ":" -n -r -k2 file.txt 
f:11
a:4
b:3
c:2
d:1
1
2
3
4
5
6

# uniq 去重

去重. 用于检查及删除文本文件中 相邻重复 出现的行列, 所以一般与 sort 命令结合使用

-c      在每列旁边显示该行重复出现的次数
-d      仅显示重复出现的行列
-u      仅显示出一次的行列

[root@localhost opt]# cat file.txt 
hello
123
hello
123
123
func

"""
注意观察,直接使用uniq命令,只会使原数据中相邻的两个123去重.
所以需要sort命令将重复的全部放到一起
"""
[root@localhost opt]# uniq file.txt 
hello
123
hello
123
func
[root@localhost opt]# sort file.txt | uniq
123
func
hello
[root@localhost opt]# sort file.txt | uniq -c
      3 123
      1 func
      2 hello
[root@localhost opt]# sort file.txt | uniq -d
123
hello
[root@localhost opt]# sort file.txt | uniq -u
func
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

# cut 分割

用来显示行中的指定部分,删除文件中指定字段
cut支持管道符!

吐槽: awk完全可以取代cut的功能

-d     指定字段的分隔符
-f      显示指定分段的内容 -f1-3 -f1,3,4

[root@localhost opt]# head -1 /etc/passwd 
root:x:0:0:root:/root:/bin/bash
[root@localhost opt]# head -1 /etc/passwd | cut -d ":" -f1,3,4,6
root:0:0:/root

"""
-f1,2,3 等同于 -f1-3
"""
1
2
3
4
5
6
7
8

Ps: 分隔符是空格 必须用引号 cut -d " " ; 其余的都可以不用引号 cut -d: cut -d;

# tr 替换删除

用于替换或删除. 注意: 替换的字符是一一对应的,不是指整个字符串!
tr是支持管道的.

吐槽: sed配合管道也能达到同样的替换效果.

[root@localhost opt]# head -1 /etc/passwd
root:x:0:0:root:/root:/bin/bash

"""
替换 
注意: 替换的字符是一一对应的,不是整个字符串!
"""
[root@localhost opt]# head -1 /etc/passwd |tr "root" "ROOT"
ROOT:x:0:0:ROOT:/ROOT:/bin/bash
[root@localhost opt]# echo "hello egon yyds" > a.txt
[root@localhost opt]# cat a.txt 
hello egon yyds
[root@localhost opt]# tr "egon" "ABCD" < a.txt
hAllC ABCD yyds

"""
删除
"""
[root@localhost opt]# head -1 /etc/passwd |tr -d "root"
:x:0:0::/:/bin/bash
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

# wc 统计

用于统计,计算数字
wc是支持管道的

-c     统计文件的Bytes数
-l     统计文件的行数
-w     统计文件中单词的个数, 默认以空白字符(包括换行符)做为分隔符

[root@localhost opt]# ll /etc/passwd
-rw-r--r-- 1 root root 2351 7月   4 16:51 /etc/passwd
[root@localhost opt]# wc -c /etc/passwd
2351 /etc/passwd
[root@localhost opt]# wc -l /etc/passwd
45 /etc/passwd
# 等同于 wc -l /etc/passwd | cut -d" " -f1
[root@localhost opt]# cat /etc/passwd | wc -l
45

[root@localhost opt]# cat file.txt 
abc_123 b_1_2  aaabb
111  ;  ccc egonyyds
[root@localhost opt]# wc -w file.txt 
7 file.txt

"""
▲ 统计系统现目前系统中有多少个进程
grep -v "ps aux" 是因为当前这条命令也会产生一个进程
"""
[root@localhost opt]# ps aux | grep -v "ps aux" | wc -l
203
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

# tar/zip

打包会将多个文件归集到一起,压缩会减少占用的空间.
用途: (1)方便加快网络传输; (2)做备份

[方式一  tar]

打包:         tar cvf bak.tar a.txt b.txt c.txt
压缩:         gzip bak.tar or bzip2 bak.tar
打包压缩: tar cvzf bak.tar.gz a.txt b.txt c.txt
tar cvjf bak.tar.bz2 a.txt b.txt c.txt
解压缩:        tar xvf 打包压缩文件 -C 解压到的目录

选项 含义
c 创建打包压缩文件 -- 打包文件以.tar为后缀
v 显示打包的过程
f 为打包 or 打包压缩后的文件命名 / 解压缩时指定需要解压的打包压缩文件
z 使用gzip压缩算法 -- 以 .gz 结尾
j 使用bzip2压缩算法 -- 以 .bz2 结尾
x 解压缩
-C 指定解压到的目录, 此目录需要事先存在

Ps: 压缩包的后缀不携带不会报错,但 约定俗成要携带便于区分 ! 
普通文件压缩后会小很多;视频文件压缩不了多少空间..(压缩就是清理空格换行啥的)

[方式二  zip]

打包压缩: zip bak.zip a.txt b.txt c.txt
解压缩:         unzip bak.zip -d 解压到的目录

Ps: 若希望windows的软件能被linux解压,或者linux的软件包被windows能识别,选择zip!

[扩展]

打包压缩通常用于备份文件.
备份文件的名字必须见名知意且应该带上时间、主机名之类,便于数据恢复时找到需要的那个时间点的备份.

"""
``反引号代表获取命令的运行结果!
"""
tar czvf `date +"%Y_%m_%d_%H_%M_%S"`_bak.tar.gz /etc  

[root@localhost opt]# date +"%Y_%m_%d_%H_%M_%S %p"
2022_07_15_11_33_19 上午

date时间的具体使用: https://zhuanlan.zhihu.com/p/325728987  详情请看<8 拓展>那里
1
2
3
4
5
6
7
8
9

# 打包压缩文件

"""
打包的目标路径如果是绝对路径,会提示	tar:从成员名中删除开头的“/”		但这提示不会影响打包
可以cd到/etc目录下对文件进行打包,这样的话就会少一层文件夹

注意观察:
  bak.tar     打包文件    10240个字节
  bak.tar.gz  打包压缩文件 1170个字节
  再观察打包过程,它会自动带有文件的文件夹.
"""
[root@localhost opt]# tar cvf bak.tar /etc/passwd /etc/hosts /etc/hostname
tar: 从成员名中删除开头的“/”
/etc/passwd
/etc/hosts
/etc/hostname
[root@localhost opt]# ll
总用量 12
-rw-r--r-- 1 root root 10240 7月  15 10:10 bak.tar
[root@localhost opt]# gzip bak.tar 
[root@localhost opt]# ll
总用量 4
-rw-r--r-- 1 root root 1170 7月  15 10:10 bak.tar.gz


"""
加P选项,则‘tar:从成员名中删除开头的“/”’这个提示信息不会再显示

注意观察:
  bak2.tar.gz     1170字节
  bak3.tar.bz2    1180字节
  说明gzip压缩算法比bzip2压缩算法压缩的更多!
"""
tar cvzPf bak2.tar.gz /etc/passwd /etc/hosts /etc/hostname 
tar cvjPf bak3.tar.bz2 /etc/passwd /etc/hosts /etc/hostname
[root@localhost opt]# ll
总用量 16
-rw-r--r-- 1 root root 1170 7月  15 10:10 bak2.tar.gz
-rw-r--r-- 1 root root 1180 7月  15 10:11 bak3.tar.bz2
-rw-r--r-- 1 root root 1170 7月  15 10:10 bak.tar.gz
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

# 解压缩

"""
无需指定解压算法,tar会自行判断,自动调用gunzip和bunzip2!
不指定-C,则会解压到当前目录下.
"""
[root@localhost opt]# mkdir test
[root@localhost opt]# tar xvf bak.tar.gz -C /opt/test
etc/passwd
etc/hosts
etc/hostname
[root@localhost opt]# ll test/etc
总用量 12
-rw-r--r-- 1 root root   22 6月  30 18:35 hostname
-rw-r--r-- 1 root root  158 6月   7 2013 hosts
-rw-r--r-- 1 root root 2351 7月   4 16:51 passwd
1
2
3
4
5
6
7
8
9
10
11
12
13
14

文件管理之四大模式
文件管理之文件系统

← 文件管理之四大模式 文件管理之文件系统→

最近更新
01
deepseek本地部署+知识库
02-17
02
实操-微信小程序
02-14
03
教学-cursor深度探讨
02-13
更多文章>
Theme by Vdoing | Copyright © 2023-2025 DC | One Piece
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式