一、定義一個對全部用戶都生效的命令別名,例如:lftps=‘lftp 172.168.0.1/pub’html
在/etc/profile.d/目錄下建立文件alias_glob.sh,添加一下代碼:
node
alias lftps=‘lftp 172.168.0.1/pub’
添加後保存退出,當即生效命令:shell
source /etc/profile.d/alias_glob.sh
二、顯示/etc/passwd文件中不以/bin/bash結尾的行express
grep -v '/bin/bash' /etc/passwd
三、找出/etc/passwd文件中,包含二位數字或者三位數的行vim
[root@localhost profile.d]# grep "\<[0-9]\{2,3\}\>" /etc/passwd
四、顯示/proc/meminfo文件中以大寫或小寫S開頭的行;用三種方式實現。bash
第一種:[root@localhost profile.d]# grep "^[sS]" /proc/meminfo 第二種:[root@localhost profile.d]# grep -E "^(s|S)" /proc/meminfo 第三種:[root@localhost profile.d]# grep -i "^s" /proc/meminfo
五、使用echo輸出一個絕對路徑,使用egrep取出路徑名,相似執行dirname /etc/passwd的結果。dom
[root@localhost profile.d]# echo /etc/sysconfig/.etwork-scripts | egrep -o ".*/[^/ ]" | egrep -o ".*/" | egrep -o ".*[^/]"
六、找出ifconfig中的ip地址。要求結果只顯示IP地址;ide
[root@localhost profile.d]# ifconfig | grep "inet" | head -1 |grep -Eo "inet (addr :)?([0-9]*\.){3}[0-9]*" | grep -Eo '([0-9]*\.){3}[0-9]*'
七、VIM定製自動縮進四個字符。性能
編輯vim配置文件: vim /etc/vim/vimrc 在文件最後加入如下行: set shiftwidth=4
八、編寫腳本,實現自動添加三個用戶,並計算三個用戶的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
九、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 {} \; ============================================================================ 查詢文件並詢問是否要顯示 [root@book 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可使用戶對所匹配到的文件執行幾乎全部的命令。