0
点赞
收藏
分享

微信扫一扫

佳豪哥哥教你学Linux的第九天

文件管理命令

文件过滤命令

|:管道符
# 作用:将管道符左边命令的标准输出,交给管道符右边命令的标准输入来处理
grep:过滤输出内容
^:以...开头
$:以...结尾
*:匹配*前面的内容至少0次或多次
.:匹配任意字符
## 选项
-n:打印行号 number
-A:after 查看过滤内容的后N行
-B:before 查看过滤内容的前N行
-C:center 查看过滤内容的前、后N行
-v:取反
-E:可以过滤多个结果
-o:只打印出过滤内容
-i:不区分大小写

##举例

#^
[root@wujiahao /abc]# vi wjh.txt
[root@wujiahao /abc]# grep '^#' wjh.txt
#1.I hope you are the time fixed lover
#2.I love you for my life past.
#3.No one and you
#4.Want perfect, always not perfect.
#5.Time is we do not come loose.
#6.However long the night, the dawn will break.
#7.Time always save the best for last.
#8.Love is not the strong vow but the simple accompany .

[root@wu /abc]# grep '^我' wjh.txt
我爱你,爱了整整一个曾经。

#$
[root@wu /abc]# grep '。$' wjh.txt
愿你是我时光盗不走的爱人。
我爱你,爱了整整一个曾经。
无人及你。
想要的完美,始终不完美。
时光不老,我们不散。
不管黑夜有多长,天亮总会到来。
时间总把最好的人留到最后。
真爱不是轰轰烈烈的誓言,而是平平淡淡的陪伴。

[root@wu /abc]# grep '来。$' wjh.txt
不管黑夜有多长,天亮总会到来。

# . 和 *
[root@wu /abc]# grep 's.' wjh.txt
#2.I love you for my life past.
#4.Want perfect, always not perfect.
#5.Time is we do not come loose.
#7.Time always save the best for last.
#8.Love is not the strong vow but the simple accompany..

[root@wu /abc]# grep 's' wjh.txt
#2.I love you for my life past.
#4.Want perfect, always not perfect.
#5.Time is we do not come loose.
#7.Time always save the best for last.
#8.Love is not the strong vow but the simple accompany.

# -n
[root@wu /abc]# grep -n 's' wjh.txt
3:#2.I love you for my life past.
7:#4.Want perfect, always not perfect.
9:#5.Time is we do not come loose.
13:#7.Time always save the best for last.
15:#8.Love is not the strong vow but the simple accompany.
# -A -B -C
[root@wu /abc]# grep -A 3 '管' wjh.txt
不管黑夜有多长,天亮总会到来。
#7.Time always save the best for last.
时间总把最好的人留到最后。
#8.Love is not the strong vow but the simple accompany.
[root@wu /abc]# grep -B 3 '管' wjh.txt
#5.Time is we do not come loose.
时光不老,我们不散。
#6.However long the night, the dawn will break.
不管黑夜有多长,天亮总会到来。

[root@wu /abc]# grep -B 3 -A 3 '管' wjh.txt
#5.Time is we do not come loose.
时光不老,我们不散。
#6.However long the night, the dawn will break.
不管黑夜有多长,天亮总会到来。
#7.Time always save the best for last.
时间总把最好的人留到最后。
#8.Love is not the strong vow but the simple accompany.

[root@wu /abc]# grep -C 3 '管' wjh.txt
#5.Time is we do not come loose.
时光不老,我们不散。
#6.However long the night, the dawn will break.
不管黑夜有多长,天亮总会到来。
#7.Time always save the best for last.
时间总把最好的人留到最后。
#8.Love is not the strong vow but the simple accompany.

[root@wu /abc]# grep -A 5 -B 2 '管' wjh.txt
时光不老,我们不散。
#6.However long the night, the dawn will break.
不管黑夜有多长,天亮总会到来。
#7.Time always save the best for last.
时间总把最好的人留到最后。
#8.Love is not the strong vow but the simple accompany.
真爱不是轰轰烈烈的誓言,而是平平淡淡的陪伴。

# -v
[root@wu /abc]# grep -v '管' wjh.txt
#1.I hope you are the time fixed lover
愿你是我时光盗不走的爱人。
#2.I love you for my life past.
我爱你,爱了整整一个曾经。
#3.No one and you
无人及你。
#4.Want perfect, always not perfect.
想要的完美,始终不完美。
#5.Time is we do not come loose.
时光不老,我们不散。
#6.However long the night, the dawn will break.
#7.Time always save the best for last.
时间总把最好的人留到最后。
#8.Love is not the strong vow but the simple accompany.
真爱不是轰轰烈烈的誓言,而是平平淡淡的陪伴。

# -E
[root@wu /abc]# grep -E '管|my' wjh.txt
#2.I love you for my life past.
不管黑夜有多长,天亮总会到来。
[root@wu /abc]# grep -E '管|my|but|人' wjh.txt
愿你是我时光盗不走的爱人。
#2.I love you for my life past.
无人及你。
不管黑夜有多长,天亮总会到来。
时间总把最好的人留到最后。
#8.Love is not the strong vow but the simple accompany.

