1. 程式人生 > >高階檔案操作

高階檔案操作

輸入輸出的介紹

標準輸入(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]

沒有任何輸出說明檔案不一