03、linux文件管理(高级)

一、文本处理三剑客命令基本使用

1、sed

流式编辑器,主要擅长对文件的编辑操作,我们可以事先定制好编辑文件的指令,然后让sed自动完成 对文件的整体编辑

# 用法
	sed 选项 "定位+命令" 文件路径

# 选项
    -n 取消默认输出
    -r 支持扩展正则元字符
    -i 立即编辑文件

# 定位
    行定位示例:
        1  	 定位到第一行
        1,3	 定位从第1行到第3行
        不写定位就代表定位所有行
    正则表达式定位示例:
        /root/  包含root的行
        /^root/ 以root开头的行
        /root$/ 以root结尾的行
    数字+正则表达式定位:
        "1,8p" 	代表打印1到8行,
        "1,/root/p"则代表取从第1行到首次匹配到/root/的行
        示例:
        [root@handsome ~]# sed -n '1,8p' /etc/passwd
        [root@handsome ~]# sed -n '1,/daemon/p' /etc/passwd
        
# 命令
	d  		删除
	p		复制
	s///g 	替换(所有)
	s///	替换(第一个)
	示例:
	# 命令可以用;号连接多多条,如1d;3d;5d代表删除1,3,5行
	sed -i '1d;3d;8d' a.txt   删除第1,3,8行并保存
	sed 's/ *//g' a.txt		 删除每一行的空格
	
# 使用示例:
	sed '' a.txt  	查看所有行
	sed -n '' a.txt	静默输出,默认不打印
	sed -n '1,/root/p' a.txt 从第一行开始匹配到第一个包含root的行进行复制
	sed '1,/root/d' a.txt	从第一行开始匹配到第一个包含root的行删除
	sed '1d;3d;5d' a.txt	删除第1,3,5行,输出其他行
	sed 's/root/ROOT/g' a.txt	将所有的root换成ROOT
	sed '/^bin/s/root/ROOT/g' a.txt  以bin开头的行中将所有的root换成ROOT
	sed '1,3s/root/ROOT/' a.txt  将1-3行中第一个root换成ROOT(没有加g)

2、awk

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

# 用法
	awk 选项 'pattern{action}' 文件路径
# 选项
	-F 指定行分隔符
	
# 工作流程
	示例:
	awk -F: '{print $1,$3}' /etc/passwd
    1、awk会读取文件的一行内容然后赋值给$0
    2、然后awk会以-F指定的分隔符将该行切分成n段,最多可以达到100段,第一段给$1,第二段给$2,依次类推
	3、print输出该行的第一段和第三段,逗号代表输出分隔符,默认与-F保持一致
	4、重复步骤1,2,3直到文件每一行内容读完
# 内置变量
    $0 一整行内容
    NR 记录号,等同于行号
    NF 以-F分隔符分隔的段数

# pattern
	1、/正则/
		/正则/	# 该行内容匹配成功正则
		$1 ~ /正则/ # 第一段内容匹配成功正则
		$1 !~ /正则/ # 第一段内容没有匹配成功正则
	2、比较运算
		NR>=3 && NR <=5  # 3-5行
		$1 == "root"  # 第一段内容等于root
		
# action可以是	
	print $1,$3

# 用法示例:
	awk -F: '/^root/{print $1,$3}' a.txt  # 以root开头的行,以:为分隔符,打印第一第三段内容
	awk -F: '$1 ~ /^r/{print $1,$3}' a.txt
	awk -F: '$1 !~ /^r/{print $1,$3}' a.txt
	awk -F: '$1 == 'root'{print $0}' a.txt
	awk -F: 'NR>3{print $1,$3}' a.txt
	awk -F: 'NR>3{print $1,$3}' a.txt
	cat b.txt | awk -F: '{print$1}'  # 管道

3、grep

grep擅长过滤内

# 用法
	grep 选项 '正则' 文件路径

# 选项
	-n, --line-number 			在过滤出的每一行前面加上它在文件中的相对行号
    -i, --ignore-case 			忽略大小写
    --color 					颜色
    -l, --files-with-matches 	如果匹配成功,则只将文件名打印出来,失败则不打印
    							通常-rl一起用,grep -rl 'root' /etc
    -R, -r, --recursive 		递归
   