# -o
[root@wu /abc]# grep '人' wjh.txt
愿你是我时光盗不走的爱人。
无人及你。
时间总把最好的人留到最后。

root@wu /abc]# grep -o '人' wjh.txt




# -i
[root@wu /abc]# grep 'l' wjh.txt
#1.I hope you are the time fixed lover
#2.I love you for my life past.
#4.Want perfect, always not perfect.
#5.Time is we do not come loose.
#6.However long the night, the dawn will break.
#7.Time always save the best for last.
#8.Love is not the strong vow but the simple accompany

[root@wu /abc]# grep -i 'L' wjh.txt
#1.I hope you are the time fixed lover
#2.I love you for my life past.
#4.Want perfect, always not perfect.
#5.Time is we do not come loose.
#6.However long the night, the dawn will break.
#7.Time always save the best for last.
#8.Love is not the strong vow but the simple accompany.

# [a-zA-Z]
[root@wu /abc]# grep '[a-z]' wjh.txt
#1.I hope you are the time fixed lover
#2.I love you for my life past.
#3.No one and you
#4.Want perfect, always not perfect.
#5.Time is we do not come loose.
#6.However long the night, the dawn will break.
#7.Time always save the best for last.
#8.Love is not the strong vow but the simple accompany.

[root@wu /abc]# grep '[A-Z]' wjh.txt
#1.I hope you are the time fixed lover
#2.I love you for my life past.
#3.No one and you
#4.Want perfect, always not perfect.
#5.Time is we do not come loose.
#6.However long the night, the dawn will break.
#7.Time always save the best for last.
#8.Love is not the strong vow but the simple accompany.

[root@wu /abc]# grep '[a-zA-Z]' wjh.txt
#1.I hope you are the time fixed lover
#2.I love you for my life past.
#3.No one and you
#4.Want perfect, always not perfect.
#5.Time is we do not come loose.
#6.However long the night, the dawn will break.
#7.Time always save the best for last.
#8.Love is not the strong vow but the simple accompany.

root@wu /abc]# grep -E 'a|z' wjh.txt
#1.I hope you are the time fixed lover
#2.I love you for my life past.
#3.No one and you
#4.Want perfect, always not perfect.
#6.However long the night, the dawn will break.
#7.Time always save the best for last.
#8.Love is not the strong vow but the simple accompany.

[root@wu /abc]# grep -E '[a-c]' wjh.txt
#1.I hope you are the time fixed lover
#2.I love you for my life past.
#3.No one and you
#4.Want perfect, always not perfect.
#5.Time is we do not come loose.
#6.However long the night, the dawn will break.
#7.Time always save the best for last.
#8.Love is not the strong vow but the simple accompany.

[root@wu /abc]# grep -E '[a-Z]' wjh.txt
#1.I hope you are the time fixed lover
#2.I love you for my life past.
#3.No one and you
#4.Want perfect, always not perfect.
#5.Time is we do not come loose.
#6.However long the night, the dawn will break.
#7.Time always save the best for last.
#8.Love is not the strong vow but the simple accompany.

[root@wu /abc]# grep -E '[a-zA-Z]' wjh.txt
#1.I hope you are the time fixed lover
#2.I love you for my life past.
#3.No one and you
#4.Want perfect, always not perfect.
#5.Time is we do not come loose.
#6.However long the night, the dawn will break.
#7.Time always save the best for last.
#8.Love is not the strong vow but the simple accompany.

[root@wu /abc]# grep '^[a-zA-Z]' wjh.txt
W2.I love you for my life past.
A时光不老,我们不散。
[root@wu /abc]# grep '^[a-Z]' wjh.txt
W2.I love you for my life past.
A时光不老,我们不散。
[root@wu /abc]#

文件的上传和下载命令

# 如何把windows的文件上传到虚拟机里
rz
# 如何把虚拟机的文件下载到windows里
sz 文件名
## 需要安装lrzsz
#rz
total 8
-rw-r--r--. 1 root root 615 Mar 24 14:24 wjh.txt
-rw-r--r--. 1 root root 10 Mar 24 14:29 wujiahao.txt
[root@wu /abc]# cat wujiahao.txt
4
3
2
#sz
[root@wu /abc]# wget http://124.223.208.171/wp-content/uploads/2022/03/background-2-scaled.jpg
--2022-03-24 14:37:48-- http://124.223.208.171/wp-content/uploads/2022/03/background-2-scaled.jpg
Connecting to 124.223.208.171:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 420318 (410K) [image/jpeg]
Saving to: ‘background-2-scaled.jpg’

100%[=======================>] 420,318 1.62MB/s in 0.2s

2022-03-24 14:37:49 (1.62 MB/s) - ‘background-2-scaled.jpg’ saved [420318/420318]

