Linux基础之文件管理(高级)上等相关内容-96

文件管理之(高级)

一 文本处理三剑客命令初探

1.1 sed

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

# 用法
sed 选项 '定位+命令' 文件路径
# 选项
-n 取消默认输出
-r 支持扩展正则元字符(由于尚未学习正则,所以此处暂作了解)
-i 立即编辑文件
# 定位
行定位:
1定位到第一行
1,3代表从第1行到第3行
不写定位代表定位所有行
正则表达式定位:
/egon/ 包含egon的行
/^egon/ 以egon开头的行
/egon$/以egon结尾的行
数字+正则表达式定位
"1,8p"代表打印1到8行,
"1,/egon/p"则代表取从第1行到首次匹配到/egon/的行
# 命令
d
p
s///g
命令可以用;号连接多多条,如1d;3d;5d代表删除1,3,5行

# =========================》用法示例:p与d
[root@localhost ~]# sed '' a.txt
egon1111
22222egon
3333egon33333
4444xxx44444
5555xxx55555xxxx555xxx
6666egon6666egon666egon
[root@localhost ~]# sed -n '' a.txt
[root@localhost ~]#
[root@localhost ~]# sed -n '1,/xxx/p' a.txt
egon1111
22222egon
3333egon33333
4444xxx44444
[root@localhost ~]# sed '1,/xxx/d' a.txt
5555xxx55555xxxx555xxx
6666egon6666egon666egon
[root@localhost ~]# sed '1d;3d;5d' a.txt
22222egon
4444xxx44444
6666egon6666egon666egon

# =========================》用法示例: s///g
[root@localhost ~]# cat a.txt
egon1111
22222egon
3333egon33333
4444xxx44444
5555xxx55555xxxx555xxx
6666egon6666egon666egon
[root@localhost ~]# sed 's/egon/BIGEGON/g' a.txt # 把所有行的所有的egon都换成
BIGEGON
BIGEGON1111
22222BIGEGON
3333BIGEGON33333
4444xxx44444
5555xxx55555xxxx555xxx
6666BIGEGON6666BIGEGON666BIGEGON
[root@localhost ~]#
[root@localhost ~]# sed '/^egon/s/egon/GAGAGA/g' a.txt # 以egon开头的行中的egon换
成GAGAGA
GAGAGA1111
22222egon
3333egon33333
4444xxx44444
5555xxx55555xxxx555xxx
6666egon6666egon666egon
[root@localhost ~]# sed '6s/egon/BIGEGON/' a.txt # 只把第6行的egon换成BIGEGON,加
上g代表???
egon1111
22222egon
3333egon33333
4444xxx44444
5555xxx55555xxxx555xxx
6666BIGEGON6666egon666egon
[root@localhost ~]#
[root@localhost ~]# sed '1,3s/egon/BIGEGON/g' a.txt # 把1到3行的egon换成BIGEGON
BIGEGON1111
22222BIGEGON
3333BIGEGON33333
4444xxx44444
5555xxx55555xxxx555xxx
6666egon6666egon666egon
[root@localhost ~]# cat a.txt | sed '1,5d' # sed也支持管道
6666egon6666egon666egon
# 加上-i选项,直接修改文件,通常会在调试完毕确保没有问题后再加-i选项

1.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 !~ /正则/ # 第一段内容没有匹配成功正则
比较运算:
NR >= 3 && NR <=5 # 3到5行
$1 == "root" # 第一段内容等于root

# action可以是
print $1,$3

# 用法示例
[root@localhost ~]# cat a.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
[root@localhost ~]# awk -F: '/^root/{print $1,$3}' a.txt
root 0
[root@localhost ~]# awk -F: '$1 ~ /^d/{print $1,$3}' a.txt
daemon 2
[root@localhost ~]# awk -F: '$1 !~ /^d/{print $1,$3}' a.txt
root 0
bin 1
adm 3
lp 4
[root@localhost ~]# awk -F: 'NR>3{print $1}' a.txt
adm
lp
[root@localhost ~]# awk -F: '$1 == "lp"{print $0}' a.txt
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
[root@localhost ~]#
[root@localhost ~]# cat a.txt | awk -F: '{print $1}' # awk也支持管道
root
bin
daemon
adm
lp
[root@localhost ~]#

