在學習Linux系統中,我們要想看文件,一般就cat命令執行,執行大文件時,就不好使了;
less和more可以實現你心中的夢想,可以幫你查看大文件內容,
more命令:
[root@localhost ~]# more /etc/man_db.conf
#
#
# This file is used by the man-db package to configure the man and cat paths.
# It is also used to provide a manpath for those without one by examining
# their PATH environment variable. For details see the manpath(5) man page.
#
空格鍵:代表向下翻一頁
enter:代表向下翻一行
/字符串:代表在這個顯示的內容當中,向下查詢“字符串”這個關鍵字
:f :立即顯示文件名以及目前顯示的行數;
q: 代表立刻離開more,不在顯示該文本內容。
b:代表往回翻頁。
less命令:
lessd的用法比起more又更加彈性,在more我們沒辦法往前翻,只能往後看,然後自動退出,man命令就是調用less命令來實現的
[root@localhost ~]# less /etc/rc.d/init.d/functions
空格鍵:向下翻動一頁
k:翻下一行
j:向上翻一行
/字符串:向下查詢
?字符串:向上查詢
n:正向
N:反向
q:離開
文本截取內容head和tail命令
head [-n number] 文件
-n:後面接數字,代表幾行的意思
[root@localhost ~]# head /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
默認情況爲10行,顯示。
[root@localhost ~]# head -n 20 /etc/man_db.conf
這是顯示20行
tail命令:
tail [-n number] 文件
-n:後面接數字,代表顯示後面幾行的意思
-f:表示持續檢測後面接的文件名,要Ctrl-c纔會結束tail的檢測
[root@localhost ~]# tail /etc/passwd
qemu:x:107:107:qemu user:/:/sbin/nologin
chrony:x:990:985::/var/lib/chrony:/sbin/nologin
當我們不跟數字時,默認從文件尾部輸出10行,
要是想看文件最後3行,可以這樣操作
[root@localhost ~]# tail -n 3 /etc/passwd
ntp:x:38:38::/etc/ntp:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
aliyun:x:1000:1000:aliyun:/home/aliyun:/bin/bash
cut命令:剪切文本的字符段,達到自己想要的結果
cut [OPTION]... [FILE]...
-d DELIMITER: 指明分隔符,默認tab
-f FILEDS:
#: 第#個字段
#,#[,#]:離散的多個字段,例如1,3,6
#-#:連續的多個字段, 例如1-6
混合使用:1-3,7
-c按字符切割
--output-delimiter=STRING指定輸出分隔符
例:找出/etc/passwd文件中的uid;首先我們分析,可以用冒號:作爲分隔符,這樣可以分出7個字段,然後我們取出第3個字段,表示的就是uid;
[root@localhost ~]# cut -d : -f 3 < /etc/passwd
0
1
2
取出第3,4,7段時,應該這樣表達;-d : -f3,4,7
[root@localhost ~]# cat /etc/passwd | cut -d: -f3,4,7
0:0:/bin/bash
1:1:/sbin/nologin
2:2:/sbin/nologin
取出連續3,567,可以用。-d : -f 3,5-7
[root@localhost ~]# cat /etc/passwd | cut -d: -f3,5-7
0:root:/root:/bin/bash
1:bin:/bin:/sbin/nologin
要是按字符切割:表示的就是從行,最前面,到的第幾個字符
[root@localhost ~]# cat txt
123456789
123456789
123456789
[root@localhost ~]# cat txt | cut -c 5
替換符號,,把取出來的中間換上*號
[root@localhost ~]# cut -d: -f3,4 --output-delimiter=* < /etc/passwd
0*0
1*1
2*2
paste (合併兩個文件同行號的列到一行)
-d:分隔符:指定分隔符,默認用tab
-s:所有行合成一行顯示
paste f1 f2
paste -s f1 f2
[root@localhost ~]# paste -d "+" -s txt txt1
123456789+ 123456789+ 123456789
121121212121+232323232323+44444444444455+66666666677777
指明分隔符爲"+"然後每個文件所有行都合成一行。
wc命令:收集文本的統計數據(行,字節,單詞)
[root@localhost ~]# cat /etc/passwd | wc -l
44 行
[root@locahost ~]# cat /etc/passwd |wc -w
89 單詞
[root@localhost ~]# cat /etc/passwd | wc
44 89 2331
[root@localhost ~]# cat /etc/passwd | wc -c
2331 字節、
文本排序sort命令:
sort[options] file
-r :執行反方向
-n:安數字大小
-f:忽略字符串中的字符大小寫
-u:選項刪除輸出中的重複行
-t c 選項使用c作爲字段定界
-k X 選項按照使用c字符分隔的X列來整理能使用多次
uniq命令:從輸入中刪除重複的前後相接的行
uniq [option] ...[file]...
-c:顯示每行重複出現的次數
-d:僅顯示重複過的行
-u:僅顯示不曾重複的行;連續且完全相同爲爲重複
常和sort命令一起配合使用:
sort userlist.txt | uniq -c
[root@localhost ~]# sort -r txt | uniq -c
4 ssssss
1 eeeeee
1 dddddd
4 aaaaaa
diff命令比較兩個文件的不同之處;
[root@localhost ~]# diff -u f1 f2 > diff.log打個補丁輸出到diff。log文件
[root@localhost ~]# rm -f f2
[root@localhost ~]# patch -b f1 diff.log
patching file f1
[root@localhost ~]# mv f1.orig f1
[root@localhost ~]#
[root@localhost ~]# mv f1 f2