[root@wu /abc]# ll
total 420
-rw-r--r--. 1 root root 420318 Mar 24 11:35 background-2-scaled.jp
-rw-r--r--. 1 root root 615 Mar 24 14:24 wjh.txt
-rw-r--r--. 1 root root 10 Mar 24 14:29 wujiahao.txt
[root@wu /abc]# sz background-2-scaled.jpg


wget 下载地址
-O:指定下载的位置和文件的名字

文件查找命令

type -a 只针对系统内置命令
## 查找命令位置的命令 which
which 只能查找命令
## 举例
[root@wu /]# which rm
alias rm='rm -i'
/usr/bin/rm
[root@wu /]# which mkdir
/usr/bin/mkdir

字符处理命令-排序

sort
## 语法
sort [选项]... File...
## 选项
-t:指定分隔符
-k:指定按照某一列,进行排序
-n:按照阿拉伯数字排序
-r:reverse 倒叙排序
## 举例
[root@wu /]# cat >> wu.txt <<EOF
> c:5
> x:8
> o:4
> a:9
> d:7
> EOF

[root@wu /]# cat wu.txt
c:5
x:8
o:4
a:9
d:7

[root@wu /]# sort wu.txt
a:9
c:5
d:7
o:4
x:8
## 注意:默认按照每一行的第一个字符进行排序,如果字符相同就往后推,字母按照a-z的顺序排序,排序不修改源
文件内容
# -t -k
[root@wu /]# sort -t ':' wu.txt
a:9
c:5
d:7
o:4
x:8

[root@wu /]# sort -t ':' -k 2 wu.txt
f:1
b:2
o:4
c:5
d:7
x:8
a:9

# -n
[root@wu /]# sort -t ':' -k 2 -n wu.txt
f:1
b:2
o:4
c:5
d:7
x:8
a:9

# -r
[root@wu /]# sort -rnt ':' -k 2 wu.txt
a:9
x:8
d:7
c:5
o:4
b:2
f:1

## 练习题

cat >> paixuti.txt <<EOF
192.168.3.1 00:0F:AF:81:19:1F
192.168.3.2 00:0F:AF:85:6C:25
192.168.3.3 00:0F:AF:85:70:42
192.168.2.20 00:0F:AF:85:55:DE
192.168.2.21 00:0F:AF:85:6C:09
192.168.2.22 00:0F:AF:85:5C:41
192.168.0.151 00:0F:AF:85:6C:F6
192.168.0.152 00:0F:AF:83:1F:65
192.168.0.153 00:0F:AF:85:70:03
192.168.1.10 00:30:15:A2:3B:B6
192.168.1.11 00:30:15:A3:23:B7
192.168.1.12 00:30:15:A2:3A:A1
192.168.1.1 00:0F:AF:81:19:1F
192.168.2.2 00:0F:AF:85:6C:25
192.168.3.3 00:0F:AF:85:70:42
192.168.2.20 00:0F:AF:85:55:DE
192.168.1.21 00:0F:AF:85:6C:09
192.168.2.22 00:0F:AF:85:5C:41
192.168.0.151 00:0F:AF:85:6C:F6
192.168.1.152 00:0F:AF:83:1F:65
192.168.0.153 00:0F:AF:85:70:03
192.168.3.10 00:30:15:A2:3B:B6
192.168.1.11 00:30:15:A3:23:B7
192.168.3.12 00:30:15:A2:3A:A1
EOF
[root@localhost ~]# sort -t '.' -k 4 -n paixuti.txt
192.168.1.1 00:0F:AF:81:19:1F
192.168.3.1 00:0F:AF:81:19:1F
192.168.2.2 00:0F:AF:85:6C:25
192.168.3.2 00:0F:AF:85:6C:25
192.168.3.3 00:0F:AF:85:70:42
192.168.3.3 00:0F:AF:85:70:42
192.168.1.10 00:30:15:A2:3B:B6
192.168.3.10 00:30:15:A2:3B:B6
192.168.1.11 00:30:15:A3:23:B7
192.168.1.11 00:30:15:A3:23:B7
192.168.1.12 00:30:15:A2:3A:A1
192.168.3.12 00:30:15:A2:3A:A1
192.168.2.20 00:0F:AF:85:55:DE
192.168.2.20 00:0F:AF:85:55:DE
192.168.1.21 00:0F:AF:85:6C:09
192.168.2.21 00:0F:AF:85:6C:09
192.168.2.22 00:0F:AF:85:5C:41
192.168.2.22 00:0F:AF:85:5C:41
192.168.0.151 00:0F:AF:85:6C:F6
192.168.0.151 00:0F:AF:85:6C:F6
192.168.0.152 00:0F:AF:83:1F:65
192.168.1.152 00:0F:AF:83:1F:65
192.168.0.153 00:0F:AF:85:70:03
192.168.0.153 00:0F:AF:85:70:03

佳豪哥哥教你学Linux的第九天_文件查找

举报

相关推荐

0 条评论