# 示例
	grep '^root' /etc/passwd  # /etc/passwd中以root开头的行的内容
	grep -n 'bash$' /etc/passwd  # /etc/passwd中以bash结尾的行的行号和内容
	grep -rl 'bash' /etc	# /etc中所有含有bash的文件名
	# 支持管道
	[root@handsome ~]# ps aux | grep ssh
    root       1012  0.0  0.3  83040  3664 ?        Ss   Nov20   0:00 /usr/sbin/sshd -D
    root       7030  0.0  0.5 141268  5312 ?        Ss   04:39   0:01 sshd: root@pts/0
    root       7463  0.0  0.0 112644   940 pts/0    R+   10:12   0:00 grep --color=auto ssh
    # 加了[]就不匹配刚输入的指令
    [root@handsome ~]# ps aux | grep [s]sh
    root       1012  0.0  0.3  83040  3664 ?        Ss   Nov20   0:00 /usr/sbin/sshd -D
    root       7030  0.0  0.5 141268  5312 ?        Ss   04:39   0:01 sshd: root@pts/0

二、文件管理:文件查找

1、查看命令所属文件

[root@handsome ~]# which ip
/usr/sbin/ip
[root@handsome ~]# whereis ip
ip: /usr/sbin/ip /usr/share/man/man8/ip.8.gz

2、查找文件

  • 语法
find [options] [path...] [expression]
  • 按文件名:
find /etc -name "ifcfg*"
find /etc -iname "ifcfg-eth0"  # 加上i忽略大小写
  • 按文件大小
find /etc -size +3M		# 大于3M
find /etc -size 3M		# 等于3M
find /etc -size -3M  	# 小于3M
find /etc -size +3M -ls  # 找到以后的处理动作
  • 指定查找的目录深度
- maxdepth levels
find / -maxdepth 5 -a -name 'ifcfg*'  # -a (and 并且)深度为5,并且文件名叫。。。
find / -maxdepth 5 -o -name 'ifcfg*'  # -o (or 或者)深度为5,或者文件名叫。。。
  • 按时间找(atime、mtime、ctime)
[root@localhost ~]# find /etc -mtime +3  	# 修改时间超过3天
[root@localhost ~]# find /etc -mtime 3 		# 修改时间等于3天
[root@localhost ~]# find /etc -mtime -3 	# 修改时间3天以内
  • 按文件属主、属组找
[root@localhost ~]# find /home -user cc 				# 属主是cc的文件
[root@localhost ~]# find /home -group it 				# 属组是it组的文件
[root@localhost ~]# find /home -user cc -group it		 #  属主是cc并且属组是it的文件
[root@localhost ~]# find /home -user cc -a -group it 	  # 同上意思一样
[root@localhost ~]# find /home -user cc -o -group it	 #  属主是cc或者属组是it的文件
[root@localhost ~]# find /home -nouser 					# 用户还存在,在/etc/passwd中删除了记录
[root@localhost ~]# find /home -nogroup 				# 用户还存在,在/etc/group中删除了记录
[root@localhost ~]# find /home -nouser -o -nogroup
  • 按文件类型
