Linux基礎命令--grep/find
阿新 • • 發佈:2018-12-02
1、定義一個對所有使用者都生效的命令別名,例如:lftps=‘lftp 172.168.0.1/pub’
在/etc/profile.d/目錄下建立檔案alias_glob.sh,新增一下程式碼:
alias lftps=‘lftp 172.168.0.1/pub’
新增後儲存退出,立即生效命令:
source /etc/profile.d/alias_glob.sh
2、顯示/etc/passwd檔案中不以/bin/bash結尾的行
grep -v '/bin/bash' /etc/passwd
3、找出/etc/passwd檔案中,包含二位數字或者三位數的行
[[email protected] profile.d]# grep "\<[0-9]\{2,3\}\>" /etc/passwd
4、顯示/proc/meminfo檔案中以大寫或小寫S開頭的行;用三種方式實現。
第一種:[[email protected] profile.d]# grep "^[sS]" /proc/meminfo 第二種:[[email protected] profile.d]# grep -E "^(s|S)" /proc/meminfo 第三種:[[email protected] profile.d]# grep -i "^s" /proc/meminfo
5、使用echo輸出一個絕對路徑,使用egrep取出路徑名,類似執行dirname /etc/passwd的結果。
[[email protected] profile.d]# echo /etc/sysconfig/.etwork-scripts | egrep -o ".*/[^/ ]" | egrep -o ".*/" | egrep -o ".*[^/]"
6、找出ifconfig中的ip地址。要求結果只顯示IP地址;
[[email protected] profile.d]# ifconfig | grep "inet" | head -1 |grep -Eo "inet (addr :)?([0-9]*\.){3}[0-9]*" | grep -Eo '([0-9]*\.){3}[0-9]*'
7、VIM定製自動縮排四個字元。
編輯vim配置檔案: vim /etc/vim/vimrc 在檔案最後加入以下行: set shiftwidth=4
8、編寫指令碼,實現自動新增三個使用者,並計算三個使用者的uid之和。
#!/bin/bash # #[ $# -ne 3 ] && echo "Args must be 3" && exit id $1 &> /dev/null && echo "$1 exists" || useradd $1 id $2 &> /dev/null && echo "$2 exists" || useradd $2 id $3 &> /dev/null && echo "$3 exists" || useradd $3 ID1=$(id $1 -u) ID2=$(id $2 -u) ID3=$(id $3 -u) sumid=$[ID1+ID2+ID3] echo $sumid
9、find用法及常用用法的例項演示。
Linux中find常見用法示例 #find path -option [ -print ] [ -exec -ok command ] {} \; #-print 將查詢到的檔案輸出到標準輸出 #-exec command {} \; —–將查到的檔案執行command操作,{} 和 \;之間有空格。其實在命令執行的時候"{}"將被find到的結果替換掉,因此將"{}"看成find到的檔案來進行操作就很容易理解這個選項了。 #-ok 和-exec相同,只不過在操作前要詢使用者 ==================================================== -name filename #查詢名為filename的檔案 -iname filename #查詢名為filename的檔案,忽略大小寫(case insensitive) -perm XXX -ugo=rwx 按執行許可權來查詢 -user username #按檔案屬主來查詢 -group groupname #按組來查詢 -mtime -n +n #按檔案更改時間來查詢檔案,-n指n天以內,+n指n天以前 -atime -n +n #按檔案訪問時間來查GIN: 0px">-perm -user username #按檔案屬主來查詢 -group groupname #按組來查詢 -mtime -n +n #按檔案更改時間來查詢檔案,-n指n天以內,+n指n天以前 -atime -n +n #按檔案訪問時間來查詢檔案,-n指n天以內,+n指n天以前 -ctime -n +n #按檔案建立時間來查詢檔案,-n指n天以內,+n指n天以前 -nogroup #查詢無有效屬組的檔案,即檔案的屬組在/etc/groups中不存在 -nouser #查詢無有效屬主的檔案,即檔案的屬主在/etc/passwd中不存 -newer file #查詢修改時間比file更近的檔案 -newer f1 !f2 #查更改時間比f1新但比f2舊的檔案 -ctime -n +n #按檔案建立時間來查詢檔案,-n指n天以內,+n指n天以前 -nogroup #查無有效屬組的檔案,即檔案的屬組在/etc/groups中不存在 -nouser #查無有效屬主的檔案,即檔案的屬主在/etc/passwd中不存 -type b/d/c/p/l/f #查是塊裝置、目錄、字元裝置、管道、符號連結、普通檔案 -size n[c/w/k/M/G] #查長度為n塊[或n位元組]的檔案 `b' for 512-byte blocks (this is the default if no suffix is used) `c' for bytes `w' for two-byte words `k' for Kilobytes (units of 1024 bytes) `M' for Megabytes (units of 1048576 bytes) `G' for Gigabytes (units of 1073741824 bytes) -depth #使查詢在進入子目錄前先行查詢完本目錄 -maxdepth n #指定查詢目錄最大深度 -mindepth n #指定查詢目錄最小深度 -fstype #查位於某一型別檔案系統中的檔案,這些檔案系統型別通常可在/etc/fstab中 找到 -mount #查檔案時不跨越檔案系統mount點 -follow #如果遇到符號連結檔案,就跟蹤連結所指的檔案 -mount #查檔案時不跨越檔案系統mount點 -cpio #對匹配的檔案使用cpio命令,將他們備份到磁帶裝置中 -prune #忽略某個目錄 -inum #通過inode碼查詢檔案 ============================以下英文部分摘自find man手冊========================================== OPERATORS Listed in order of decreasing precedence: ( expr ) Force precedence. Since parentheses are special to the shell, you will normally need to quote them. Many of the examples in this manual page use backslashes for this purpose: `\(...\)' instead of `(...)'. ! expr True if expr is false. This character will also usually need protec‐ tion from interpretation by the shell. -not expr Same as ! expr, but not POSIX compliant. expr1 expr2 Two expressions in a row are taken to be joined with an implied "and"; expr2 is not evaluated if expr1 is false. expr1 -a expr2 Same as expr1 expr2. expr1 -and expr2 Same as expr1 expr2, but not POSIX compliant. expr1 -o expr2 Or; expr2 is not evaluated if expr1 is true. expr1 -or expr2 Same as expr1 -o expr2, but not POSIX compliant. expr1 , expr2 List; both expr1 and expr2 are always evaluated. The value of expr1 is discarded; the value of the list is the value of expr2. The comma operator can be useful for searching for several different types of thing, but traversing the filesystem hierarchy only once. The -fprintf action can be used to list the various matched items into several different output files. =====================================示 例=========================================== $find ~ -name "*.txt" -print #在$HOME中查.txt檔案並顯示 $find . -name "*.txt" -print $find /etc -name "host*" -print #查以host開頭的檔案 $find . -name "[a-z][a-z][0–9][0–9].txt" -print #查以兩個小寫字母和兩個數字開頭 的txt檔案 $find . -perm 755 -print $find . -perm -007 -exec ls -l {} \; #查所有使用者都可讀寫執行的檔案同-perm 777 $find . -size +1000000c -print #查長度大於1Mb的檔案 $find . -size 100c -print # 查長度為100c的檔案 $find . -size +10 -print #查長度超過10塊的檔案(1塊=512位元組) $find /etc -name "passwd*" -exec grep "cnscn" {} \; #看是否存在cnscn使用者 ====================================================== find -name april* 在當前目錄下查詢以april開始的檔案 find -name april* fprint file 在當前目錄下查詢以april開始的檔案,並把結果輸出到 file中 find -name ap* -or -name may* 查詢以ap或may開頭的檔案 find /mnt -name tom.txt -fstype vfat 在/mnt下查詢名稱為tom.txt且檔案系統型別為vfat的 檔案 find /mnt -name t.txt ! -fstype vfat 在/mnt下查詢名稱為tom.txt且檔案系統型別不為vfat 的檔案 find /tmp -name wa* -type l 在/tmp下查詢名為wa開頭且型別為符號連結的檔案 find /home -mtime -2 在/home下查最近兩天內改動過的檔案 find /home -atime -1 查1天之內被訪問過的檔案 find /home -mmin +60 在/home下查60分鐘前改動過的檔案 find /home -amin +30 查最近30分鐘前被訪問過的檔案 find /home -newer tmp.txt 在/home下查更新時間比tmp.txt近的檔案或目錄 find /home -anewer tmp.txt 在/home下查訪問時間比tmp.txt近的檔案或目錄 find /home -used -2 列出檔案或目錄被改動過之後,在2日內被訪問過的檔案 或目錄 find /home -user cnscn 列出/home目錄內屬於使用者cnscn的檔案或目錄 find /home -uid +501 列出/home目錄內使用者的識別碼大於501的檔案或目錄 find /home -group cnscn 列出/home內組為cnscn的檔案或目錄 find /home -gid 501 列出/home內組id為501的檔案或目錄 find /home -nouser 列出/home內不屬於本地使用者的檔案或目錄 find /home -nogroup 列出/home內不屬於本地組的檔案或目錄 find /home -name tmp.txt -maxdepth 4 列出/home內的tmp.txt 查時深度最多為3層 find /home -name tmp.txt -mindepth 3 從第2層開始查 find / -mindepth 3 -maxdepth 5 -name txt在根目錄的第2級和第4級之間查詢 find /home -empty 查詢大小為0的檔案或空目錄 find /home -size +512k 查大於512k的檔案 find /home -size -512k 查小於512k的檔案 find /home -links +2 查硬連線數大於2的檔案或目錄 find /home -perm 0700 查許可權為700的檔案或目錄 find /tmp -name tmp.txt -exec cat {} \;查詢並列印tmp.txt find /tmp -name tmp.txt -ok rm {} \;查詢並刪除tmp.txt,刪除之前確認 ======================================================================= 查詢當前目錄中的所有htm檔案,並將其改名為html檔案。 find . -name "*.htm" -exec mv {} {}l \; ============================================================================= 在/logs目錄中查詢更改時間在5日以前的檔案並刪除它們: $ find logs -type f -mtime +5 -exec -ok rm {} \; ============================================================================ 查詢當天修改過的檔案 # find ./ -mtime -1 -type f -exec ls -l {} \; ============================================================================ 查詢檔案並詢問是否要顯示 [[email protected] class]# find ./ -mtime -1 -type f -ok ls -l {} \; ============================================================================ Create Alias for Frequent Find Operations If you find some thing as pretty useful, then you can make it as an alias. And execute it whenever you want. Remove the files named a.out frequently. # alias rmao="find . -iname a.out -exec rm {} /;" # rmao ============================================================================ 使用find查詢檔案的時候怎麼避開某個檔案目錄 比如要在/usr/sam目錄下查詢不在dir1子目錄之內的所有檔案 find /usr/sam -path "/usr/sam/dir1" -prune -o -print -prune if the file is a directory, do not descend into(落進,降到…裡) it. find path [-path ..] [expression] 在路徑列表的後面的是表示式 -path "/usr/sam" -prune -o -print 是 -path "/usr/sam" -a -prune -o -print 的簡寫表示式,按順序求值, -a 和 -o 都是短路求值,與 shell 的 && 和 || 類似如果 -path "/usr/sam" 為真,則求值 -prune , -prune 返回真,與邏輯表示式為真;否則不求值 -prune,與邏輯表示式為假。如果 -path "/usr/sam" -a -prune 為假,則求值 -print ,-print返回真,或邏輯表示式為真;否則不求值 -print,或邏輯表示式為真。 這個表示式組合特例可以用偽碼寫為 if -path "/usr/sam" then -prune else -print 避開多個資料夾 find /usr/sam \( -path /usr/sam/dir1 -o -path /usr/sam/file1 \) -prune -o -print 圓括號表示表示式的結合。\ 表示引用,即指示 shell 不對後面的字元作特殊解釋,而留給 find 命令去解釋其意義。 查詢某一確定檔案,-name等選項加在-o 之後 find /usr/sam \(-path /usr/sam/dir1 -o -path /usr/sam/file1 \) -prune -o -name "temp" -print ================================================================================================== find + xargs xargs xargs - build and execute command lines from standard input 在使用find命令的-exec選項處理匹配到的檔案時, find命令將所有匹配到的檔案一起傳遞給exec執行。但有些系統對能夠傳遞給exec的命令長度有限制,這樣在find命令執行幾分鐘之後,就會出現溢位錯誤。錯誤資訊通常是“引數列太長”或“引數列溢位”。這就是xargs命令的用處所在,特別是與find命令一起使用。 find命令把匹配到的檔案傳遞給xargs命令,而xargs命令每次只獲取一部分檔案而不是全部,不像-exec選項那樣。這樣它可以先處理最先獲取的一部分檔案,然後是下一批,並如此繼續下去。 在有些系統中,使用-exec選項會為處理每一個匹配到的檔案而發起一個相應的程序,並非將匹配到的檔案全部作為引數一次執行;這樣在有些情況下就會出現程序過多,系統性能下降的問題,因而效率不高;而使用xargs命令則只有一個程序。另外,在使用xargs命令時,究竟是一次獲取所有的引數,還是分批取得引數,以及每一次獲取引數的數目都會根據該命令的選項及系統核心中相應的可調引數來確定。 來看看xargs命令是如何同find命令一起使用的,並給出一些例子。 下面的例子查詢系統中的每一個普通檔案,然後使用xargs命令來測試它們分別屬於哪類檔案 #find . -type f -print | xargs file ./.kde/Autostart/Autorun.desktop: UTF-8 Unicode English text ./.kde/Autostart/.directory: ISO-8859 text\ ...... 在整個系統中查詢記憶體資訊轉儲檔案(core dump) ,然後把結果儲存到/tmp/core.log 檔案中: $ find / -name "core" -print | xargs echo "" >/tmp/core.log 上面這個執行太慢,我改成在當前目錄下查詢 #find . -name "file*" -print | xargs echo "" > /temp/core.log # cat /temp/core.log ./file6 在當前目錄下查詢所有使用者具有讀、寫和執行許可權的檔案,並收回相應的寫許可權: # ls -l drwxrwxrwx 2 sam adm 4096 10月 30 20:14 file6 -rwxrwxrwx 2 sam adm 0 10月 31 01:01 http3.conf -rwxrwxrwx 2 sam adm 0 10月 31 01:01 httpd.conf # find . -perm -7 -print | xargs chmod o-w # ls -l drwxrwxr-x 2 sam adm 4096 10月 30 20:14 file6 -rwxrwxr-x 2 sam adm 0 10月 31 01:01 http3.conf -rwxrwxr-x 2 sam adm 0 10月 31 01:01 httpd.conf 用grep命令在所有的普通檔案中搜索hostname這個詞: # find . -type f -print | xargs grep "hostname" ./httpd1.conf:# different IP addresses or hostnames and have them handled by the ./httpd1.conf:# VirtualHost: If you want to maintain multiple domains/hostnames on your 用grep命令在當前目錄下的所有普通檔案中搜索hostnames這個詞: # find . -name \* -type f -print | xargs grep "hostnames" ./httpd1.conf:# different IP addresses or hostnames and have them handled by the ./httpd1.conf:# VirtualHost: If you want to maintain multiple domains/hostnames on your 注意,在上面的例子中, \用來取消find命令中的*在shell中的特殊含義。 find命令配合使用exec和xargs可以使使用者對所匹配到的檔案執行幾乎所有的命令。