Linux - 文件管理

一:文本处理三剑客

sed

流式编辑器,主要擅长对文件的编辑操作

可以事先定制好编辑文件的指令,然后让sed自动完成对文件的整体编辑

用法

sed 选项 '定位+命令' 文件路径

选项

选项 作用
-n 取消默认输出(不会输出文件原内容)
-r 支持扩展正则元字符
-i 立即编辑文件

定位

行定位:
  • 1定位到第1行
  • 1,3代表从第1行到第3行
  • 不写定位 代表定位所有行
正则表达式定位:
  • /darker/ 包含darker的行
  • /^darker/ 以darker开头的行
  • /darker$/ 以darker结尾的行
数字+正则表达式定位:
  • 1,8p 代表打印1到8行,
  • 1,/darker/p 则代表取从第1行到首次匹配到/darker/的行

命令

命令 作用
d 删除
p 复制
s/before/after/g
before:替换前的内容
after:替换后的内容
替换
命令可以用;连接多条

测试

# 创建文件夹test
[root@localhost ~]# mkdir test


# 进入文件夹
[root@localhost test]# cd test


# 创建文件1.txt
[root@localhost test]# touch 1.txt


# 进入1.txt进行编辑
[root@localhost test]# vim 1.txt

# 按 i 进入插入模式
i


# 添加文本
darkerline1
linedarker22
line333
line4444darker
linedarker55555
line666cm666cm


# 退出并保存
Esc
:wq


# 正常查看1.txt内容
[root@localhost test]# cat 1.txt
darkerline1
linedarker22
line333
line4444darker
linedarker55555
line6
66cm666cm

# 默认匹配所有行,显示1.txt的全部内容
[root@localhost test]# sed '' 1.txt
darkerline1
linedarker22
line333
line4444darker
linedarker55555
line666cm666cm


# 取消默认输出,默认匹配所有行,显示a.txt的全部内容
[root@localhost test]#  sed -n '' 1.txt
# 没有输出


# 定位1.txt第1行到第5行 进行复制(p),显示原文内容 以及复制内容
[root@localhost test]# sed '1,5p' 1.txt
darkerline1
darkerline1
linedarker22
linedarker22
line333
line333
line4444darker
line4444darker
linedarker55555
linedarker55555
line666cm666cm


# 从第1行开始匹配到第1个包含darker的行进行复制
[root@localhost test]# sed '1,/darker/p' 1.txt
darkerline1
darkerline1
linedarker22
linedarker22
line333
line4444darker
linedarker55555
line666cm666cm


# 取消默认输出从第一行匹配到第一个包含xxx的行进行复制
[root@localhost test]# sed -n '1,/darker/p' 1.txt
darkerline1
linedarker22


# 从第1行开始匹配到第1个包含333的行进行复制
[root@localhost test]# sed '1,/333/p' 1.txt
darkerline1
darkerline1
linedarker22
linedarker22
line333
line333
line4444darker
linedarker55555
line666cm666cm


# 从第1行开始匹配到第1个包含darker的行删除(从第1行删到包含darker的行)
[root@localhost test]# sed '1,/darker/d' 1.txt
line333
line4444darker
linedarker55555
line666cm666cm


# 删除第1行、第3行、第5行,输出其他行
[root@localhost test]# sed '1d;3d;5d' 1.txt
linedarker22
line4444darker
line666cm666cm


# 把所有行的所有的darker 替换成DARKER,然后输出
[root@localhost test]# sed 's/darker/DARKER/g' 1.txt
DARKERline1
lineDARKER22
line333
line4444DARKER
lineDARKER55555
line666cm666cm


# 把所有以darker开头的 行中的darker 替换成Start,然后输出
[root@localhost test]# sed '/^darker/s/darker/Start/g' 1.txt
Startline1
linedarker22
line333
line4444darker
linedarker55555
line666cm666cm


# 只把第6行的首个cm换成HIGH
[root@localhost test]# sed '6s/cm/HIGH/' 1.txt
darkerline1
linedarker22
line333
line4444darker
linedarker55555
line666HIGH666cm


# 只把第6行的所有cm换成HIGH(加上g代表全部替换)
[root@localhost test]# sed '6s/cm/HIGH/g' 1.txt
darkerline1
linedarker22
line333
line4444darker
linedarker55555
line666HIGH666HIGH


