linux shell 學習摘記(2)
《linux shell 指令碼程式設計攻略 (第2版)》 第二章 “命令之樂” 筆記
主要命令:
cat
,script
,scriptreplay
,find -exec
,xargs
,tr
,md5sum
,sha1sum
,md5dep
,crypt
,gpg
,base64
,opensslpasswd
,sort
,uniq
,mktemp
,split
,csplit
,${URL%*.}
,${URL##*.}
,rename
,look
,echo -e
,grep -q
,PIDARRAY+=("$!")
,expect
等
-
被作為stdin文字的檔名,如echo "hello" | cat - file.txt
cat -s file.txt
可壓縮相鄰的空白行;cat -T file.py
能將製表符標記成^|
(對python等檔案很有用);cat -n
顯示行號(其也會為空白行新增行號,加-b
可跳過空白符)錄製並回放終端會話
- 開始錄製會話
script -t 2> timing.log -a output.session
,錄製完後鍵入exit
退出,最後通過scriptreplay timing.log output.session
回放 - timing.log 用來存在時序資訊(
-t
參數列示將時序資訊重定向到stderr) output.session 儲存命令輸出資訊
- 開始錄製會話
find
find . -name "*.txt"
,find . -iname "example*"
忽略大小寫find . \( -name "*.txt" -o -name "*.sh" \)
列印出所有的.txt和.sh檔案find /home/swf -path "*/hello/*"
使用-path
來匹配檔案路徑;find . -iregex ".*\(\.py\|\.sh\)$"
通過正規表示式來匹配.py和.sh檔案,且忽略find . -iregex ".*\(\.py\|\.sh\)$"
忽略大小寫find . ! -name "*.txt"
否定查詢find . -maxdepth 3 -mindepth 1 -name "f*"
基於目錄深度的搜尋find . -type d
查詢目錄(f/l/c/b/s/p),s表示套接字裝置,p表示FIFO-atime -mtime -ctime
訪問時間、檔案內容修改時間、檔案後設資料修改時間(許可權、所有權);-atime -7
表示最近7天內,-atime 7
恰好在七天前,-atime +7
超過七天, 如find . -type f -atime +7
; 類似的有-amin -mmin -cmin
(基於分鐘的引數)-size +2k
,-size 2k
,-size -2k
按檔案大小(大於,等於,小於)- 刪除當前目錄下的.swp檔案,
find . -type f -perm 644 -name "*.swp" -delete
find . -name "*.c" -exec cat {} \; > all_c_files.txt
find同命令結合find . \( -name ".git" -prune \) -o \( -type f -print \)
不包含在.git目錄中的所有檔名(若前者為真則後者不執行)
xargs能把從stdin接收到的資料重新格式化,再將其作為引數提供給其他命令
cat example.txt | xargs -d X -n 3
以X為定界符, 每行2個引數cat args.txt | xargs -n 1 ./cecho.sh
,cat args.txt | xargs -I {} ./cecho.sh -p {} -l
,其中{}會進行擴充- 刪除特定系列檔案
find . -type f -name "*.txt" -print0 | xargs -0 rm -rf"
- 統計每個檔案的行數和總行數
find . -type -f -name "*.md" -print0 | xargs -0 wc -l
cat files.txt | ( while read arg; do cat $arg; done; )
相當於cat files.txt | xargs -I {} cat {}
echo file.txt | tr 'a-zA-Z' 'n-za-mN-ZA-M'
,tr -d '0-9'
刪除指定集合中的字元,tr -d -c '0-9 \n'將不在集合中的所有字元刪除,
tr -s ' '`將連續空格壓縮為一個空格- 將檔案中的數字疊加
cat sum.txt | echo $[ $( tr '\n' '+' ) 0]
- 支援字元類
tr [:lower:] [:upper:]
- 將檔案中的數字疊加
加密和雜湊
md5sum -c *.md5
,sha1sum -c *.sha1
檢查校驗和- 對目錄進行校驗
md5dep -rl . > dircetory.md5
(-r 遞迴方式, -l 相對路徑);find . -type f -print0 | xargs -0 md5sum >> directory.md
crypt <input_file >output_file
加密,crypt PASSPARSE -d <encryptedfile >outputfile
解密;gpg -c filename
加密,gpg filename.gpg
解密;base64 -d file > outputfile
- shadow-like雜湊, salt -->
opensslpasswd -1 -salt SALTSTRING PASSWORD
(類似/etc/shadow
)維基參考
sort -nrk 1 data.txt
-nr表明按照數字,採用逆序排列(預設是按照字母表排序),-k 1
表示按照第一列排序;sort -z data.txt | xargs -0
;sort -bd unsorted.txt
-b忽略檔案中的前導空白行,-d表明用字典排序uniq -u sorted.txt
只顯示第一行,-c
統計各行出現的次數,-d
找出重複的行;sort data.txt | uniq -s 2 -w 2
-s指定可以跳過前2個字元;-w指定用於比較的最大字元數;uniq -z file.txt | xargs -o rm
mktemp -d
,mktemp test.XXX
根據模版建立臨時檔案,(保證至少有3個X)split -b 10k data.file -d -a 4 splitfile
-d 表示以數字為字尾, -a 表示字尾長度;-l
可以通過行來進行切分。但split只能通過檔案大小和行來進行切分csplit server.log /SERVER/ -n 2 -s {*} -f server -b "%02d.log" ; rm server00.log
按日誌檔案中的某個單詞或內容進行切割。/[REGEX]/
表示文字樣式,用來匹配某一行{*}
表示匹配重複的次數,中間數字若為*
則表示匹配直到檔案結尾-s
靜態模式,-n
分割後的檔名字尾的數字數,-f
分割後的檔名字首,-b
指定字尾格式(同fprint)
副檔名切分,
%
表示從右向左進行匹配,刪除匹配的到的內容(非貪婪),%%
同%但是貪婪匹配;#
表示從左向右匹配,刪除匹配到的內容(非貪婪),##
同#但是**貪婪匹配 此處有指令碼rename 's/ /_/g' *
將檔名中的空格替換成字元_
;rename 'y/A-Z/a-z/' *
轉換檔名的大小;find . -type f -name "*.mp3" -exec mv {} targetdir \;
移動mp3檔案到特定的目錄下;find . -type -f -exec rename 's/ /_/g' {} \;
[ -z $output ]
用於判斷output是否為空grep "^word" /usr/share/dict/words -q
;look word
列出預設字典中所有的單詞,look word /home/wenfeng/test.txt
列出檔案中以word起頭的所有單詞echo -e "\nhello\n"
expect可實現互動式輸入利用並行程式進行加速
$!
最近一個後臺程式的PID 此處有指令碼
相關文章
- 【Linux學習筆記29】shell指令碼基礎Linux筆記指令碼
- shell學習筆記筆記
- shell學習總結-2
- Linux學習筆記(2)——ls指令Linux筆記
- Linux學習之(shell展開)Linux
- Linux學習-shell基礎02Linux
- Linux程式設計學習筆記 | Linux IO學習[2] – 標準IOLinux程式設計筆記
- Linux命令和shell指令碼學習Linux指令碼
- linux shell陣列深入學習理解Linux陣列
- 前端學習 linux —— shell 程式設計前端Linux程式設計
- POSIX-shell學習筆記筆記
- 什麼是shell指令碼?Linux為什麼學習shell?指令碼Linux
- Linux學習之路(三)Shell指令碼初探Linux指令碼
- 【學習】Linux Shell指令碼程式設計Linux指令碼程式設計
- shell指令碼學習筆記-1指令碼筆記
- 如何學習shell程式設計?Linux運維學習shell程式設計是什麼程式設計Linux運維
- Linux shell基礎2Linux
- shell雜記2
- linux學習day3——shell指令碼上Linux指令碼
- linux學習day4——shell指令碼中Linux指令碼
- MongoDB 學習筆記之常用 shell 命令MongoDB筆記
- shell學習
- Linux 學習筆記Linux筆記
- Linux運維要了解哪些shell技能?Linux學習入門Linux運維
- Linux Shell程式設計(2)Linux程式設計
- markdown使用摘記
- Linux 學習筆記--程式Linux筆記
- shell程式設計學習筆記(二):Shell中變數的使用程式設計筆記變數
- 學習Shell 教程
- Linux基礎學習-Docker學習筆記LinuxDocker筆記
- react學習筆記2React筆記
- Vue學習筆記2Vue筆記
- 學習筆記2(下)筆記
- RocketMQ學習筆記 2MQ筆記
- Python學習筆記(2)Python筆記
- Solidity學習筆記-2Solid筆記
- vue學習筆記-2Vue筆記
- hibernate學習筆記(2)筆記
- MySQL學習筆記2MySql筆記