Linux - 檔案管理
Linux - 檔案管理
一:文字處理三劍客
sed
流式編輯器,主要擅長對檔案的編輯操作
可以事先定製好編輯檔案的指令,然後讓sed自動完成對檔案的整體編輯
用法
sed 選項 '定位+命令' 檔案路徑
選項
選項 | 作用 |
---|---|
-n | 取消預設輸出(不會輸出檔案原內容) |
-r | 支援擴充套件正則元字元 |
-i | 立即編輯檔案 |
定位
行定位:
1
定位到第1行1,3
代表從第1行到第3行不寫
定位 代表定位所有行
正則表示式定位:
/darker/
包含darker的行/^darker/
以darker開頭的行/darker$/
以darker結尾的行
數字+正則表示式定位:
1,8p
1,/darker/p
則代表取從第1行到首次匹配到/darker/的行
命令
命令 | 作用 |
---|---|
d | 刪除 |
p | 複製 |
s/before/after/g before:替換前的內容 after:替換後的內容 |
替換 |
命令可以用;
連線多條
測試
# 建立資料夾test [root@localhost ~]# mkdir test # 進入資料夾 [root@localhost test]# cd test # 建立檔案1.txt [root@localhost test]# touch 1.txt # 進入1.txt進行編輯 [root@localhost test]# vim1.txt # 按 i 進入插入模式 i # 新增文字 darkerline1 linedarker22 line333 line4444darker linedarker55555 line666cm666cm # 退出並儲存 Esc :wq # 正常檢視1.txt內容 [root@localhost test]# cat 1.txt darkerline1 linedarker22 line333 line4444darker linedarker55555 line6 66cm666cm # 預設匹配所有行,顯示1.txt的全部內容 [root@localhost test]# sed'' 1.txt darkerline1 linedarker22 line333 line4444darker linedarker55555 line666cm666cm # 取消預設輸出,預設匹配所有行,顯示a.txt的全部內容 [root@localhost test]# sed -n '' 1.txt # 沒有輸出 # 定位1.txt第1行到第5行 進行復制(p),顯示原文內容 以及複製內容 [root@localhost test]# sed '1,5p' 1.txt darkerline1 darkerline1 linedarker22 linedarker22 line333 line333 line4444darker line4444darker linedarker55555 linedarker55555 line666cm666cm # 從第1行開始匹配到第1個包含darker的行進行復制 [root@localhost test]# sed '1,/darker/p' 1.txt darkerline1 darkerline1 linedarker22 linedarker22 line333 line4444darker linedarker55555 line666cm666cm # 取消預設輸出從第一行匹配到第一個包含xxx的行進行復制 [root@localhost test]# sed -n '1,/darker/p' 1.txt darkerline1 linedarker22 # 從第1行開始匹配到第1個包含333的行進行復制 [root@localhost test]# sed '1,/333/p' 1.txt darkerline1 darkerline1 linedarker22 linedarker22 line333 line333 line4444darker linedarker55555 line666cm666cm # 從第1行開始匹配到第1個包含darker的行刪除(從第1行刪到包含darker的行) [root@localhost test]# sed '1,/darker/d' 1.txt line333 line4444darker linedarker55555 line666cm666cm # 刪除第1行、第3行、第5行,輸出其他行 [root@localhost test]# sed '1d;3d;5d' 1.txt linedarker22 line4444darker line666cm666cm # 把所有行的所有的darker 替換成DARKER,然後輸出 [root@localhost test]# sed 's/darker/DARKER/g' 1.txt DARKERline1 lineDARKER22 line333 line4444DARKER lineDARKER55555 line666cm666cm # 把所有以darker開頭的 行中的darker 替換成Start,然後輸出 [root@localhost test]# sed '/^darker/s/darker/Start/g' 1.txt Startline1 linedarker22 line333 line4444darker linedarker55555 line666cm666cm # 只把第6行的首個cm換成HIGH [root@localhost test]# sed '6s/cm/HIGH/' 1.txt darkerline1 linedarker22 line333 line4444darker linedarker55555 line666HIGH666cm # 只把第6行的所有cm換成HIGH(加上g代表全部替換) [root@localhost test]# sed '6s/cm/HIGH/g' 1.txt darkerline1 linedarker22 line333 line4444darker linedarker55555 line666HIGH666HIGH # 把1到3行的darker換成WOW [root@localhost test]# sed '1,3s/darker/WOW/g' 1.txt WOWline1 lineWOW22 line333 line4444darker linedarker55555 line666cm666cm # 刪除2-5行,複製並輸出第1行和第6行(sed也支援管道操作) [root@localhost test]# cat 1.txt | sed '1p;2d;3d;4d;5d;6p' darkerline1 darkerline1 line666cm666cm line666cm666cm
sed命令加上
-i
選項,可以直接修改檔案,通常會在除錯完畢確保沒有問題後
再加-i選項
awk
主要用於處理有格式的文字,例如/etc/passwd這種
把文件內容切成一段一段
用法
awk 選項 'pattern{action}' 檔案路徑
選項
選項 | 作用 |
---|---|
-F | 指定分隔符(預設是空格 ) |
工作流程
awk -F: '{print $1,$3}' /etc/passwd
① awk會讀取檔案的1行內容然後賦值給$0
② 然後awk會以-F
指定的分隔符將該行切分成n段,最多可以達到100段,第一段給$1
,第二段給$2
,以此類推
③print
輸出該行的第1段和第3段,逗號代表輸出分隔符,預設與-F
保持一致
④ 重複步驟1,2,3直到檔案內容讀完
內建變數
變數 | 作用 |
---|---|
$0 | 一整行內容 |
NR | 記錄號,等同於行號 |
NF | 以-F 分隔符分隔的段數 |
pattern的內容
- 正則
/正則/ # 該行內容匹配成功正則 $1 ~ /正則/ # 第一段內容匹配成功正則 $1 !~ /正則/ # 第一段內容沒有匹配成功正則
- 比較運算
NR >= 3 && NR <=5 # 3到5行 $1 == "root" # 第一段內容等於root
action的內容
- 行號
print $1,$3 # 第1行和第3行 print $0 # 一整行
測試
# 準備資料 [root@localhost test]# vim 2.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 # 儲存並退出 Esc :wq # 檢視2.txt [root@localhost test]# cat 2.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 # 查詢:以:為分隔符 行號大於3的行 列印第1段內容 [root@localhost test]# awk -F: 'NR>3{print $1}' 2.txt adm lp # 查詢:以:為分隔符 從第1行開始匹配 以root開頭的行 列印第1段和第3段內容 [root@localhost test]# awk -F: '/^root/{print $1,$3}' 2.txt root 0 # 查詢:以:為分隔符 從第1行開始匹配 以字母d開頭的 列印第1段和第3段內容 [root@localhost test]# awk -F: '$1 ~ /^d/{print $1,$3}' 2.txt daemon 2 # 查詢:以:為分隔符 從第1行開始匹配 不是以字母d開頭的 列印第1段和第3段內容 [root@localhost test]# awk -F: '$1 !~ /^d/{print $1,$3}' 2.txt root 0 bin 1 adm 3 lp 4 # 查詢:以:為分隔符 匹配第1段內容為lp的行 並列印該行全部內容 [root@localhost test]# awk -F: '$1 == "lp"{print $0}' 2.txt lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin # 查詢:以:為分隔符 列印第1段內容(awk也支援管道命令) [root@localhost test]# cat 2.txt | awk -F: '{print $1}' root bin daemon adm lp
事實上awk是一門
程式語言
,可以獨立完成
很強大的操作
grep
grep擅長過濾內容
用法
grep 選項 '正則' 檔案路徑
選項
選項 | 作用 |
---|---|
-n, --line-number | 在過濾出的每一行前面加上它在檔案中的相對行號 |
-i, --ignore-case | 忽略大小寫 |
--color | 顏色 |
-l, --files-with-matches | 如果匹配成功,則只將檔名打印出來,失敗則不列印 通常 -rl 一起用,grep -rl 'root' /etc |
-R, -r, --recursive | 遞迴 |
測試
# 匹配以root開頭的行 [root@localhost test ~]# grep '^root' /etc/passwd root:x:0:0:root:/root:/bin/bash # 匹配以bash結尾的行並顯示行號 [root@localhost test ~]# grep -n 'bash$' /etc/passwd 1:root:x:0:0:root:/root:/bin/bash # 配合管道命令,查詢有ssh的程序(包括這 grep ssh 這句查詢命令本身) [root@localhost test ~]# ps aux | grep ssh root 1104 0.0 0.3 83040 3652 ? Ss 16:05 0:00 /usr/sbin/sshd -D root 2242 0.0 0.5 141268 5304 ? Ss 16:05 0:00 sshd: root@pts/0 root 2501 0.0 0.0 112644 952 pts/0 S+ 17:58 0:00 grep --color=auto ssh # 配合管道命令,查詢有ssh的程序(不包括這 grep ssh 這句查詢命令本身) [root@localhost test ~]# ps aux | grep [s]sh root 1104 0.0 0.3 83040 3652 ? Ss 16:05 0:00 /usr/sbin/sshd -D root 2242 0.0 0.5 141268 5304 ? Ss 16:05 0:00 sshd: root@pts/0 # 匹配/etc下檔案內容中包含root的檔名並列印(只打印檔名) [root@localhost test ~]# grep -rl 'root' /etc /etc/fstab /etc/pki/tls/certs/make-dummy-cert /etc/pki/tls/openssl.cnf ...
二:檔案查詢
1.檢視命令所屬的檔案
# 查詢ip命令 [root@localhost ~]# which ip /usr/sbin/ip # 查詢ping命令 [root@localhost ~]# which ping /usr/bin/ping # 一些命令的路徑都被配置到了環境變數PATH裡 [root@localhost ~]# echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
2.查詢檔案
語法:
find [options] [path...] [expression]
按檔名查詢
# 按照檔名查詢(完整匹配):查詢/etc/下 名稱為ifcfg的檔案 [root@localhost ~]# find /etc/ -name 'ifcfg-lo' /etc/sysconfig/network-scripts/ifcfg-lo # 按照檔名查詢(萬用字元*):查詢/etc/下 名稱以hosts開頭的檔案 [root@localhost ~]# find /etc/ -name 'hosts*' /etc/hosts /etc/hosts.allow /etc/hosts.deny # 按照檔名查詢(忽略大小寫):查詢/etc/下 名稱以ifcfg開頭的檔案 忽略大小寫 [root@localhost ~]# find /etc/ -iname 'ifcfg*' /etc/sysconfig/network-scripts/ifcfg-lo /etc/sysconfig/network-scripts/ifcfg-eno16777736
按檔案大小查詢
# 查詢/etc目錄下大於3M的檔案 [root@localhost ~]# find /etc -size +3M /etc/udev/hwdb.bin /etc/selinux/targeted/policy/policy.29 # 查詢/etc目錄下等於3M的檔案 [root@localhost] ~# find /etc -size 3M # 查詢/etc目錄下小於3M的檔案 [root@localhost ~]# find /etc -size +3M /etc /etc/fstab /etc/crypttab /etc/mtab ... # -ls找到的處理動作 [root@localhost ~]# find /etc -size +3M -ls 34243049 6824 -r--r--r-- 1 root root 6984832 Nov 18 17:47 /etc/udev/hwdb.bin 101322226 3688 -rw-r--r-- 1 root root 3773297 Nov 18 21:44 /etc/selinux/targeted/policy/policy.29
按照指定目錄深度查詢
# 查詢目錄深度為5 並且 名字包含ifcfg的檔案(-a:並且,-o:或者;如果不加-a,預設就是-a) [root@localhost ~]# find / -maxdepth 5 -a -name "ifcfg*" /etc/sysconfig/network-scripts/ifcfg-lo /etc/sysconfig/network-scripts/ifcfg-eno16777736 /var/log/anaconda/ifcfg.log /usr/sbin/ifcfg /usr/share/man/man8/ifcfg.8.gz
按照時間查詢(atime、mtime、ctime)
# 查詢修改時間在3天以上的日誌檔案 [root@localhost ~]# find / -mtime +3 -name "*.log" /tmp/yum.log /var/log/anaconda/anaconda.log /var/log/anaconda/X.log /var/log/anaconda/program.log /var/log/anaconda/packaging.log /var/log/anaconda/storage.log /var/log/anaconda/ifcfg.log /var/log/anaconda/ks-script-JuIsIQ.log /var/log/anaconda/journal.log /usr/lib/rpm/rpm.log [root@localhost ~]# find /etc -mtime 3 # 修改時間等於3天 [root@localhost ~]# find /etc -mtime -3 # 修改時間3天以內
按照檔案屬主、屬組查詢:
# 查詢屬主是darker的檔案 [root@localhost ~]# find /home -user darker # 查詢屬組是it組的檔案 [root@localhost ~]# find /home -group it [root@localhost ~]# find /home -user cm -group it [root@localhost ~]# find /home -user cm -a -group it # 同上意思一樣 [root@localhost ~]# find /home -user cm -o -group it [root@localhost ~]# find /home -nouser # 使用者還存在,在/etc/passwd中刪除了記錄 [root@localhost ~]# find /home -nogroup # 使用者還存在,在/etc/group中刪除了記錄 [root@localhost ~]# find /home -nouser -o -nogroup
按照檔案型別查詢
find -perm mode詳解
-perm mode # 檔案的許可權正好是mode就匹配 -perm -mode # 檔案的許可權包括mode就匹配(該檔案還可以擁有額外的許可權屬性) -perm +mode # 檔案的許可權部分滿足mode就匹配(已棄用,find新版使用-perm /mode)
[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管道檔案
按照檔案許可權查詢
# 建立1-4.txt [root@localhost ~]#touch {1..4}.txt # 修改1-4.txt的檔案許可權 [root@localhost ~]#chmod 6000 1.txt [root@localhost ~]#chmod 2000 2.txt [root@localhost ~]#chmod 4000 3.txt [root@localhost ~]#chmod 6600 4.txt [root@localhost ~]# find . -perm 6000 -ls 67372494 4 ---S--S--- 1 root root 378 Nov 24 05:13 ./1.txt #按檔案許可權查詢,精確匹配許可權為6000的檔案 [root@localhost test]# find . -perm -6000 -ls # 按檔案許可權查詢,檔案許可權為6000和6000以上的檔案 [root@localhost test]# find . -perm +6000 -ls # 按檔案許可權查詢,部分許可權符合6000的檔案,因centos已啟用+mode寫法,此處會報錯 [root@localhost test]# find . -perm /6000 -ls # 按檔案許可權查詢,部分許可權滿足6000的檔案 [root@localhost local]# find . -perm 644 -ls # 按檔案許可權查詢,檔案許可權為644的檔案 [root@localhost local]# find . -perm -644 -ls # 按檔案許可權查詢,檔案許可權為644或644許可權以上的檔案 [root@localhost local]# find . -perm /644 -ls # 按檔案許可權查詢,部分許可權滿足644的檔案
找到後處理的動作
- ls
- delete
- exec
- ok
[root@localhost ~]# find /etc -name "ifcfg*" -print # 必須加引號,查詢/etc/下名稱為ifcfg*的檔案並列印 [root@localhost ~]# find /etc -name "ifcfg*" -ls #查詢/etc/下名稱為ifcfg*的檔案並列出文件詳情 [root@localhost ~]# find /etc -name "ifcfg*" -exec cp -rvf {} /tmp \; # 非互動 #查詢/etc/下名稱為ifcfg*的檔案並傳遞給cp命令將檔案複製到/tmp目錄下,{}代表find查詢到的的內容 [root@localhost ~]# find /etc -name "ifcfg*" -ok cp -rvf {} /tmp \; # 互動 #查詢/etc/下名稱為ifcfg*的檔案並傳遞給cp命令將檔案複製到/tmp目錄下 [root@localhost ~]# find /etc -name "ifcfg*" -exec rm -rf {} \; #查詢/etc/下名稱為ifcfg*的檔案並傳遞給rm命令進行刪除 [root@localhost ~]# find /etc -name "ifcfg*" -delete # 同上
擴充套件:結合xargs
[root@localhost ~]# find . -name "*.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 {}
[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命令
# 將遠端包下載到本地,-O指定下載到哪裡,可以生路-O 本地路徑 wget -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
sz命令
將伺服器上選定的檔案下載/傳送到本機
系統預設沒有該命令,需要下載
yum install lrzsz -y
[root@localhost ~]# sz bak.tar.gz
2.上傳
rz命令
系統預設沒有該命令,需要下載
yum install lrzsz -y
執行該命令會彈出一個檔案選擇視窗,從本地選擇檔案上傳到伺服器
[root@localhost ~]# rz # 如果檔案已經存,則上傳失敗,可以用-E選項解決 [root@localhost ~]# rz -E # -E如果目標檔名已經存在,則重新命名傳入檔案。新檔名將新增一個點和一個數字(0..999)
如果遇到下載提示無法簡歷SSL連結,使用-k
選項或者--insecure
curl -k -o 123.png https://www.xxx.com/img/hello.png
四:輸出與重定向
1.介紹
輸出即把相關物件通過輸出裝置(顯示器等)顯示出來
輸出又分正確輸出
和錯誤輸出
一般情況下標準輸出裝置為顯示器
,標準輸入裝置為鍵盤
在Linux中:
- 0:標準輸入
- 1:正確輸出
- 2:錯誤輸出
裝置 | 裝置檔名 | 檔案描述符 | 型別 |
---|---|---|---|
鍵盤 | /dev/stdin | 0 | 標準輸入 |
顯示器 | /dev/stdout | 1 | 標準輸出 |
顯示器 | /dev/stderr | 2 | 標準錯誤輸出 |
2.輸出重定向
正常輸出是把內容輸出到顯示器上,而輸出重定向是把內容
輸出到檔案
中,>
代表覆蓋,>>
代表追加
Ps:標準輸出的1可以省略
history >> a.txt 2>&1 #history的記錄全部儲存到a.txt檔案中,不管錯誤輸出還是正確輸出 history &>> b.txt #同上 history >> a.txt #預設為正確輸出儲存到a.txt history ll 2>> b.txt #將錯誤輸出儲存到b.txt文件中
正確日誌和錯誤日誌分開儲存
history >>file1.log 2>>file2.log #將錯誤日誌和正確日誌分開儲存,由於是正確輸出,所以file2.log檔案中無內容輸入
3.輸入重定向
沒有改變輸入的方向,預設鍵盤,此時等待輸入
[root@localhost ~]# tr 'N' 'n' No no echo "hello cm qq:123456" >> file.txt [root@localhost ~]# tr 'cm' 'CM' < file.txt #從file.txt檔案中讀取內容,將小寫cm替換為大寫CM [root@localhost ~]# grep 'root' < /etc/passwd #從/etc/passwd檔案中讀取內容匹配root所在的行 root:x:0:0:root:/root:/bin/bash
輸入重定向:常用於MySQL恢復資料
mysql -u root -p123 < 1.sql
備份MySQL資料
mysqldump -u root -p123 mysql庫名 >> 2.sql
history >> a.txt 2>&1 history &>> b.txt 錯誤輸出 history ll 2>> a.txt
history >>file1.log 2>>file2.log history --ll >>file1.log 2>>file2.log
五:字元處理命令
1.sort命令
用於將檔案內容加以排序
命令 | 作用 |
---|---|
-n | 按照數值的大小進行排序 |
-r | 用相反的順序進行排序 |
-k | 以某列進行排序 |
-t | 指定分隔符(預設是空格) |
# 向file.txt中輸入內容,輸入EOF才停止輸入
cat >> file.txt << EOF
[root@localhost ~]# cat >> file.txt <<EOF b:3 c:2 a:4 e:5 d:1 f:11 EOF #將終端輸入內容插入file.txt檔案中,碰到字元EOF結束。
[root@localhost ~]# sort file.txt a:4 b:3 c:2 d:1 e:5 f:11 #對file.txt文字進行排序
[root@localhost ~]# sort -t ":" -n -k2 file.txt #以冒號為間隔符,對數字型別做比較,比較第二個欄位的值 d:1 c:2 b:3 a:4 e:5 f:11
[root@localhost ~]# sort -t ":" -n -r -k2 file.txt #以冒號為間隔符,對數字型別做比較,比較第二個欄位的值 f:11 e:5 a:4 b:3 c:2 d:1
2.unic命令
用於檢查及刪除文字檔案中重複出現的行列,一般與 sort 命令結合使用
選項 | 作用 |
---|---|
-c | 在每列旁邊顯示該行重複出現的次數 |
-d | 僅顯示重複出現的行列 |
-u | 僅顯示出一次的行列 |
[root@localhost ~]# cat > file.txt << EOF > hello > 123 > world > 123 > hahaha > 456 > EOF [root@localhost ~]# cat file.txt hello 123 world 123 hahaha 456 [root@localhost ~]# sort file.txt 123 123 456 hahaha hello world [root@localhost ~]# sort file.txt | uniq 123 456 hahaha hello world [root@localhost ~]# sort file.txt | uniq -c 2 123 1 456 1 hahaha 1 hello 1 world [root@localhost ~]# sort file.txt | uniq -u 456 hahaha hello world
3.cut命令
cut命令用來顯示行中的指定部分,刪除檔案中指定欄位
選項 | 作用 |
---|---|
-d | 指定欄位的分隔符,預設的欄位分隔符為"TAB" |
-f | 顯示指定欄位的內容 |
[root@localhost ~]# head -1 /etc/passwd #顯示/etc/passwd檔案的第一行內容 root:x:0:0:root:/root:/bin/bash [root@localhost ~]# head -1 /etc/passwd | cut -d ":" -f1,3,4,6 #顯示/etc/passwd第一行內容,並以冒號為間隔符,顯示1,3,4,6段的內容 root:0:0:/root
4.tr命令
替換或刪除命令
選項 | 作用 |
---|---|
-d | 刪除字元 |
[root@localhost ~]# head -1 /etc/passwd |tr "root" "ROOT" #檢視/etc/passwd第一行的內容並將root替換為ROOT ROOT:x:0:0:ROOT:/ROOT:/bin/bash [root@localhost ~]# [root@localhost ~]# head -1 /etc/passwd |tr -d "root" #檢視/etc/passwd第一行的內容並將root字元刪除掉顯示到當前終端 :x:0:0::/:/bin/bash [root@localhost ~]# echo "hello cm qq:123456" > a.txt [root@localhost ~]# tr "cm" "CM" < a.txt hEllO CM qq:123456
5.wc命令
統計,計算數字
選項 | 作用 |
---|---|
-c | 統計檔案的Bytes數 |
-l | 統計檔案的行數 |
-w | 統計檔案中單詞的個數,預設以空白字元做為分隔符 |
[root@localhost ~]# cat file.txt hello 123 world 123 hahaha 456 [root@localhost ~]# wc -c file.txt 31 file.txt [root@localhost ~]# ll file.txt -rw-r--r--. 1 root root 31 Nov 23 20:03 file.txt [root@localhost ~]# wc -l file.txt 6 file.txt [root@localhost ~]# ll file.txt -rw-r--r--. 1 root root 25 8月 12 20:09 file.txt [root@localhost ~]# wc -c file.txt #統計file.txt檔案的大小 [root@localhost ~]# wc -l file.txt #統計file.txt檔案的行數 [root@localhost ~]# grep "hello" file.txt |wc -l #匹配file.txt檔案中hello出現的次數並統計行數 [root@localhost ~]# wc -w file.txt #統計file.txt檔案中單詞的個數
六:壓縮 與 解壓
壓縮
[root@localhost test]# tar czvf etc1_bak.tar.gz /etc/ # 選項z代表gzip壓縮演算法
[root@localhost test]# tar cjvf etc1_bak.tar.bz2 /etc/ # 選項j代表bzip2壓縮演算法
解壓
無論哪種壓縮格式解壓命令都相同
tar xvf etc1_bak.tar.gz -C /usr/local/ #將etc1_bak.tar.gz壓縮包解壓到/usr/local下
tar xvf etc1_bak.tar.bz2 . #將etc1_bak.tar.bz2壓縮包解壓到當前目錄下
評論
------------恢復內容開始------------
Linux - 檔案管理
一:文字處理三劍客
sed
流式編輯器,主要擅長對檔案的編輯操作
可以事先定製好編輯檔案的指令,然後讓sed自動完成對檔案的整體編輯
用法
sed 選項 '定位+命令' 檔案路徑
選項
選項 | 作用 |
---|---|
-n | 取消預設輸出(不會輸出檔案原內容) |
-r | 支援擴充套件正則元字元 |
-i | 立即編輯檔案 |
定位
行定位:
1
定位到第1行1,3
代表從第1行到第3行不寫
定位 代表定位所有行
正則表示式定位:
/darker/
包含darker的行/^darker/
以darker開頭的行/darker$/
以darker結尾的行
數字+正則表示式定位:
1,8p
代表列印1到8行,1,/darker/p
則代表取從第1行到首次匹配到/darker/的行
命令
命令 | 作用 |
---|---|
d | 刪除 |
p | 複製 |
s/before/after/g before:替換前的內容 after:替換後的內容 |
替換 |
命令可以用;
連線多條
測試
# 建立資料夾test [root@localhost ~]# mkdir test # 進入資料夾 [root@localhost test]# cd test # 建立檔案1.txt [root@localhost test]# touch 1.txt # 進入1.txt進行編輯 [root@localhost test]# vim 1.txt # 按 i 進入插入模式 i # 新增文字 darkerline1 linedarker22 line333 line4444darker linedarker55555 line666cm666cm # 退出並儲存 Esc :wq # 正常檢視1.txt內容 [root@localhost test]# cat 1.txt darkerline1 linedarker22 line333 line4444darker linedarker55555 line6 66cm666cm # 預設匹配所有行,顯示1.txt的全部內容 [root@localhost test]# sed '' 1.txt darkerline1 linedarker22 line333 line4444darker linedarker55555 line666cm666cm # 取消預設輸出,預設匹配所有行,顯示a.txt的全部內容 [root@localhost test]# sed -n '' 1.txt # 沒有輸出 # 定位1.txt第1行到第5行 進行復制(p),顯示原文內容 以及複製內容 [root@localhost test]# sed '1,5p' 1.txt darkerline1 darkerline1 linedarker22 linedarker22 line333 line333 line4444darker line4444darker linedarker55555 linedarker55555 line666cm666cm # 從第1行開始匹配到第1個包含darker的行進行復制 [root@localhost test]# sed '1,/darker/p' 1.txt darkerline1 darkerline1 linedarker22 linedarker22 line333 line4444darker linedarker55555 line666cm666cm # 取消預設輸出從第一行匹配到第一個包含xxx的行進行復制 [root@localhost test]# sed -n '1,/darker/p' 1.txt darkerline1 linedarker22 # 從第1行開始匹配到第1個包含333的行進行復制 [root@localhost test]# sed '1,/333/p' 1.txt darkerline1 darkerline1 linedarker22 linedarker22 line333 line333 line4444darker linedarker55555 line666cm666cm # 從第1行開始匹配到第1個包含darker的行刪除(從第1行刪到包含darker的行) [root@localhost test]# sed '1,/darker/d' 1.txt line333 line4444darker linedarker55555 line666cm666cm # 刪除第1行、第3行、第5行,輸出其他行 [root@localhost test]# sed '1d;3d;5d' 1.txt linedarker22 line4444darker line666cm666cm # 把所有行的所有的darker 替換成DARKER,然後輸出 [root@localhost test]# sed 's/darker/DARKER/g' 1.txt DARKERline1 lineDARKER22 line333 line4444DARKER lineDARKER55555 line666cm666cm # 把所有以darker開頭的 行中的darker 替換成Start,然後輸出 [root@localhost test]# sed '/^darker/s/darker/Start/g' 1.txt Startline1 linedarker22 line333 line4444darker linedarker55555 line666cm666cm # 只把第6行的首個cm換成HIGH [root@localhost test]# sed '6s/cm/HIGH/' 1.txt darkerline1 linedarker22 line333 line4444darker linedarker55555 line666HIGH666cm # 只把第6行的所有cm換成HIGH(加上g代表全部替換) [root@localhost test]# sed '6s/cm/HIGH/g' 1.txt darkerline1 linedarker22 line333 line4444darker linedarker55555 line666HIGH666HIGH # 把1到3行的darker換成WOW [root@localhost test]# sed '1,3s/darker/WOW/g' 1.txt WOWline1 lineWOW22 line333 line4444darker linedarker55555 line666cm666cm # 刪除2-5行,複製並輸出第1行和第6行(sed也支援管道操作) [root@localhost test]# cat 1.txt | sed '1p;2d;3d;4d;5d;6p' darkerline1 darkerline1 line666cm666cm line666cm666cm
sed命令加上
-i
選項,可以直接修改檔案,通常會在除錯完畢確保沒有問題後
再加-i選項
awk
主要用於處理有格式的文字,例如/etc/passwd這種
把文件內容切成一段一段
用法
awk 選項 'pattern{action}' 檔案路徑
選項
選項 | 作用 |
---|---|
-F | 指定分隔符(預設是空格 ) |
工作流程
awk -F: '{print $1,$3}' /etc/passwd
① awk會讀取檔案的1行內容然後賦值給$0
② 然後awk會以-F
指定的分隔符將該行切分成n段,最多可以達到100段,第一段給$1
,第二段給$2
,以此類推
③print
輸出該行的第1段和第3段,逗號代表輸出分隔符,預設與-F
保持一致
④ 重複步驟1,2,3直到檔案內容讀完
內建變數
變數 | 作用 |
---|---|
$0 | 一整行內容 |
NR | 記錄號,等同於行號 |
NF | 以-F 分隔符分隔的段數 |
pattern的內容
- 正則
/正則/ # 該行內容匹配成功正則 $1 ~ /正則/ # 第一段內容匹配成功正則 $1 !~ /正則/ # 第一段內容沒有匹配成功正則
- 比較運算
NR >= 3 && NR <=5 # 3到5行 $1 == "root" # 第一段內容等於root
action的內容
- 行號
print $1,$3 # 第1行和第3行 print $0 # 一整行
測試
# 準備資料 [root@localhost test]# vim 2.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 # 儲存並退出 Esc :wq # 檢視2.txt [root@localhost test]# cat 2.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 # 查詢:以:為分隔符 行號大於3的行 列印第1段內容 [root@localhost test]# awk -F: 'NR>3{print $1}' 2.txt adm lp # 查詢:以:為分隔符 從第1行開始匹配 以root開頭的行 列印第1段和第3段內容 [root@localhost test]# awk -F: '/^root/{print $1,$3}' 2.txt root 0 # 查詢:以:為分隔符 從第1行開始匹配 以字母d開頭的 列印第1段和第3段內容 [root@localhost test]# awk -F: '$1 ~ /^d/{print $1,$3}' 2.txt daemon 2 # 查詢:以:為分隔符 從第1行開始匹配 不是以字母d開頭的 列印第1段和第3段內容 [root@localhost test]# awk -F: '$1 !~ /^d/{print $1,$3}' 2.txt root 0 bin 1 adm 3 lp 4 # 查詢:以:為分隔符 匹配第1段內容為lp的行 並列印該行全部內容 [root@localhost test]# awk -F: '$1 == "lp"{print $0}' 2.txt lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin # 查詢:以:為分隔符 列印第1段內容(awk也支援管道命令) [root@localhost test]# cat 2.txt | awk -F: '{print $1}' root bin daemon adm lp
事實上awk是一門
程式語言
,可以獨立完成
很強大的操作
grep
grep擅長過濾內容
用法
grep 選項 '正則' 檔案路徑
選項
選項 | 作用 |
---|---|
-n, --line-number | 在過濾出的每一行前面加上它在檔案中的相對行號 |
-i, --ignore-case | 忽略大小寫 |
--color | 顏色 |
-l, --files-with-matches | 如果匹配成功,則只將檔名打印出來,失敗則不列印 通常 -rl 一起用,grep -rl 'root' /etc |
-R, -r, --recursive | 遞迴 |
測試
# 匹配以root開頭的行 [root@localhost test ~]# grep '^root' /etc/passwd root:x:0:0:root:/root:/bin/bash # 匹配以bash結尾的行並顯示行號 [root@localhost test ~]# grep -n 'bash$' /etc/passwd 1:root:x:0:0:root:/root:/bin/bash # 配合管道命令,查詢有ssh的程序(包括這 grep ssh 這句查詢命令本身) [root@localhost test ~]# ps aux | grep ssh root 1104 0.0 0.3 83040 3652 ? Ss 16:05 0:00 /usr/sbin/sshd -D root 2242 0.0 0.5 141268 5304 ? Ss 16:05 0:00 sshd: root@pts/0 root 2501 0.0 0.0 112644 952 pts/0 S+ 17:58 0:00 grep --color=auto ssh # 配合管道命令,查詢有ssh的程序(不包括這 grep ssh 這句查詢命令本身) [root@localhost test ~]# ps aux | grep [s]sh root 1104 0.0 0.3 83040 3652 ? Ss 16:05 0:00 /usr/sbin/sshd -D root 2242 0.0 0.5 141268 5304 ? Ss 16:05 0:00 sshd: root@pts/0 # 匹配/etc下檔案內容中包含root的檔名並列印(只打印檔名) [root@localhost test ~]# grep -rl 'root' /etc /etc/fstab /etc/pki/tls/certs/make-dummy-cert /etc/pki/tls/openssl.cnf ...
二:檔案查詢
1.檢視命令所屬的檔案
# 查詢ip命令 [root@localhost ~]# which ip /usr/sbin/ip # 查詢ping命令 [root@localhost ~]# which ping /usr/bin/ping # 一些命令的路徑都被配置到了環境變數PATH裡 [root@localhost ~]# echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
2.查詢檔案
語法:
find [options] [path...] [expression]
按檔名查詢
# 按照檔名查詢(完整匹配):查詢/etc/下 名稱為ifcfg的檔案 [root@localhost ~]# find /etc/ -name 'ifcfg-lo' /etc/sysconfig/network-scripts/ifcfg-lo # 按照檔名查詢(萬用字元*):查詢/etc/下 名稱以hosts開頭的檔案 [root@localhost ~]# find /etc/ -name 'hosts*' /etc/hosts /etc/hosts.allow /etc/hosts.deny # 按照檔名查詢(忽略大小寫):查詢/etc/下 名稱以ifcfg開頭的檔案 忽略大小寫 [root@localhost ~]# find /etc/ -iname 'ifcfg*' /etc/sysconfig/network-scripts/ifcfg-lo /etc/sysconfig/network-scripts/ifcfg-eno16777736
按檔案大小查詢
# 查詢/etc目錄下大於3M的檔案 [root@localhost ~]# find /etc -size +3M /etc/udev/hwdb.bin /etc/selinux/targeted/policy/policy.29 # 查詢/etc目錄下等於3M的檔案 [root@localhost] ~# find /etc -size 3M # 查詢/etc目錄下小於3M的檔案 [root@localhost ~]# find /etc -size +3M /etc /etc/fstab /etc/crypttab /etc/mtab ... # -ls找到的處理動作 [root@localhost ~]# find /etc -size +3M -ls 34243049 6824 -r--r--r-- 1 root root 6984832 Nov 18 17:47 /etc/udev/hwdb.bin 101322226 3688 -rw-r--r-- 1 root root 3773297 Nov 18 21:44 /etc/selinux/targeted/policy/policy.29
按照指定目錄深度查詢
# 查詢目錄深度為5 並且 名字包含ifcfg的檔案(-a:並且,-o:或者;如果不加-a,預設就是-a) [root@localhost ~]# find / -maxdepth 5 -a -name "ifcfg*" /etc/sysconfig/network-scripts/ifcfg-lo /etc/sysconfig/network-scripts/ifcfg-eno16777736 /var/log/anaconda/ifcfg.log /usr/sbin/ifcfg /usr/share/man/man8/ifcfg.8.gz
按照時間查詢(atime、mtime、ctime)
# 查詢修改時間在3天以上的日誌檔案 [root@localhost ~]# find / -mtime +3 -name "*.log" /tmp/yum.log /var/log/anaconda/anaconda.log /var/log/anaconda/X.log /var/log/anaconda/program.log /var/log/anaconda/packaging.log /var/log/anaconda/storage.log /var/log/anaconda/ifcfg.log /var/log/anaconda/ks-script-JuIsIQ.log /var/log/anaconda/journal.log /usr/lib/rpm/rpm.log [root@localhost ~]# find /etc -mtime 3 # 修改時間等於3天 [root@localhost ~]# find /etc -mtime -3 # 修改時間3天以內
按照檔案屬主、屬組查詢:
# 查詢屬主是darker的檔案 [root@localhost ~]# find /home -user darker # 查詢屬組是it組的檔案 [root@localhost ~]# find /home -group it [root@localhost ~]# find /home -user cm -group it [root@localhost ~]# find /home -user cm -a -group it # 同上意思一樣 [root@localhost ~]# find /home -user cm -o -group it [root@localhost ~]# find /home -nouser # 使用者還存在,在/etc/passwd中刪除了記錄 [root@localhost ~]# find /home -nogroup # 使用者還存在,在/etc/group中刪除了記錄 [root@localhost ~]# find /home -nouser -o -nogroup
按照檔案型別查詢
find -perm mode詳解
-perm mode # 檔案的許可權正好是mode就匹配 -perm -mode # 檔案的許可權包括mode就匹配(該檔案還可以擁有額外的許可權屬性) -perm +mode # 檔案的許可權部分滿足mode就匹配(已棄用,find新版使用-perm /mode)
[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管道檔案
按照檔案許可權查詢
# 建立1-4.txt [root@localhost ~]#touch {1..4}.txt # 修改1-4.txt的檔案許可權 [root@localhost ~]#chmod 6000 1.txt [root@localhost ~]#chmod 2000 2.txt [root@localhost ~]#chmod 4000 3.txt [root@localhost ~]#chmod 6600 4.txt [root@localhost ~]# find . -perm 6000 -ls 67372494 4 ---S--S--- 1 root root 378 Nov 24 05:13 ./1.txt #按檔案許可權查詢,精確匹配許可權為6000的檔案 [root@localhost test]# find . -perm -6000 -ls # 按檔案許可權查詢,檔案許可權為6000和6000以上的檔案 [root@localhost test]# find . -perm +6000 -ls # 按檔案許可權查詢,部分許可權符合6000的檔案,因centos已啟用+mode寫法,此處會報錯 [root@localhost test]# find . -perm /6000 -ls # 按檔案許可權查詢,部分許可權滿足6000的檔案 [root@localhost local]# find . -perm 644 -ls # 按檔案許可權查詢,檔案許可權為644的檔案 [root@localhost local]# find . -perm -644 -ls # 按檔案許可權查詢,檔案許可權為644或644許可權以上的檔案 [root@localhost local]# find . -perm /644 -ls # 按檔案許可權查詢,部分許可權滿足644的檔案
找到後處理的動作
- ls
- delete
- exec
- ok
[root@localhost ~]# find /etc -name "ifcfg*" -print # 必須加引號,查詢/etc/下名稱為ifcfg*的檔案並列印 [root@localhost ~]# find /etc -name "ifcfg*" -ls #查詢/etc/下名稱為ifcfg*的檔案並列出文件詳情 [root@localhost ~]# find /etc -name "ifcfg*" -exec cp -rvf {} /tmp \; # 非互動 #查詢/etc/下名稱為ifcfg*的檔案並傳遞給cp命令將檔案複製到/tmp目錄下,{}代表find查詢到的的內容 [root@localhost ~]# find /etc -name "ifcfg*" -ok cp -rvf {} /tmp \; # 互動 #查詢/etc/下名稱為ifcfg*的檔案並傳遞給cp命令將檔案複製到/tmp目錄下 [root@localhost ~]# find /etc -name "ifcfg*" -exec rm -rf {} \; #查詢/etc/下名稱為ifcfg*的檔案並傳遞給rm命令進行刪除 [root@localhost ~]# find /etc -name "ifcfg*" -delete # 同上
擴充套件:結合xargs
[root@localhost ~]# find . -name "*.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 {}
[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命令
# 將遠端包下載到本地,-O指定下載到哪裡,可以生路-O 本地路徑 wget -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
sz命令
將伺服器上選定的檔案下載/傳送到本機
系統預設沒有該命令,需要下載
yum install lrzsz -y
[root@localhost ~]# sz bak.tar.gz
2.上傳
rz命令
系統預設沒有該命令,需要下載
yum install lrzsz -y
執行該命令會彈出一個檔案選擇視窗,從本地選擇檔案上傳到伺服器
[root@localhost ~]# rz # 如果檔案已經存,則上傳失敗,可以用-E選項解決 [root@localhost ~]# rz -E # -E如果目標檔名已經存在,則重新命名傳入檔案。新檔名將新增一個點和一個數字(0..999)
如果遇到下載提示無法簡歷SSL連結,使用-k
選項或者--insecure
curl -k -o 123.png https://www.xxx.com/img/hello.png
四:輸出與重定向
1.介紹
輸出即把相關物件通過輸出裝置(顯示器等)顯示出來
輸出又分正確輸出
和錯誤輸出
一般情況下標準輸出裝置為顯示器
,標準輸入裝置為鍵盤
在Linux中:
- 0:標準輸入
- 1:正確輸出
- 2:錯誤輸出
裝置 | 裝置檔名 | 檔案描述符 | 型別 |
---|---|---|---|
鍵盤 | /dev/stdin | 0 | 標準輸入 |
顯示器 | /dev/stdout | 1 | 標準輸出 |
顯示器 | /dev/stderr | 2 | 標準錯誤輸出 |
2.輸出重定向
正常輸出是把內容輸出到顯示器上,而輸出重定向是把內容
輸出到檔案
中,>
代表覆蓋,>>
代表追加
Ps:標準輸出的1可以省略
history >> a.txt 2>&1 #history的記錄全部儲存到a.txt檔案中,不管錯誤輸出還是正確輸出 history &>> b.txt #同上 history >> a.txt #預設為正確輸出儲存到a.txt history ll 2>> b.txt #將錯誤輸出儲存到b.txt文件中
正確日誌和錯誤日誌分開儲存
history >>file1.log 2>>file2.log #將錯誤日誌和正確日誌分開儲存,由於是正確輸出,所以file2.log檔案中無內容輸入
3.輸入重定向
沒有改變輸入的方向,預設鍵盤,此時等待輸入
[root@localhost ~]# tr 'N' 'n' No no echo "hello cm qq:123456" >> file.txt [root@localhost ~]# tr 'cm' 'CM' < file.txt #從file.txt檔案中讀取內容,將小寫cm替換為大寫CM [root@localhost ~]# grep 'root' < /etc/passwd #從/etc/passwd檔案中讀取內容匹配root所在的行 root:x:0:0:root:/root:/bin/bash
輸入重定向:常用於MySQL恢復資料
mysql -u root -p123 < 1.sql
備份MySQL資料
mysqldump -u root -p123 mysql庫名 >> 2.sql
history >> a.txt 2>&1 history &>> b.txt 錯誤輸出 history ll 2>> a.txt
history >>file1.log 2>>file2.log history --ll >>file1.log 2>>file2.log
五:字元處理命令
1.sort命令
用於將檔案內容加以排序
命令 | 作用 |
---|---|
-n | 按照數值的大小進行排序 |
-r | 用相反的順序進行排序 |
-k | 以某列進行排序 |
-t | 指定分隔符(預設是空格) |
# 向file.txt中輸入內容,輸入EOF才停止輸入
cat >> file.txt << EOF
[root@localhost ~]# cat >> file.txt <<EOF b:3 c:2 a:4 e:5 d:1 f:11 EOF #將終端輸入內容插入file.txt檔案中,碰到字元EOF結束。
[root@localhost ~]# sort file.txt a:4 b:3 c:2 d:1 e:5 f:11 #對file.txt文字進行排序
[root@localhost ~]# sort -t ":" -n -k2 file.txt #以冒號為間隔符,對數字型別做比較,比較第二個欄位的值 d:1 c:2 b:3 a:4 e:5 f:11
[root@localhost ~]# sort -t ":" -n -r -k2 file.txt #以冒號為間隔符,對數字型別做比較,比較第二個欄位的值 f:11 e:5 a:4 b:3 c:2 d:1
2.unic命令
用於檢查及刪除文字檔案中重複出現的行列,一般與 sort 命令結合使用
選項 | 作用 |
---|---|
-c | 在每列旁邊顯示該行重複出現的次數 |
-d | 僅顯示重複出現的行列 |
-u | 僅顯示出一次的行列 |
[root@localhost ~]# cat > file.txt << EOF > hello > 123 > world > 123 > hahaha > 456 > EOF [root@localhost ~]# cat file.txt hello 123 world 123 hahaha 456 [root@localhost ~]# sort file.txt 123 123 456 hahaha hello world [root@localhost ~]# sort file.txt | uniq 123 456 hahaha hello world [root@localhost ~]# sort file.txt | uniq -c 2 123 1 456 1 hahaha 1 hello 1 world [root@localhost ~]# sort file.txt | uniq -u 456 hahaha hello world
3.cut命令
cut命令用來顯示行中的指定部分,刪除檔案中指定欄位
選項 | 作用 |
---|---|
-d | 指定欄位的分隔符,預設的欄位分隔符為"TAB" |
-f | 顯示指定欄位的內容 |
[root@localhost ~]# head -1 /etc/passwd #顯示/etc/passwd檔案的第一行內容 root:x:0:0:root:/root:/bin/bash [root@localhost ~]# head -1 /etc/passwd | cut -d ":" -f1,3,4,6 #顯示/etc/passwd第一行內容,並以冒號為間隔符,顯示1,3,4,6段的內容 root:0:0:/root
4.tr命令
替換或刪除命令
選項 | 作用 |
---|---|
-d | 刪除字元 |
[root@localhost ~]# head -1 /etc/passwd |tr "root" "ROOT" #檢視/etc/passwd第一行的內容並將root替換為ROOT ROOT:x:0:0:ROOT:/ROOT:/bin/bash [root@localhost ~]# [root@localhost ~]# head -1 /etc/passwd |tr -d "root" #檢視/etc/passwd第一行的內容並將root字元刪除掉顯示到當前終端 :x:0:0::/:/bin/bash [root@localhost ~]# echo "hello cm qq:123456" > a.txt [root@localhost ~]# tr "cm" "CM" < a.txt hEllO CM qq:123456
5.wc命令
統計,計算數字
選項 | 作用 |
---|---|
-c | 統計檔案的Bytes數 |
-l | 統計檔案的行數 |
-w | 統計檔案中單詞的個數,預設以空白字元做為分隔符 |
Linux - 檔案管理
分享到:一:文字處理三劍客
sed
流式編輯器,主要擅長對檔案的編輯操作
可以事先定製好編輯檔案的指令,然後讓sed自動完成對檔案的整體編輯
用法
sed 選項 '定位+命令' 檔案路徑
選項
選項 | 作用 |
---|---|
-n | 取消預設輸出(不會輸出檔案原內容) |
-r | 支援擴充套件正則元字元 |
-i | 立即編輯檔案 |
定位
行定位:
1
定位到第1行1,3
代表從第1行到第3行不寫
定位 代表定位所有行
正則表示式定位:
/darker/
包含darker的行/^darker/
以darker開頭的行/darker$/
以darker結尾的行
數字+正則表示式定位:
1,8p
代表列印1到8行,1,/darker/p
則代表取從第1行到首次匹配到/darker/的行
命令
命令 | 作用 |
---|---|
d | 刪除 |
p | 複製 |
s/before/after/g before:替換前的內容 after:替換後的內容 |
替換 |
命令可以用;
連線多條
測試
# 建立資料夾test
[root@localhost ~]# mkdir test
# 進入資料夾
[root@localhost test]# cd test
# 建立檔案1.txt
[root@localhost test]# touch 1.txt
# 進入1.txt進行編輯
[root@localhost test]# vim 1.txt
# 按 i 進入插入模式
i
# 新增文字
darkerline1
linedarker22
line333
line4444darker
linedarker55555
line666cm666cm
# 退出並儲存
Esc
:wq
# 正常檢視1.txt內容
[root@localhost test]# cat 1.txt
darkerline1
linedarker22
line333
line4444darker
linedarker55555
line6
66cm666cm
# 預設匹配所有行,顯示1.txt的全部內容
[root@localhost test]# sed '' 1.txt
darkerline1
linedarker22
line333
line4444darker
linedarker55555
line666cm666cm
# 取消預設輸出,預設匹配所有行,顯示a.txt的全部內容
[root@localhost test]# sed -n '' 1.txt
# 沒有輸出
# 定位1.txt第1行到第5行 進行復制(p),顯示原文內容 以及複製內容
[root@localhost test]# sed '1,5p' 1.txt
darkerline1
darkerline1
linedarker22
linedarker22
line333
line333
line4444darker
line4444darker
linedarker55555
linedarker55555
line666cm666cm
# 從第1行開始匹配到第1個包含darker的行進行復制
[root@localhost test]# sed '1,/darker/p' 1.txt
darkerline1
darkerline1
linedarker22
linedarker22
line333
line4444darker
linedarker55555
line666cm666cm
# 取消預設輸出從第一行匹配到第一個包含xxx的行進行復制
[root@localhost test]# sed -n '1,/darker/p' 1.txt
darkerline1
linedarker22
# 從第1行開始匹配到第1個包含333的行進行復制
[root@localhost test]# sed '1,/333/p' 1.txt
darkerline1
darkerline1
linedarker22
linedarker22
line333
line333
line4444darker
linedarker55555
line666cm666cm
# 從第1行開始匹配到第1個包含darker的行刪除(從第1行刪到包含darker的行)
[root@localhost test]# sed '1,/darker/d' 1.txt
line333
line4444darker
linedarker55555
line666cm666cm
# 刪除第1行、第3行、第5行,輸出其他行
[root@localhost test]# sed '1d;3d;5d' 1.txt
linedarker22
line4444darker
line666cm666cm
# 把所有行的所有的darker 替換成DARKER,然後輸出
[root@localhost test]# sed 's/darker/DARKER/g' 1.txt
DARKERline1
lineDARKER22
line333
line4444DARKER
lineDARKER55555
line666cm666cm
# 把所有以darker開頭的 行中的darker 替換成Start,然後輸出
[root@localhost test]# sed '/^darker/s/darker/Start/g' 1.txt
Startline1
linedarker22
line333
line4444darker
linedarker55555
line666cm666cm
# 只把第6行的首個cm換成HIGH
[root@localhost test]# sed '6s/cm/HIGH/' 1.txt
darkerline1
linedarker22
line333
line4444darker
linedarker55555
line666HIGH666cm
# 只把第6行的所有cm換成HIGH(加上g代表全部替換)
[root@localhost test]# sed '6s/cm/HIGH/g' 1.txt
darkerline1
linedarker22
line333
line4444darker
linedarker55555
line666HIGH666HIGH
# 把1到3行的darker換成WOW
[root@localhost test]# sed '1,3s/darker/WOW/g' 1.txt
WOWline1
lineWOW22
line333
line4444darker
linedarker55555
line666cm666cm
# 刪除2-5行,複製並輸出第1行和第6行(sed也支援管道操作)
[root@localhost test]# cat 1.txt | sed '1p;2d;3d;4d;5d;6p'
darkerline1
darkerline1
line666cm666cm
line666cm666cm
Bash
sed命令加上
-i
選項,可以直接修改檔案,通常會在除錯完畢確保沒有問題後
再加-i選項
awk
主要用於處理有格式的文字,例如/etc/passwd這種
把文件內容切成一段一段
用法
awk 選項 'pattern{action}' 檔案路徑
選項
選項 | 作用 |
---|---|
-F | 指定分隔符(預設是空格 ) |
工作流程
awk -F: '{print $1,$3}' /etc/passwd
① awk會讀取檔案的1行內容然後賦值給$0
② 然後awk會以-F
指定的分隔符將該行切分成n段,最多可以達到100段,第一段給$1
,第二段給$2
,以此類推
③print
輸出該行的第1段和第3段,逗號代表輸出分隔符,預設與-F
保持一致
④ 重複步驟1,2,3直到檔案內容讀完
內建變數
變數 | 作用 |
---|---|
$0 | 一整行內容 |
NR | 記錄號,等同於行號 |
NF | 以-F 分隔符分隔的段數 |
pattern的內容
- 正則
/正則/ # 該行內容匹配成功正則
$1 ~ /正則/ # 第一段內容匹配成功正則
$1 !~ /正則/ # 第一段內容沒有匹配成功正則
Bash
- 比較運算
NR >= 3 && NR <=5 # 3到5行
$1 == "root" # 第一段內容等於root
Bash
action的內容
- 行號
print $1,$3 # 第1行和第3行
print $0 # 一整行
Bash
測試
# 準備資料
[root@localhost test]# vim 2.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
# 儲存並退出
Esc
:wq
# 檢視2.txt
[root@localhost test]# cat 2.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
# 查詢:以:為分隔符 行號大於3的行 列印第1段內容
[root@localhost test]# awk -F: 'NR>3{print $1}' 2.txt
adm
lp
# 查詢:以:為分隔符 從第1行開始匹配 以root開頭的行 列印第1段和第3段內容
[root@localhost test]# awk -F: '/^root/{print $1,$3}' 2.txt
root 0
# 查詢:以:為分隔符 從第1行開始匹配 以字母d開頭的 列印第1段和第3段內容
[root@localhost test]# awk -F: '$1 ~ /^d/{print $1,$3}' 2.txt
daemon 2
# 查詢:以:為分隔符 從第1行開始匹配 不是以字母d開頭的 列印第1段和第3段內容
[root@localhost test]# awk -F: '$1 !~ /^d/{print $1,$3}' 2.txt
root 0
bin 1
adm 3
lp 4
# 查詢:以:為分隔符 匹配第1段內容為lp的行 並列印該行全部內容
[root@localhost test]# awk -F: '$1 == "lp"{print $0}' 2.txt
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
# 查詢:以:為分隔符 列印第1段內容(awk也支援管道命令)
[root@localhost test]# cat 2.txt | awk -F: '{print $1}'
root
bin
daemon
adm
lp
Bash
事實上awk是一門
程式語言
,可以獨立完成
很強大的操作
grep
grep擅長過濾內容
用法
grep 選項 '正則' 檔案路徑
選項
選項 | 作用 |
---|---|
-n, --line-number | 在過濾出的每一行前面加上它在檔案中的相對行號 |
-i, --ignore-case | 忽略大小寫 |
--color | 顏色 |
-l, --files-with-matches | 如果匹配成功,則只將檔名打印出來,失敗則不列印 通常 -rl 一起用,grep -rl 'root' /etc |
-R, -r, --recursive | 遞迴 |
測試
# 匹配以root開頭的行
[root@localhost test ~]# grep '^root' /etc/passwd
root:x:0:0:root:/root:/bin/bash
# 匹配以bash結尾的行並顯示行號
[root@localhost test ~]# grep -n 'bash$' /etc/passwd
1:root:x:0:0:root:/root:/bin/bash
# 配合管道命令,查詢有ssh的程序(包括這 grep ssh 這句查詢命令本身)
[root@localhost test ~]# ps aux | grep ssh
root 1104 0.0 0.3 83040 3652 ? Ss 16:05 0:00 /usr/sbin/sshd -D
root 2242 0.0 0.5 141268 5304 ? Ss 16:05 0:00 sshd: root@pts/0
root 2501 0.0 0.0 112644 952 pts/0 S+ 17:58 0:00 grep --color=auto ssh
# 配合管道命令,查詢有ssh的程序(不包括這 grep ssh 這句查詢命令本身)
[root@localhost test ~]# ps aux | grep [s]sh
root 1104 0.0 0.3 83040 3652 ? Ss 16:05 0:00 /usr/sbin/sshd -D
root 2242 0.0 0.5 141268 5304 ? Ss 16:05 0:00 sshd: root@pts/0
# 匹配/etc下檔案內容中包含root的檔名並列印(只打印檔名)
[root@localhost test ~]# grep -rl 'root' /etc
/etc/fstab
/etc/pki/tls/certs/make-dummy-cert
/etc/pki/tls/openssl.cnf
...
Bash
二:檔案查詢
1.檢視命令所屬的檔案
# 查詢ip命令
[root@localhost ~]# which ip
/usr/sbin/ip
# 查詢ping命令
[root@localhost ~]# which ping
/usr/bin/ping
# 一些命令的路徑都被配置到了環境變數PATH裡
[root@localhost ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
Bash
2.查詢檔案
語法:
find [options] [path...] [expression]
按檔名查詢
# 按照檔名查詢(完整匹配):查詢/etc/下 名稱為ifcfg的檔案
[root@localhost ~]# find /etc/ -name 'ifcfg-lo'
/etc/sysconfig/network-scripts/ifcfg-lo
# 按照檔名查詢(萬用字元*):查詢/etc/下 名稱以hosts開頭的檔案
[root@localhost ~]# find /etc/ -name 'hosts*'
/etc/hosts
/etc/hosts.allow
/etc/hosts.deny
# 按照檔名查詢(忽略大小寫):查詢/etc/下 名稱以ifcfg開頭的檔案 忽略大小寫
[root@localhost ~]# find /etc/ -iname 'ifcfg*'
/etc/sysconfig/network-scripts/ifcfg-lo
/etc/sysconfig/network-scripts/ifcfg-eno16777736
Bash
按檔案大小查詢
# 查詢/etc目錄下大於3M的檔案
[root@localhost ~]# find /etc -size +3M
/etc/udev/hwdb.bin
/etc/selinux/targeted/policy/policy.29
# 查詢/etc目錄下等於3M的檔案
[root@localhost] ~# find /etc -size 3M
# 查詢/etc目錄下小於3M的檔案
[root@localhost ~]# find /etc -size +3M
/etc
/etc/fstab
/etc/crypttab
/etc/mtab
...
# -ls找到的處理動作
[root@localhost ~]# find /etc -size +3M -ls
34243049 6824 -r--r--r-- 1 root root 6984832 Nov 18 17:47 /etc/udev/hwdb.bin
101322226 3688 -rw-r--r-- 1 root root 3773297 Nov 18 21:44 /etc/selinux/targeted/policy/policy.29
Bash
按照指定目錄深度查詢
# 查詢目錄深度為5 並且 名字包含ifcfg的檔案(-a:並且,-o:或者;如果不加-a,預設就是-a)
[root@localhost ~]# find / -maxdepth 5 -a -name "ifcfg*"
/etc/sysconfig/network-scripts/ifcfg-lo
/etc/sysconfig/network-scripts/ifcfg-eno16777736
/var/log/anaconda/ifcfg.log
/usr/sbin/ifcfg
/usr/share/man/man8/ifcfg.8.gz
Bash
按照時間查詢(atime、mtime、ctime)
# 查詢修改時間在3天以上的日誌檔案
[root@localhost ~]# find / -mtime +3 -name "*.log"
/tmp/yum.log
/var/log/anaconda/anaconda.log
/var/log/anaconda/X.log
/var/log/anaconda/program.log
/var/log/anaconda/packaging.log
/var/log/anaconda/storage.log
/var/log/anaconda/ifcfg.log
/var/log/anaconda/ks-script-JuIsIQ.log
/var/log/anaconda/journal.log
/usr/lib/rpm/rpm.log
[root@localhost ~]# find /etc -mtime 3 # 修改時間等於3天
[root@localhost ~]# find /etc -mtime -3 # 修改時間3天以內
Bash
按照檔案屬主、屬組查詢:
# 查詢屬主是darker的檔案
[root@localhost ~]# find /home -user darker
# 查詢屬組是it組的檔案
[root@localhost ~]# find /home -group it
[root@localhost ~]# find /home -user cm -group it
[root@localhost ~]# find /home -user cm -a -group it # 同上意思一樣
[root@localhost ~]# find /home -user cm -o -group it
[root@localhost ~]# find /home -nouser # 使用者還存在,在/etc/passwd中刪除了記錄
[root@localhost ~]# find /home -nogroup # 使用者還存在,在/etc/group中刪除了記錄
[root@localhost ~]# find /home -nouser -o -nogroup
Bash
按照檔案型別查詢
find -perm mode詳解
-perm mode # 檔案的許可權正好是mode就匹配
-perm -mode # 檔案的許可權包括mode就匹配(該檔案還可以擁有額外的許可權屬性)
-perm +mode # 檔案的許可權部分滿足mode就匹配(已棄用,find新版使用-perm /mode)
Bash
[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管道檔案
Bash
按照檔案許可權查詢
------------恢復內容結束------------