# 把1到3行的darker换成WOW
[root@localhost test]# sed '1,3s/darker/WOW/g' 1.txt
WOWline1
lineWOW22
line333
line4444darker
linedarker55555
line666cm666cm


# 删除2-5行,复制并输出第1行和第6行(sed也支持管道操作)
[root@localhost test]# cat 1.txt | sed '1p;2d;3d;4d;5d;6p'
darkerline1
darkerline1
line666cm666cm
line666cm666cm

sed命令加上-i选项,可以直接修改文件,通常会在调试完毕确保没有问题后再加-i选项

awk

主要用于处理有格式的文本,例如/etc/passwd这种

把文档内容切成一段一段

用法

awk 选项 'pattern{action}' 文件路径

选项

选项 作用
-F 指定分隔符(默认是空格

工作流程

awk -F: '{print $1,$3}' /etc/passwd

① awk会读取文件的1行内容然后赋值给$0

② 然后awk会以-F指定的分隔符将该行切分成n段,最多可以达到100段,第一段给$1,第二段给$2,以此类推

print输出该行的第1段和第3段,逗号代表输出分隔符,默认与-F保持一致

④ 重复步骤1,2,3直到文件内容读完

内置变量

变量 作用
$0 一整行内容
NR 记录号,等同于行号
NF -F分隔符分隔的段数

pattern的内容

  • 正则
/正则/		# 该行内容匹配成功正则
$1 ~ /正则/	# 第一段内容匹配成功正则
$1 !~ /正则/	# 第一段内容没有匹配成功正则
  • 比较运算
NR >= 3 && NR <=5	# 3到5行
$1 == "root"		# 第一段内容等于root

action的内容

  • 行号
print $1,$3		# 第1行和第3行
print $0 		# 一整行

测试

# 准备数据
[root@localhost test]# vim 2.txt


# 插入数据
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin


# 保存并退出
Esc
:wq


# 查看2.txt
[root@localhost test]# cat 2.txt
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin


# 查询:以:为分隔符 行号大于3的行 打印第1段内容
[root@localhost test]# awk -F: 'NR>3{print $1}' 2.txt
adm
lp


# 查询:以:为分隔符 从第1行开始匹配 以root开头的行 打印第1段和第3段内容
[root@localhost test]#  awk -F: '/^root/{print $1,$3}' 2.txt
root 0


# 查询:以:为分隔符 从第1行开始匹配 以字母d开头的 打印第1段和第3段内容
[root@localhost test]# awk -F: '$1 ~ /^d/{print $1,$3}' 2.txt
daemon 2


# 查询:以:为分隔符 从第1行开始匹配 不是以字母d开头的 打印第1段和第3段内容
[root@localhost test]# awk -F: '$1 !~ /^d/{print $1,$3}' 2.txt
root 0
bin 1
adm 3
lp 4


# 查询:以:为分隔符 匹配第1段内容为lp的行 并打印该行全部内容
[root@localhost test]# awk -F: '$1 == "lp"{print $0}' 2.txt
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin


# 查询:以:为分隔符 打印第1段内容(awk也支持管道命令)
[root@localhost test]# cat 2.txt | awk -F: '{print $1}'
root
bin
daemon
adm
lp

事实上awk是一门编程语言,可以独立完成很强大的操作

grep

grep擅长过滤内容

用法

grep 选项 '正则' 文件路径

选项

选项 作用
-n, --line-number 在过滤出的每一行前面加上它在文件中的相对行号
-i, --ignore-case 忽略大小写
--color 颜色
-l, --files-with-matches 如果匹配成功,则只将文件名打印出来,失败则不打印
通常-rl一起用,grep -rl 'root' /etc
-R, -r, --recursive 递归

测试

# 匹配以root开头的行
[root@localhost test ~]# grep '^root' /etc/passwd
root:x:0:0:root:/root:/bin/bash


# 匹配以bash结尾的行并显示行号
[root@localhost test ~]# grep -n 'bash$' /etc/passwd
1:root:x:0:0:root:/root:/bin/bash


# 配合管道命令,查询有ssh的进程(包括这 grep ssh 这句查询命令本身)
[root@localhost test ~]# ps aux | grep ssh
root       1104  0.0  0.3  83040  3652 ?        Ss   16:05   0:00 /usr/sbin/sshd -D
root       2242  0.0  0.5 141268  5304 ?        Ss   16:05   0:00 sshd: root@pts/0
root       2501  0.0  0.0 112644   952 pts/0    S+   17:58   0:00 grep --color=auto ssh


# 配合管道命令,查询有ssh的进程(不包括这 grep ssh 这句查询命令本身)
[root@localhost test ~]# ps aux | grep [s]sh
root       1104  0.0  0.3  83040  3652 ?        Ss   16:05   0:00 /usr/sbin/sshd -D
root       2242  0.0  0.5 141268  5304 ?        Ss   16:05   0:00 sshd: root@pts/0


# 匹配/etc下文件内容中包含root的文件名并打印(只打印文件名)
[root@localhost test ~]# grep -rl 'root' /etc
/etc/fstab
/etc/pki/tls/certs/make-dummy-cert
/etc/pki/tls/openssl.cnf
...

二:文件查找

1.查看命令所属的文件

# 查找ip命令
[root@localhost ~]# which ip
/usr/sbin/ip

# 查找ping命令
[root@localhost ~]# which ping
/usr/bin/ping

# 一些命令的路径都被配置到了环境变量PATH里
[root@localhost ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

2.查找文件

语法:

find [options] [path...] [expression]

按文件名查找

# 按照文件名查找(完整匹配):查找/etc/下 名称为ifcfg的文件
[root@localhost ~]# find /etc/ -name 'ifcfg-lo'
/etc/sysconfig/network-scripts/ifcfg-lo


# 按照文件名查找(通配符*):查找/etc/下 名称以hosts开头的文件
[root@localhost ~]# find /etc/ -name 'hosts*'
/etc/hosts
/etc/hosts.allow
/etc/hosts.deny


# 按照文件名查找(忽略大小写):查找/etc/下 名称以ifcfg开头的文件 忽略大小写
[root@localhost ~]# find /etc/ -iname 'ifcfg*'
/etc/sysconfig/network-scripts/ifcfg-lo
/etc/sysconfig/network-scripts/ifcfg-eno16777736

按文件大小查找

# 查找/etc目录下大于3M的文件
[root@localhost ~]# find /etc -size +3M
/etc/udev/hwdb.bin
/etc/selinux/targeted/policy/policy.29


# 查找/etc目录下等于3M的文件
[root@localhost] ~# find /etc -size 3M


# 查找/etc目录下小于3M的文件
[root@localhost ~]# find /etc -size +3M
/etc
/etc/fstab
/etc/crypttab
/etc/mtab
...


# -ls找到的处理动作
[root@localhost ~]# find /etc -size +3M -ls
34243049 6824 -r--r--r--   1 root     root      6984832 Nov 18 17:47 /etc/udev/hwdb.bin
101322226 3688 -rw-r--r--   1 root     root      3773297 Nov 18 21:44 /etc/selinux/targeted/policy/policy.29

按照指定目录深度查找

# 查找目录深度为5 并且 名字包含ifcfg的文件(-a:并且,-o:或者;如果不加-a,默认就是-a)
[root@localhost ~]# find / -maxdepth 5 -a -name "ifcfg*"
/etc/sysconfig/network-scripts/ifcfg-lo
/etc/sysconfig/network-scripts/ifcfg-eno16777736
/var/log/anaconda/ifcfg.log
/usr/sbin/ifcfg
/usr/share/man/man8/ifcfg.8.gz

按照时间查找(atime、mtime、ctime)

# 查找修改时间在3天以上的日志文件
[root@localhost ~]# find / -mtime +3 -name "*.log"
/tmp/yum.log
/var/log/anaconda/anaconda.log
/var/log/anaconda/X.log
/var/log/anaconda/program.log
/var/log/anaconda/packaging.log
/var/log/anaconda/storage.log
/var/log/anaconda/ifcfg.log
/var/log/anaconda/ks-script-JuIsIQ.log
/var/log/anaconda/journal.log
/usr/lib/rpm/rpm.log

[root@localhost ~]# find /etc -mtime 3 # 修改时间等于3天
[root@localhost ~]# find /etc -mtime -3 # 修改时间3天以内

按照文件属主、属组查找:

# 查找属主是darker的文件
[root@localhost ~]# find /home -user darker    


# 查找属组是it组的文件
[root@localhost ~]# find /home -group it    

[root@localhost ~]# find /home -user cm -group it
[root@localhost ~]# find /home -user cm -a -group it  # 同上意思一样
[root@localhost ~]# find /home -user cm -o -group it

[root@localhost ~]# find /home -nouser # 用户还存在,在/etc/passwd中删除了记录
[root@localhost ~]# find /home -nogroup # 用户还存在,在/etc/group中删除了记录
[root@localhost ~]# find /home -nouser -o -nogroup

按照文件类型查找

find -perm mode详解
-perm mode	# 文件的权限正好是mode就匹配
-perm -mode	# 文件的权限包括mode就匹配(该文件还可以拥有额外的权限属性)
-perm +mode	# 文件的权限部分满足mode就匹配(已弃用,find新版使用-perm /mode)
[root@localhost ~]# find /dev -type f	# f普通文件
[root@localhost ~]# find /dev -type d	# d目录
[root@localhost ~]# find /dev -type l	# l链接文件
[root@localhost ~]# find /dev -type b	# b块设备
[root@localhost ~]# find /dev -type c	# c字符设备
[root@localhost ~]# find /dev -type s	# s套接字
[root@localhost ~]# find /dev -type p	# p管道文件

按照文件权限查找

# 创建1-4.txt
[root@localhost ~]#touch {1..4}.txt

# 修改1-4.txt的文件权限
[root@localhost ~]#chmod 6000 1.txt
[root@localhost ~]#chmod 2000 2.txt
[root@localhost ~]#chmod 4000 3.txt
[root@localhost ~]#chmod 6600 4.txt

[root@localhost ~]# find . -perm 6000 -ls
67372494    4 ---S--S---   1 root     root          378 Nov 24 05:13 ./1.txt

#按文件权限查找,精确匹配权限为6000的文件
[root@localhost test]# find . -perm -6000 -ls			# 按文件权限查找,文件权限为6000和6000以上的文件
[root@localhost test]# find . -perm +6000 -ls			# 按文件权限查找,部分权限符合6000的文件,因centos已启用+mode写法,此处会报错
[root@localhost test]# find . -perm /6000 -ls			# 按文件权限查找,部分权限满足6000的文件
[root@localhost local]# find . -perm 644 -ls			# 按文件权限查找,文件权限为644的文件
[root@localhost local]# find . -perm -644 -ls			# 按文件权限查找,文件权限为644或644权限以上的文件
[root@localhost local]# find . -perm /644 -ls			# 按文件权限查找,部分权限满足644的文件

找到后处理的动作

  • print
  • ls
  • delete
  • exec
  • ok
[root@localhost ~]# find /etc -name "ifcfg*" -print  # 必须加引号,查找/etc/下名称为ifcfg*的文件并打印
[root@localhost ~]# find /etc -name "ifcfg*" -ls			#查找/etc/下名称为ifcfg*的文件并列出文件详情
[root@localhost ~]# find /etc -name "ifcfg*" -exec cp -rvf {} /tmp \;  # 非交互	#查找/etc/下名称为ifcfg*的文件并传递给cp命令将文件复制到/tmp目录下,{}代表find查找到的的内容
[root@localhost ~]# find /etc -name "ifcfg*" -ok cp -rvf {} /tmp \;  # 交互 	
#查找/etc/下名称为ifcfg*的文件并传递给cp命令将文件复制到/tmp目录下
[root@localhost ~]# find /etc -name "ifcfg*" -exec rm -rf {} \;		#查找/etc/下名称为ifcfg*的文件并传递给rm命令进行删除
[root@localhost ~]# find /etc -name "ifcfg*" -delete  # 同上

扩展:结合xargs

[root@localhost ~]# find . -name "*.txt" |xargs rm -rf
[root@localhost ~]# find /etc -name "ifcfg-eth0" |xargs -I {} cp -rf {} /var/tmp
[root@localhost ~]# find /test -name "ifcfg-ens33" |xargs -I {} mv {} /ttt
[root@localhost ~]# find /ttt/ -name "ifcfg*" |xargs -I {} chmod 666 {}
[root@localhost ~]# find . -name "egon*.txt" |xargs rm -rf
[root@localhost ~]# find /etc -name "ifcfg-eth0" |xargs -I {} cp -rf {} /var/tmp
[root@localhost ~]# find /test -name "ifcfg-ens33" |xargs -I {} mv {} /ttt
[root@localhost ~]# find /ttt/ -name "ifcfg*" |xargs -I {} chmod 666 {}

三:上传与下载

1.下载

wget命令

# 将远程包下载到本地,-O指定下载到哪里,可以生路-O 本地路径
wget -O 本地路径 远程包链接地址


# ps:如果wget下载提示无法建立SSL连接,则加上选项--no-check-certificate
wget --no-check-certificate -O 本地路径 远程包链接地址

curl命令

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

它支持文件的上传和下载,所以是综合传输 工具,但按传统,习惯称curl为下载工具

作为一款强力工具,curl支持包括HTTP、HTTPS、[ftp]等众多 协议,还支持POST、cookies、认证、从指定偏移处下载部分文件、用户代理字符串、限速、文件大小、进 度条等特征。做网页处理流程和数据检索自动化,curl可以祝一臂之力

[root@localhost ~]# curl -o 123.png https://www.xxx.com/img/hello.png

sz命令

将服务器上选定的文件下载/发送到本机

系统默认没有该命令,需要下载

yum install lrzsz -y
[root@localhost ~]# sz bak.tar.gz

2.上传

rz命令

系统默认没有该命令,需要下载

yum install lrzsz -y

运行该命令会弹出一个文件选择窗口,从本地选择文件上传到服务器

[root@localhost ~]# rz  # 如果文件已经存,则上传失败,可以用-E选项解决
[root@localhost ~]# rz -E # -E如果目标文件名已经存在,则重命名传入文件。新文件名将添加一个点和一个数字(0..999)

如果遇到下载提示无法简历SSL链接,使用-k选项或者--insecure

curl -k -o 123.png https://www.xxx.com/img/hello.png

四:输出与重定向

1.介绍

输出即把相关对象通过输出设备(显示器等)显示出来

输出又分正确输出错误输出

一般情况下标准输出设备为显示器,标准输入设备为键盘

在Linux中:

  • 0:标准输入
  • 1:正确输出
  • 2:错误输出
设备 设备文件名 文件描述符 类型
键盘 /dev/stdin 0 标准输入
显示器 /dev/stdout 1 标准输出
显示器 /dev/stderr 2 标准错误输出

2.输出重定向

正常输出是把内容输出到显示器上,而输出重定向是把内容输出到文件中,>代表覆盖,>>代表追加

Ps:标准输出的1可以省略
history >> a.txt 2>&1  	#history的记录全部保存到a.txt文件中,不管错误输出还是正确输出
history &>> b.txt		#同上

history >> a.txt		#默认为正确输出保存到a.txt
history ll 2>> b.txt	#将错误输出保存到b.txt文档中 

正确日志和错误日志分开保存

history >>file1.log 2>>file2.log	#将错误日志和正确日志分开保存,由于是正确输出,所以file2.log文件中无内容输入

3.输入重定向

没有改变输入的方向,默认键盘,此时等待输入

[root@localhost ~]# tr 'N' 'n'
No
no

echo "hello cm qq:123456" >> file.txt
[root@localhost ~]# tr 'cm' 'CM' < file.txt	#从file.txt文件中读取内容,将小写cm替换为大写CM

[root@localhost ~]# grep 'root' < /etc/passwd	#从/etc/passwd文件中读取内容匹配root所在的行
root:x:0:0:root:/root:/bin/bash

输入重定向:常用于MySQL恢复数据

mysql -u root -p123 < 1.sql

备份MySQL数据

mysqldump -u root -p123 mysql库名 >> 2.sql
history >> a.txt 2>&1
history &>> b.txt 
错误输出
history ll 2>> a.txt
history >>file1.log 2>>file2.log 
history --ll >>file1.log 2>>file2.log

五:字符处理命令

1.sort命令

用于将文件内容加以排序

命令 作用
-n 按照数值的大小进行排序
-r 用相反的顺序进行排序
-k 以某列进行排序
-t 指定分隔符(默认是空格)
# 向file.txt中输入内容,输入EOF才停止输入
cat >> file.txt << EOF
[root@localhost ~]# cat >> file.txt <<EOF      
b:3
c:2
a:4
e:5
d:1
f:11
EOF
#将终端输入内容插入file.txt文件中,碰到字符EOF结束。
[root@localhost ~]# sort file.txt 
a:4
b:3
c:2
d:1
e:5
f:11
#对file.txt文本进行排序
[root@localhost ~]# sort -t ":" -n -k2 file.txt   #以冒号为间隔符,对数字类型做比较,比较第二个字段的值
d:1
c:2
b:3
a:4
e:5
f:11
[root@localhost ~]# sort -t ":" -n -r -k2 file.txt   #以冒号为间隔符,对数字类型做比较,比较第二个字段的值
f:11
e:5
a:4
b:3
c:2
d:1

2.unic命令

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

选项 作用
-c 在每列旁边显示该行重复出现的次数
-d 仅显示重复出现的行列
-u 仅显示出一次的行列
[root@localhost ~]# cat > file.txt << EOF
> hello
> 123
> world
> 123
> hahaha
> 456
> EOF
[root@localhost ~]# cat file.txt
hello
123
world
123
hahaha
456
[root@localhost ~]# sort file.txt
123
123
456
hahaha
hello
world
[root@localhost ~]# sort file.txt | uniq
123
456
hahaha
hello
world
[root@localhost ~]# sort file.txt | uniq -c
      2 123
      1 456
      1 hahaha
      1 hello
      1 world
[root@localhost ~]# sort file.txt | uniq -u
456
hahaha
hello
world

3.cut命令

cut命令用来显示行中的指定部分,删除文件中指定字段
选项 作用
-d 指定字段的分隔符,默认的字段分隔符为"TAB"
-f 显示指定字段的内容
[root@localhost ~]# head -1 /etc/passwd 	#显示/etc/passwd文件的第一行内容
root:x:0:0:root:/root:/bin/bash
[root@localhost ~]# head -1 /etc/passwd | cut -d ":" -f1,3,4,6
#显示/etc/passwd第一行内容,并以冒号为间隔符,显示1,3,4,6段的内容
root:0:0:/root

4.tr命令

替换或删除命令
选项 作用
-d 删除字符
[root@localhost ~]# head -1 /etc/passwd |tr "root" "ROOT"
#查看/etc/passwd第一行的内容并将root替换为ROOT
ROOT:x:0:0:ROOT:/ROOT:/bin/bash
[root@localhost ~]# 
[root@localhost ~]# head -1 /etc/passwd |tr -d "root"
#查看/etc/passwd第一行的内容并将root字符删除掉显示到当前终端
:x:0:0::/:/bin/bash

[root@localhost ~]# echo "hello cm qq:123456" > a.txt
[root@localhost ~]# tr "cm" "CM" < a.txt 
hEllO CM qq:123456

5.wc命令

统计,计算数字
选项 作用
-c 统计文件的Bytes数
-l 统计文件的行数
-w 统计文件中单词的个数,默认以空白字符做为分隔符
[root@localhost ~]# cat file.txt
hello
123
world
123
hahaha
456
[root@localhost ~]# wc -c file.txt
31 file.txt
[root@localhost ~]# ll file.txt
-rw-r--r--. 1 root root 31 Nov 23 20:03 file.txt
[root@localhost ~]# wc -l file.txt
6 file.txt


[root@localhost ~]# ll file.txt 
-rw-r--r--. 1 root root 25 8月  12 20:09 file.txt
[root@localhost ~]# wc -c file.txt 	#统计file.txt文件的大小
[root@localhost ~]# wc -l file.txt	#统计file.txt文件的行数
[root@localhost ~]# grep "hello" file.txt |wc -l	#匹配file.txt文件中hello出现的次数并统计行数
[root@localhost ~]# wc -w file.txt		#统计file.txt文件中单词的个数

六:压缩 与 解压

压缩

[root@localhost test]# tar czvf etc1_bak.tar.gz /etc/  # 选项z代表gzip压缩算法
[root@localhost test]# tar cjvf etc1_bak.tar.bz2 /etc/  # 选项j代表bzip2压缩算法

解压

无论哪种压缩格式解压命令都相同
tar xvf etc1_bak.tar.gz -C /usr/local/		#将etc1_bak.tar.gz压缩包解压到/usr/local下
tar xvf etc1_bak.tar.bz2  .					#将etc1_bak.tar.bz2压缩包解压到当前目录下
posted @ 2020-11-28 21:57  轻描丨淡写  阅读(132)  评论(0编辑  收藏  举报