高級文件操作
- 2019 年 10 月 3 日
- 筆記
輸入輸出的介紹
標準輸入(stdin) : 從該設配接受用戶輸入的數據
標準輸出(stdout) : 通過該設備向用戶輸入數據
標準錯誤: 通過該設備報告執行出錯資訊
類型 | 設備文件 | 文件描述符 | 默認設備 |
---|---|---|---|
標準輸入 | /dev/stdin | 0 | 鍵盤 |
標準輸出 | /dev/stdout | 1 | 顯示器 |
標準錯誤輸出 | /dev/stderr | 2 | 顯示器 |
輸入輸出重定向
通過命令,改變了標準輸入輸出的方向就是重定向
類型 | 操作符 | 用途 |
---|---|---|
重定向標準輸出 | > | 將命令執行的結果,重定向輸入的指定的文件中,而不再是顯示器 |
重定向標準輸出 | >> | 將命令執行的結果追加到指定的文件中 |
重定向標準輸入 | < | 將命令中接受到的路徑,由默認的鍵盤,更改為指定的文件 |
例: 將abc.log的內容當成輸入,重定向作為cat的輸出
cat < abc.log
標準錯誤: 通過該設備報告執行出錯資訊
類型 | 操作符 |
---|---|
重定向標準錯誤 | 2> |
重定向標準錯誤 | 2>> |
重定向標準輸出 和 標準錯誤 | 2>&1 或者 >& 或 &> |
重定向標準輸出 和 標準錯誤 到不同的文件 | >文件1 2>文件2 |
其中的2是標識符號
例: 將標準輸出和標準錯誤重定向到不同的文件
ls a.txt aaaa > /dev/null 2>err.txt 如果目標文件是不存在的,會被自動創建出來
例: 將標準輸出和標準錯誤重定向相同的文件
ls a.txt aaaa > /dev/null 2>&1 如果目標文件是不存在的,會被自動創建出來 1是可以省略的
兩個特殊的文件
- /dev/null 黑洞文件,傳遞進去的任何文件都會被當成垃圾丟棄
- /dev/zero 用來創建指定長度的文件,不指定就是空文件
其他命令
echo
在螢幕上顯示一段指定的文件或者內容
echo [-n] 字元串
-n: 不會進行換行
[root@ecs-t6-large-2-linux-20190824103606 ~]# echo 123 123 [root@ecs-t6-large-2-linux-20190824103606 ~]# echo -n 123 123[root@ecs-t6-large-2-linux-20190824103606 ~]#
管道及相關配套命令
操作符
一條豎線 |
作用
連接左右兩個命令,將左側命令的標準輸出,作為右側命令的標準輸入
有約束: 左側命令要支援標準輸出, 右側命令會支援標準輸入
格式:
cmd1 | cmd2 | cmd3 出 出入 入 cmd1的標準輸出 被cmd2當成標準輸入使用, cmd2的標準輸出被cmd3當成標準輸入使用
例: 過濾出 /etc/下包含 pass的行
[root@ecs-t6-large-2-linux-20190824103606 ~]# ls -l /etc | grep pass -rw-r--r-- 1 root root 1092 Sep 8 20:32 passwd -rw-r--r--. 1 root root 1135 Sep 8 20:29 passwd-
xargs參數
用途: 讓一些不支援管道的操作的命令行,可以使用管道
例1:
查看useradd命令所在的位置 [root@ecs-t6-large-2-linux-20190824103606 ~]# which useradd /usr/sbin/useradd 管道的右邊不支援標準輸入, 所以它是在對當前目錄使用 ls -lh [root@ecs-t6-large-2-linux-20190824103606 ~]# which useradd | ls -lh total 0 使用xargs 將左邊的標準輸入作為參數,添加的右側的命令中 [root@ecs-t6-large-2-linux-20190824103606 ~]# which useradd |xargs ls -lh -rwxr-x--- 1 root root 116K Mar 14 18:35 /usr/sbin/useradd
例2 可以實現分批刪除:
find 200個文件 | args rm -f
seq
作用: 列印出一串有序的數字
格式: seq [選項] [範圍]
- -s:指定分隔符
- -w:指定同等寬度
例:
[root@ecs-t6-large-2-linux-20190824103606 ~]# seq 3 1 2 3 [root@ecs-t6-large-2-linux-20190824103606 ~]# seq 2 3 2 3 [root@ecs-t6-large-2-linux-20190824103606 ~]# seq 5 2 10 5 7 9 [root@ecs-t6-large-2-linux-20190824103606 ~]# seq 1 -1 10 [root@ecs-t6-large-2-linux-20190824103606 ~]# 死循環 [root@ecs-t6-large-2-linux-20190824103606 ~]# seq 1 0 10 [root@ecs-t6-large-2-linux-20190824103606 ~]# [root@ecs-t6-large-2-linux-20190824103606 ~]# seq -s XXX 1 10 1XXX2XXX3XXX4XXX5XXX6XXX7XXX8XXX9XXX10 [root@ecs-t6-large-2-linux-20190824103606 ~]# seq -w 90 100 090 091 092 093 094 095 096 097 098 099 100
tr 轉換/刪除/壓縮
- 字元轉換工具
不能直接對文件進行操作
命令: tr set1 set2
作用: 用set2中的字元替換掉set1中相同的字元
echo 123456 | tr 345 abc 將左邊標準輸出的3 轉換為 a , 4->b , 5->c 只要相同, 一一轉換 將 /etc/hosts的內容轉大寫 tr 『[a-z]』 『[A-Z]』 < /etc/hosts 將A-Z 轉換成a-z
- 使用tr 刪除字元
格式 tr -d set
刪除和set相同的字元
[root@ecs-t6-large-2-linux-20190824103606 ~]# echo 123456 | tr -d 123 456
- 壓縮
將連續相同的字元壓縮成一個字元
echo 112233444555666 | tr -s 345 碰到連續的3, 就壓縮成1個3 碰到連續的4, 就壓縮成1個4 tr -s SET1 SET2 先替換為SET2再壓縮 echo 112233444555666 | tr -s 345 abc echo 112233444555666 | tr 345 abc | tr -s abc
排序sort
默認會按照每一行的第一個字元進行排序
- -n: numeric sort 按整數排序
- -r: reverse 遞減排序
- -k: key 指定某一列為排序鍵
- -t: field-separator 指定欄位分隔符
例:
[root@ecs-t6-large-2-linux-20190824103606 tmp]# cat sort_text dsd asd sad asa das da1 da3 d23 123 316 164 a45 d34 456 234 444 d64 as6 da4 d6a asd [root@ecs-t6-large-2-linux-20190824103606 tmp]# cat sort_text | sort -n a45 as6 asa asd asd d23 d34 d64 d6a da1 da3 da4 das dsd sad 123 164 234 316 444 456 [root@ecs-t6-large-2-linux-20190824103606 tmp]# cat sort_text | sort -n -k2 123 164 234 316 444 456 a45 as6 asa asd asd d23 d34 d64 d6a da1 da3 da4 das dsd sad
例2: 按照表格的方式展示 /etc/passwd 的內容
sort /etc/passwd | column -t -s ":" [root@ecs-t6-large-2-linux-20190824103606 tmp]# sort /etc/passwd | column -t -s ":" adm x 3 4 adm /var/adm /sbin/nologin bin x 1 1 bin /bin /sbin/nologin daemon x 2 2 daemon /sbin /sbin/nologin dbus x 81 81 System message bus / /sbin/nologin ....
uniq命令
刪除重複的記錄,通常和sort連用
它只會去除連續出現的相同的記錄,針對如下記錄的結果操作如下 123 123123 123123 123 123 [root@ecs-t6-large-2-linux-20190824103606 tmp]# cat text | uniq 123 123123 123 先排序,再去重的效果如下 [root@ecs-t6-large-2-linux-20190824103606 tmp]# sort -r text | uniq 123123 123
- -c: count 顯示文件中連續出現的次數
[root@ecs-t6-large-2-linux-20190824103606 tmp]# cat text | uniq -c 1 123 122 123123 1 123
- -u:unique 只顯示不重複的行
cat tt | uniq –u - -d:repead 只顯示重複的行
cat tt | uniq -d
wc 命令
wc(字數統計)命令
格式:wc [選項]… 目標文件…
- -l:lines 統計行數
- -w:words 統計字數 (前後都是空白的一組字元)
- -c:bytes 統計字元數(可見和不可見的字元)
例1:
[root@ecs-t6-large-2-linux-20190824103606 ~]# wc /etc/passwd 23 44 1092 /etc/passwd 23行 44字 1092位元組
例2:
[root@ecs-t6-large-2-linux-20190824103606 ~]# echo 123 | wc -l 1
cut命令
從指定的文本或者文本流中提取指定的列
格式: cut [可選項] 範圍 文本/文本流
可選項
- -c: 從指定位置提取
- -f: fields 僅僅列印指定的列
- -d: delimiter 指定分隔符, 默認是 tab
提取範圍
- n: 第n列
- n-: 從n到列尾
- -m: 從開頭到m
- n,m:第n和第m項
- n-m: 從n到m項
例1:
[root@ecs-t6-large-2-linux-20190824103606 tmp]# ll total 40 drwxr-xr-x 2 root root 4096 Sep 4 19:35 hsperfdata_root drwxr-xr-x 2 root root 4096 Sep 4 19:35 jetty-0.0.0.0-9998-browser-_browser-any-6157528924019481141.dir -rw-r--r-- 1 root root 84 Sep 9 21:46 sort_text drwx------ 3 root root 4096 Sep 4 15:28 systemd-private-c3ac023caef0453f85963d47758cc2cf-ntpd.service-LXVmNS -rw-r--r-- 1 root root 862 Sep 9 21:54 text drwx------ 2 root root 4096 Sep 4 20:22 tmp.2RTupK8p7R prw-r--r-- 1 root root 0 Sep 10 08:53 wrapper-4156-1-in prw-r--r-- 1 root root 0 Sep 10 08:53 wrapper-4156-1-out -rw------- 1 root root 13478 Sep 4 20:16 yum_save_tx.2019-09-04.20-16.5iMOEY.yumtx 提取第十列 [root@ecs-t6-large-2-linux-20190824103606 tmp]# ll | cut -c 10 x x - - - - - - - 提取20-40 列 [root@ecs-t6-large-2-linux-20190824103606 tmp]# who | cut -c 20-40 2019-09-10 08:46 (
數據的提取和過濾
grep 數據提取程式
用途: 在文件中查找查找並顯示包含指定字元串的行
格式: grep [選項] 模式 目標文件
- -i: ignore case 查找時忽略大小寫
- -v: invert match 反轉查找,輸出和模式不相符的行
- -w: word regexp 按整字查找, 數字,字母,下劃線 連在一起就是整字
- -n: line number 顯示符合模式要求的行號
- -r: 遞歸查找所有文件
- -o: 僅僅輸出匹配到的字元
模式
a :包含a的行 ^...: 以...開頭 ...$: 以...結尾
例: 統計文件中某個字的數量
grep -o "abc" abc.txt | wc -l
文件的差異對比
比較兩個文件之間的差異
輸出結果為兩個文件的不同之處
diff [文件1] [文件2]
沒有任何輸出說明文件不一樣