[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管道文件
  • 按inode号查找: -inum n
[root@localhost ~]# find / -inum 1811
  • 按文件权限
[root@localhost ~]# find . -perm 644 -ls
[root@localhost ~]# find . -perm -644 -ls
[root@localhost ~]# find . -perm -600 -ls
[root@localhost ~]# find /sbin -perm -4000 -ls 		# 包含set uid
[root@localhost ~]# find /sbin -perm -2000 -ls 		# 包含set gid
[root@localhost ~]# find /sbin -perm -1000 -ls 		# 包含sticky
  • 找到以后的处理动作
-print
-ls
-delete
-exec
-ok
[root@localhost ~]# find /etc -name "ifcfg*" -print     # 必须加引号
[root@localhost ~]# find /etc -name "ifcfg*" -ls
[root@localhost ~]# find /etc -name "ifcfg*" -exec cp -rvf {} /tmp \;	# 非交互
[root@localhost ~]# find /etc -name "ifcfg*" -ok cp -rvf {} /tmp \; 	# 交互
[root@localhost ~]# find /etc -name "ifcfg*" -exec rm -rf {} \;
[root@localhost ~]# find /etc -name "ifcfg*" -delete 				 # 同上
  • 扩展:find结合xargs
[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命令
wget -O 本地路径 远程包链接地址
	# 将远程包下载到本地,-O指定下载到哪里,可以使用-O 本地路径
	# ps:如果wget下载提示无法建立SSL连接,则加上选项--no-check-certificate
wget --no-check-certificate -O 本地路径 远程包链接地址	
  • curl命令
#curl命令是一个利用URL规则在命令行下工作的文件传输工具。它支持文件的上传和下载,所以是综合传输工具,但按传统,习惯称curl为下载工具。作为一款强力工具,curl支持包括HTTP、HTTPS、[ftp]等众多协议,还支持POST、cookies、认证、从指定偏移处下载部分文件、用户代理字符串、限速、文件大小、进度条等特征。做网页处理流程和数据检索自动化,curl可以祝一臂之力。

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

# ps: 如果遇到下载提示无法简历SSL链接,使用-k选项或者--insecure
curl -k -o 123.png https://www.xxx.com/img/hello.png
  • sz命令
# 系统默认没有该命令,需要下载:yum install lrzsz -y
# 将服务器上选定的文件下载/发送到本机,
[root@handsome ~]# sz bak.tar.gz

2、上传

  • rz命令
# 系统默认没有该命令,需要下载:yum install lrzsz -y
# 运行该命令会弹出一个文件选择窗口,从本地选择文件上传到服务器。
[root@handsome opt]# rz     # 如果文件已经存在,则上传失败,可以用-E选项解决
[root@handsome opt]# rz -E  # -E:如果目标文件名已经存在,则重命名传入文件。新文件名将添加一个点和一个数字(0..999)

四、文件管理:输出与重定向

输出即把相关对象通过输出设备(显示器等)显示出来,输出又分正确输出和错误输出
一般情况下标准输出设备为显示器,标准输入设备为键盘。

linux中用:
	- 0 代表标准输入
	- 1 代表标准正确输出
	- 2 代表标准错误输出
设备设备文件名文件描述符类型
键盘/dev/stdin0标准输入
显示器/dev/sdtout1标准输出
显示器/dev/sdterr2标准错误输出

1、输出重定向:

正常输出是把内容输出到显示器上,而输出重定向是把内容输出到文件中,>代表覆盖,>>代表追加
Ps:标准输出的1可以省略
类型符号作用
标准输出重定向命令>文件以覆盖的方式,把命令的正确输入输出到指定的文件或者设备当中
命令>>文件以追加的方式,把命令的正确输入输出到指定的文件或者设备当中
标准错误输出重定向错误命令 2>文件以覆盖的方式,把命令的错误输入输出到指定的文件或者设备当中
错误命令 2>>文件以追加的方式,把命令的错误输入输出到指定的文件或者设备当中
例如:ifconfig > test.log 即把ifconfig执行显示的正确内容写入test.log.当前页面不再显示执行结果。

注意:错误输出重定向>与>>后边不要加空格

正确输出与错误输出同时保存

命令> 文件 2>&1以覆盖的方式,把正确输出和错误输出都保存到同一个文档中
命令>> 文件 2>&1以追加的方式,把正确输出和错误输出都保存到同一个文档中
命令 &>文件以覆盖的方式,把正确输出和错误输出都保存到同一个文档中
命令 &>>文件以追加的方式,把正确输出和错误输出都保存到同一个文档中
命令>>文件1 2>>文件2把正确输出追加到文件1中,把错误输出追加到文件2中

注意:

  • 下述两个命令作用相同
命令 >>file.log 2>&1
命令 &>>file.log
  • 正确日志和错误日志分开保存
命令 >>file1.log 2>>file2.log
  • 3、系统有个常见用法 ls &>/dev/null 正确输出或错误输出结果都不要。(null可以理解为黑洞或 垃圾站)

2、输入重定向

# 没有改变输入的方向,默认键盘,此时等待输入
[root@handsome ~]# tr 'N' 'n'
No
no
^C
[root@handsome ~]# tr 'N' 'n' < file.txt

#没有改变输入的方向,默认键盘,此时等待输入
[root@handsome ~]# grep 'root'
oldboy
root
root   # 筛选到了root,会变成红色打印出来
[root@handsome ~]# grep 'root'< /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

# 读写块设备
[root@handsome ~]# dd if=/dev/zero of=/file1.txt bs=1M count=20
[root@handsome ~]# dd </dev/zero >/file2.txt bs=1M count=20

# mysql如何恢复备份,了解即可,不用关注。
[root@handsome ~]# mysql -uroot -p123 < bbs.sql

五、文件管理:字符处理命令

1、sort命令

用于将文件内容加以排序:	
	-n 		# 依照数值的大小排序
	-r 		# 以相反的顺序来排序
	-k 		# 以某列进行排序
	-t 		# 指定分割符,默认是以空格为分隔符

准备文件,写入一段无序的内容(输入到end就自动结束保存)

[root@handsome ~]# cat >> file.txt <<end
> b:3
> c:12
> a:4
> d:5
> e:1
> end

示例1:sort默认以首字符排序

[root@handsome ~]# sort file.txt
a:4
b:3
c:12
d:5
e:1

示例2:可以指定按照按照:后面的数字排序

[root@handsome ~]# sort -t ':' -n -k2 file.txt
e:1
b:3
a:4
d:5
c:12
[root@handsome ~]# sort -t ':' -n -r -k2 file.txt
c:12
d:5
a:4
b:3
e:1

2、uniq命令

用于检查及删除文本文件中重复出现的行列,一般与 sort 命令结合使用
-c 	# 在每列旁边显示该行重复出现的次数。
-d 	# 仅显示重复出现的行列。
-u 	# 仅显示出一次的行列。

准备文件:

[root@handsome ~]# cat >file.txt <<end
> hello
> fine
> hello
> fine
> have fun
> 123
> end

示例:

[root@handsome ~]# sort file.txt
123
fine
fine
have fun
hello
hello
[root@handsome ~]# sort file.txt |uniq		# 不显示重复的行
123
fine
have fun
hello
hello
[root@handsome ~]# sort file.txt |uniq -c    # 显示重复的次数
      1 123
      2 fine
      1 have fun
      2 hello
[root@handsome ~]# sort file.txt |uniq -d	 # 显示重复行的内容
fine
hello

3、cut命令

cut命令用来显示行中的指定部分,删除文件中指定字段
选项:
    -d 	# 指定字段的分隔符,默认的字段分隔符为"TAB";可以自定义
    -f 	# 显示指定字段的内容;		

示例:

[root@handsome ~]# head -1 /etc/passwd
root:x:0:0:root:/root:/bin/bash
[root@handsome ~]# head -1 /etc/passwd | cut -d ':' -f 1,3,5
root:0:root

4、tr命令

替换或者删除命令
	-d  # 删除字符

示例:

[root@handsome ~]# head -1 /etc/passwd |tr 'root' 'ROOT'
ROOT:x:0:0:ROOT:/ROOT:/bin/bash
[root@handsome ~]# head -1 /etc/passwd |tr -d 'root'
:x:0:0::/:/bin/bash

[root@handsome ~]# echo 'hello world' > a.txt
[root@handsome ~]# cat a.txt
hello world
[root@handsome ~]# tr 'hello' 'HELLO' < a.txt
HELLO wOrLd

5、wc命令

统计,计算数字:
-c	# 统计文件的Bytes数
-l	# 统计文件的行数
-w  # 统计文件中单词的个数,默认以空白字符做为分隔符

示例:

[root@handsome ~]# ll a.txt
-rw-r--r--. 1 root root 12 Nov 21 13:19 a.txt
[root@handsome ~]# wc -c a.txt
12 a.txt							# 12个字符
[root@handsome ~]# wc -l a.txt
1 a.txt								# 一行内容
[root@handsome ~]# wc -w a.txt
2 a.txt								# 2个单词

示例2:与 | 联用

[root@handsome ~]# cat file.txt
hello
fine
hello
fine
have fun
123
[root@handsome ~]# grep 'hello' file.txt | wc -l
2
[root@handsome ~]# grep 'hello' file.txt | wc -w
2
[root@handsome ~]# grep 'hello' file.txt | wc -c
12

六、文件管理:打包压缩

1、介绍

## 什么是打包压缩
    打包指的是将多个文件和目录合并为一个特殊文件
    然后将该特殊文件进行压缩
    最终得到一个压缩包

## 目的:
    1、减少占用的体积
    2、加快网络的传输

## windows的压缩和linux的有什么不同
    windows: zip rar(linux不支持)
    linux: zip tar.gz tar.bz2 .gz
    如果希望windows的软件能被linux解压,或者linux的软件包被windows能识别,选择zip.
    PS: 压缩包的后缀不重要,但一定要携带.

2、linux下常见的压缩包类型、

格式压缩工具
.zipzip压缩工具
.gzgzip压缩工具,只能压缩文件,会删除源文件(通常配合tar使用)
.bz2bzip2压缩工具,只能压缩文件,会删除源文件(通常配合tar使用)
.tar.gz先使用tar命令归档打包,然后使用gzip压缩
.tar.bz2先使用tar命令归档打包,然后使用bzip2压缩

3、打包压缩方法

  • 方法一:tar压缩
# 1、打包
[root@localhost test]# tar cvf etc_bak.tar /etc/ 
# c创建 v详细 f打包后文件路径
ps:
    打包的目标路径如果是绝对路径,会提示:tar: 从成员名中删除开头的“/”,不影响打包,
    添加-P选项便不再提示:tar cvPf ...
    可以cd 到 /etc下然后tar cvf etc_bak.tar *打包,这样去掉了一层文件夹
   
# 2、压缩
[root@localhost test]# gzip etc_bak.tar 
# 文件体积变小,并且加上后缀.gz
ps:
    gzip -> gunzip
    bzip2-> bunzip2

# 3、上述两步可以合二为一
[root@localhost test]# tar czvf etc1_bak.tar.gz /etc/ # 选项z代表gzip压缩算法
[root@localhost test]# tar cjvf etc1_bak.tar.bz2 /etc/ # 选项j代表bzip2压缩算法
  • 方式二:zip压缩
zip压缩选项:
    -r 	# 递归压缩 压缩目录
    -q 	# 静默输出
    
# 示例1:
[root@localhost ~]# zip /test/bak.zip a.txt b.txt c.txt 
# zip后的第一个参数是压缩包路径,其余为被压缩的文件
adding: a.txt (stored 0%)
adding: b.txt (stored 0%)
adding: c.txt (stored 0%)
[root@localhost ~]# ls /test/
bak.zip

# 示例2:
[root@localhost ~]# zip -rq etc.zip /etc 
# 加上-q后压缩过程不再提示

4、解压缩

#1、针对xxx.tar.gz 或者 xxx.tar.bz2,统一使用
[root@localhost test]# tar xvf 压缩包 -C 解压到的目录 # 无需指定解压算法,tar会自动判断

#2、针对xxx.zip,用unzip
选项:
    -l 		# 显示压缩包的列表信息
    -q 		# 静默输出
    -d 		# 解压到指定的目录		
[root@localhost test]# unzip -q xxx.zip -d /opt

5、拓展

打包压缩通常用于备份文件,文件的名字必须见名知意且应该带上时间、主机名之类 (文件名中不能带冒号,所以不要用冒号分隔时分秒)

时间命令date的选项:

-d 		# 根据你的描述显示日期
-s 		# 修改日期
%H 		# 小时,24小时制(00~23)
%M 		# 分钟(00~59)
%s 		# 从1970年1月1日00:00:00到目前经历的秒数
%S 		# 显示秒(00~59)
%T 		# 显示时间,24小时制(hh:mm:ss)
%d 		# 一个月的第几天(01~31)
%j 		# 一年的第几天(001~366)
%m 		# 月份(01~12)
%w 		# 一个星期的第几天(0代表星期天)
%W 		# 一年的第几个星期(00~53,星期一为第一天)
%y 		# 年的最后两个数字(1999则是99)
%Y	 	# 年,实际
%F 		# 显示日期(%Y-%m-%d)

备份示例:

[root@localhost ~]# tar czvf `date +%F`_bak.tar.gz /etc
[root@localhost ~]# tar czvf `date +%F_%H_%M_%S`_bak.tar.gz /etc
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值