1.3 grep

grep擅长过滤内容

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

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

# 示例
[root@localhost ~]# grep '^root' /etc/passwd
root:x:0:0:root:/root:/bin/bash

[root@localhost ~]# grep -n 'bash$' /etc/passwd
1:root:x:0:0:root:/root:/bin/bash
44:egon:x:1000:1000:egon:/home/egon:/bin/bash

[root@localhost ~]# grep -rl 'root' /etc

# grep也支持管道,我们可以发现三剑客命令都支持管道
[root@localhost ~]# ps aux |grep ssh
root 968 0.0 0.2 112908 4312 ? Ss 14:05 0:00 /usr/sbin/sshd
-D
root 1305 0.0 0.3 163604 6096 ? Ss 14:05 0:00 sshd:
root@pts/0
root 1406 0.0 0.3 163600 6240 ? Ss 14:05 0:00 sshd:
root@pts/1
root 2308 0.0 0.0 112724 984 pts/1 R+ 15:30 0:00 grep --
color=auto ssh
[root@localhost ~]# ps aux |grep [s]sh
root 968 0.0 0.2 112908 4312 ? Ss 14:05 0:00 /usr/sbin/sshd
-D
root 1305 0.0 0.3 163604 6096 ? Ss 14:05 0:00 sshd:
root@pts/0
root 1406 0.0 0.3 163600 6240 ? Ss 14:05 0:00 sshd:
root@pts/1

二 文件管理之:文件查找

一、查看命令所属文件

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

二、查找文件

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

按文件名:

[root@localhost ~]# find /etc -name "ifcfg-eth0"
[root@localhost ~]# find /etc -iname "ifcfg-eth0" # -i忽略大小写
[root@localhost ~]# find /etc -iname "ifcfg-eth*"

按文件大小:

[root@localhost ~]# find /etc -size +3M # 大于3M
[root@localhost ~]# find /etc -size 3M
[root@localhost ~]# find /etc -size -3M
[root@localhost ~]# find /etc -size +3M -ls # -ls找到的处理动作

指定查找的目录深度:

-maxdepth levels
[root@localhost ~]# find / -maxdepth 5 -a -name "ifcfg-eth0" # -a并且,-o或者,
不加-a,默认就是-a

按时间找(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 egon # 属主是egon的文件
[root@localhost ~]# find /home -group it # 属组是it组的文件
[root@localhost ~]# find /home -user egon -group it
[root@localhost ~]# find /home -user egon -a -group it # 同上意思一样
[root@localhost ~]# find /home -user egon -o -group 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@localhost ~]# 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@localhost ~]# sz bak.tar.gz

(2)上传

rz命令

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

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

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

linux中用
   0代表标准输入
   1代表标准正确输出
   2代表标准错误输出

输出重定向:
正常输出是把内容输出到显示器上,而输出重定向是把内容输出到文件中,>代表覆盖,>>代表追加
Ps:标准输出的1可以省略
例如:ifconfig > test.log 即把ifconfig执行显示的正确内容写入test.log.当前页面不再显示执行结果。
注意:错误输出重定向>与>>后边不要加空格

注意:
1、下述两个命令作用相同
命令 >>file.log 2>&1
   命令 &>>file.log
2、正确日志和错误日志分开保存
命令 >>file1.log 2>>file2.log
3、系统有个常见用法 ls &>/dev/null 正确输出或错误输出结果都不要。(null可以理解为黑洞或
垃圾站)
输入重定向
#没有改变输入的方向,默认键盘,此时等待输入
[root@egon ~]# tr 'N' 'n'
No
no
[root@egon ~]# tr 'N' 'n' < file.txt
#没有改变输入的方向,默认键盘,此时等待输入
[root@egon ~]# grep 'root'
oldboy
root
root
[root@egon ~]# grep 'root' < /etc/passwd
root:x:0:0:root:/root:/bin/bash
# 读写块设备
[root@egon ~]# dd if=/dev/zero of=/file1.txt bs=1M count=20
[root@egon ~]# dd </dev/zero >/file2.txt bs=1M count=20
# mysql如何恢复备份,了解即可,不用关注。
[root@qls ~]# mysql -uroot -p123 < bbs.sql

 

原文地址:https://www.cnblogs.com/usherwang/p/14046681.html