![]() |
![]() |
![]() |
![]() |
![]() |
語法:type [-otpiton] COMMAND | ||
指令名稱/功能/命令使用者 | 選項 | 功能 |
type/ 顯示指令類型/Any |
-a | 顯示指令所有可能的類型 |
-t | 顯示指令的種類(alias/file or builtin) | |
-P | 根據 PATH 找指令(類似 which) |
$ type -a pwd ←看一下指令〝pwd〞放在那個目錄 pwd is a shell builtin ←〝pwd〞其一為shell 內建 pwd is /bin/pwd ←〝pwd〞其二在〝/bin/pwd〞 $ type type ←看一下指令〝type〞放在那個目錄 type is a shell builtin ←shell 內建 $ type -a ls ←搜尋指令〝ls〞放在那個目錄 ls is aliased to `ls --color=tty' ←〝ls〞其一為〝ls --color=tty〞的別名 ls is /bin/ls ←〝ls〞其二在〝/bin/ls〞 $ type shadow ←搜尋文字檔〝shadow〞放在那個目錄 bash: type: shadow: not found ←type 一般用來找執行檔,或放到環境變數 PATH 內的檔案,否則會找不到 |
$ which pwd ←看一下指令〝pwd〞放在那個目錄 /bin/pwd $ which type ←找指令〝type〞 /usr/bin/which: no cd in (/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/ usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin:/root/bin) ← 在環境變數 PATH 內找不到〝type〞(無搜尋 shell 內建指令的功能) $ which ls ←搜尋指令〝ls〞 alias ls='ls --color=tty' ← 執行〝ls〞實際上是執行〝ls --color=tty〞 /bin/ls ←〝ls〞所在路徑 $ which shadow ←搜尋文字檔〝shadow〞 /usr/bin/which: no cd in (/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/ usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin:/root/bin) ←路徑找不到〝shadow〞 |
語法:whereis [-otpiton] COMMAND | ||
指令名稱/功能/命令使用者 | 選項 | 功能 |
whereis/ (where is file) 預設路徑尋找檔案/ Any |
-b | 搜尋執行檔的相關路徑(/bin、/sbin、/usr/bin 等) |
-B | 指定搜尋執行檔路徑 | |
-l(小寫 L) | 列出搜尋的路徑 | |
-m | 搜尋說明檔的相關路徑(/usr/share/man 等) | |
-M | 指定說明檔搜尋的路徑 | |
-s | 搜尋原始碼的相關路徑(/usr/src 等) | |
-S | 指定原始碼搜尋的路徑 |
$ whereis -l ←查詢 whereis 搜尋 執行檔/說明檔/原始碼 的路徑 bin: /usr/bin ←〝bin:〞開頭即為找執行檔的路徑 bin: /usr/sbin bin: /usr/lib bin: /etc bin: /usr/etc 中間略 man: /usr/share/man/pl ←〝man:〞開頭即為找 man page 說明文件的路徑 man: /usr/share/man/man7 中間略 src: /usr/src/kernels ←〝src:〞開頭即為找原始碼的路徑 src: /usr/src/debug |
$ whereis -m zip ←搜尋〝zip〞的 man page 文件 zip: /usr/share/man/man1/zip.1.gz $ whereis -b zip ←搜尋〝zip〞執行檔 zip: /usr/bin/zip $ whereis -l | grep 'src:' ←列出搜尋原始碼的路徑 src: /usr/src/kernels src: /usr/src/debug |
# cat /etc/updatedb.conf PRUNE_BIND_MOUNTS = ←"yes" 開啟這些規則,"no" 關閉 PRUNEFS = ←索引資料庫排除的 filesystem (預設是排除罕見和光碟的 filesystem) PRUNENAMES = ←搜索時不搜索文件名中包含這些關鍵字的文件 PRUNEPATHS = ← 索引資料庫排除索引和更新的目錄 |
語法:locate [-otpiton] file/directroy | ||
指令名稱/功能/命令使用者 | 選項 | 功能 |
locate/ 硬碟索引搜尋/ Any |
-b | 只列出符合〝基底檔名〞且可配合萬用字元搜尋 |
-n # | 最多列出 # 個結果(〝#〞為數字) | |
-r | 使用正規表示法搜尋 | |
-i | 忽略大小寫 | |
-d | 指定索引資料庫 |
$ locate 586 ←搜尋檔案字串〝586〞 locate: can not open `/var/lib/mlocate/mlocate.db': No such file or directory ↑ 如出現找不到〝mlocate.db〞可能還沒執行〝updatedb〞所產生的索引資料庫 $ su - root ←請出〝root〞來手動更新〝updatedb〞 Password: # updatedb ←(會花一些時間產生索引資料庫) # locate 586 ←再搜尋〝586〞看看 /lib/modules/2.6.23.1-42.fc8/kernel/arch/i386/crypto/aes-i586.ko /usr/lib/perl5/5.8.8/pod/perl586delta.pod /usr/lib/rpm/i586-linux/macros ←路徑中有〝586〞也算 /usr/share/man/man1/perl5866delta.1.gz |
$ locate -b apple?? ←使用〝基底檔名〞配和萬用字元搜尋字串〝apple〞 /home/aaa/.gconf/apps/panel/applets /usr/share/terminfo/a/apple2e /usr/share/terminfo/a/appleII |
$ locate -b t[!o-p]e ←使用〝基底檔名〞配和萬用字元搜尋 $ locate -i x11 ←忽略大小寫 $ locate -n 5 poweroff ←最多列出 5 個結果 $ locate -d /var/lib/mlocate/office_hd.db file ←指定索引資料庫 $ locate -r 'x\{3\}' ←使用正規表示法搜尋三個相連的〝x〞的檔案或路徑 |
$ find / -name 'apple??'←從根目錄開始,依樣板檔名〝apple??〞搜尋 $ find / -name 'apple??' 2> /dev/null ←同上,但濾掉錯誤輸出(常用) /usr/src/kernels/3.10.0-693.el7.x86_64/include/config/hid/apple.h /usr/src/kernels/3.10.0-693.el7.x86_64/include/config/backlight/apple.h $ find /etc /usr -name "read*" ←搜尋的路徑可以不止一個 |
如是一般的登入者,可能沒足夠的權限進入每層目錄去搜索而產生錯誤的輸出,故畫面會很雜亂;此時可配合錯誤輸出重定向 2> /dev/null濾掉錯誤輸出,或關掉 fd stderr 寫成 find / -name 'aple??' 2>&- 輸出畫面就乾淨多了。
find 進階用法如下:語法:find [path] [-otpiton][--option] expression | |||
指令名稱/功能/命令使用者 | 選項 | 功能 | note |
find/ 終極檔案搜尋/ Any |
依檔案檔名或目錄或 filesystem | ||
-name "PATTERN" | 樣板(pattern)檔名搜尋 | PATTEN 可配合萬用字元樣板,但只可找基底檔名(base name)即檔案去掉路徑 | |
-iname "PATTERN" | 同 -name 但不分大小寫 | ||
-regex "PATTERN" | 正規表示法搜尋 | PATTEN 支援正規表示法 | |
-regextype | 變更正規表示法所支援的種類 | 選項有: 〝emacs〞(預設) 、〝posix-basic〞、〝posix-egrep〞、〝posix-extended〞、〝awk〞、〝grep〞、〝egrep〞等 |
|
-iregex | 同 -regex 但不分大小寫 | ||
-path "PATTERN" | 路徑樣板搜尋 | 和選項〝 -name〞 用法類似,但選項〝 -name〞無法對路徑符號的〝/〞或〝./〞正確匹配,用此選項可克服。 |
|
-ipath "PATTERN" | 同 -path 但不分大小寫 | ||
-prune | 排除 -path 或 -ipath 指定目錄內的檔案 | 要配合和選項〝 -path〞使用 | |
-maxdepth # | 最大搜尋 # 層目錄(# 為數字) | 如 #=1,不會搜尋子目錄 | |
-mindepth # | 最少搜尋 # 層目錄(# 為數字) | 如 #=1,會搜尋任一階目錄 | |
-fstype FILESYSTEM | 指定要搜尋的 filesystem | 常見的 filesystem 有 Linux 的 ext2/ext3/ext4//reiserFS DOS 的 vfat, Windows 的 ntfs (fuseblk) CD/DVD-ROM 的〝iso9660,DVD-ROM 的 〞udf APPLE 的 hfs,網路的 nfs 等 |
|
-xdev | 只搜尋目前的 filesystem | 例如根目錄掛載了不同的 filesystem,但只找和根目錄相同的 filesystem | |
-mount | 同 -xdev | ||
依檔案大小 | |||
-size [+][-]# [bckMG] |
依檔案大小搜尋,可接的項目有 〝b〞:檔案所佔的 Block(磁區),Bolock=512B 〝c〞:為 byte 〝k〞:為 1024 byte 〝M〞:為 10242 byte 〝G〞:為 10243 byte |
||
-empty | 搜尋大小為 0 的檔案或空目錄 | ||
依檔案類型 | |||
-type [bcdpfls] | 檔案類型有可接的項目有 〝b〞:區塊裝置(block device) 〝c〞:字元裝置(character device) 〝d〞:目錄(directory) 〝p〞:具名管線(named pipe) 〝f〞:正規檔(regular file) 〝l〞:符號連結檔(symbolic link) 〝s〞:socket |
||
-links [+][-]# | 找硬連結檔 | # 為連結數 | |
-follow | 排除符號連結檔 | ||
依權限或擁有者屬性 | |||
-perm[+][-]# | 依權限的數字表示法搜尋 | ||
-user OWNER_NAME | 搜尋指定擁有者的檔案 | ||
-group GROUP_NAME | 搜尋指定群組的檔案 | ||
-uid # | 搜尋檔案的 UID | ||
-gid # | 搜尋檔案的群組 GID | ||
-nouser | 搜尋無效擁有者的檔案 | ||
-nogroup | 搜尋無效群組的檔案 | ||
依時間 | |||
-atime[+][-]# | 搜尋 atime 時間的檔案 | # 單位為天 | |
-ctime[+][-]# | 搜尋 ctime 時間的檔案 | # 單位為天 | |
-mtime[+][-]# | 搜尋 mtime 時間的檔案 | # 單位為天 | |
-amin | 搜尋 atime 時間的檔案 | # 單位為分 | |
-cmin | 搜尋 ctime 時間的檔案 | # 單位為分 | |
-mmim | 搜尋 mtime 時間的檔案 | # 單位為分 | |
-anewer ref_file | 搜尋比參考檔的 atime 還新的檔案 | ||
-cnewer ref_file | 搜尋比參考檔的 ctime 還新的檔案 | ||
-newer ref_file | 搜尋比參考檔的 mtime 還新的檔案 | ||
-daystart | 搜尋從本日開始算天數的檔案 | 單位為天,要配合 -amin,-atime,-cmin,-ctime,-mmin,-mtime 等選項 | |
控制/執行/輸出/其他 | |||
輸出到 stdout (預設值) | |||
-exec 指令 {} \; | 將搜尋到的結果交給後續的指令 | ||
-ok 指令 {} \; | 同 -exec 但會詢問 | ||
-delete | -把搜尋到的檔案直接刪除 | ||
-a(and) ,-o(or) -not(!) | 邏輯篩選要搜尋的檔案 , 〝-a(and)〞同〝&&〞(傳回值為 0 執行) 〝-o(or)〞同〝||〞 〝-not(!)〞為邏輯的 NOT(反向) |
$ cd / $ find -path './et*et' 2>&- ←搜尋工作目錄內(目前在根目錄),路徑樣板 './et*et' 的東東 ./etc/issue.net ./etc/alternatives/servlet ./etc/auto.net $ find / -path '/et*et' 2>&- ←同上,但指定路徑開始搜尋 ./etc/issue.net ./etc/alternatives/servlet ./etc/auto.net |
# find /usr -path '/usr/lib' -a -prune -o -name 'applet.*' ↑ 搜尋〝/usr〞目錄內,但排除目錄〝/usr/lib〞,檔名為〝applet.*〞的檔案 /usr/share/pygtk/2.0/defs/applet.defs /usr/share/nm-applet/applet.glade /usr/share/system-config-printer/applet.pyc /usr/share/system-config-printer/applet.png /usr/share/system-config-printer/applet.py /usr/share/system-config-printer/applet.glade /usr/share/system-config-printer/applet.pyo |
# find -regex './.*\.txt' ←用正規表示法搜尋工作目錄下副檔名為〝.txt〞的檔案 # find / -regex '.*/bin' ←用正規表示法從根目錄開始搜尋〝/bin〞 # find / -regex '.*apple.*' ←用正規表示法從根目錄開始搜尋任何有〝apple〞字串的檔案 # find / -regextype posix-extended -regex '.*ap+le.*' ←指定延伸正規表示法 |
$ find /etc \( -name '*.conf' -o -name '*.config' \) ↑ 搜尋目錄〝/etc〞內副檔名〝conf〞或〝config〞的檔案(語法內如需〝()〞要加跳脫字元〝\〞) $ find / -mindepth 7 -name "*ee" ←搜尋樣板〝*ee〞且目錄要 >= 7 層 $ find / -name "myfile" -fstype vfat ←只搜尋 DOS FAT filesystem 的檔案 $ find / -name "myfile" ! -fstype fuseblk ←排除搜尋 Windows NTFS(fuseblk)的檔案 $ find ./ ! -name "*.thml" ←排除工作工目錄內副檔名不是 html 的檔案 $ find -not -name "*.thml" ←同上例〝!〞也可寫成〝-not〞 $ find -name *.jpg -delete ←把搜尋到的檔案直接刪除 |
$ find /etc -size 2b -exec ls -l {} \; ←列出〝/etc〞內檔案大小佔二個 blocks (512b~1024b)的檔案,且交給指令〝ls -l〞列出 $ find ~ -size 1024c ←搜尋家目錄檔案大小=1024B的檔案 $ find ./ -size -2k ←搜尋工作目錄檔案大小 < 2k的檔案 $ find my_dir/ -size +1G ←搜尋目錄〝my_dir〞且 > 1GB 的檔案 $ find -empty ←搜尋工作目錄內,檔案大小為 0 或空的目錄 |
$ find / -empty ! -type f ←搜尋工作目錄內,空的目錄(不包含檔案) $ find / -links 6 -not -type d ←搜尋檔案硬連結數=6的檔案(不包含目錄) $ find / -type d -name "man1" ←搜尋名稱叫〝man1〞的目錄 |
$ find /tmp -user aaa ←搜尋〝/tmp〞目錄,屬於〝aaa〞擁有者的檔案 $ find /tmp -group root ←搜尋〝/tmp〞目錄,群組屬於〝root〞的檔案 $ find /home /tmp -nouser ←搜尋不屬於任何人的檔案(一般用來找帳號已被取消,但之前留在硬碟的檔案) $ find /home -uid 501 ←搜尋〝/home〞目錄內,uid 為 501 的檔案 |
$ find / -perm 0444 -exec ls -l {} \; ←搜尋檔案權限為〝0444〞即權限為〝r-- r-- r--〞的檔案,並用指令〝ls -l〞輸出 $ find / -perm -0444 ←搜尋檔案權限為〝0444〞且邏輯為 AND,即全部都符合才成立的檔案 $ find / -perm +0444 ←搜尋檔案權限為〝0444〞且邏輯為 OR ,即只要符合其一就成立 |
〝rw- r-- rw-〞 〝r-x r-x r--〞 〝rwx r-x rwx〞 〝r-x r-- r--〞也就是說上面範圍,三個紅字的部分一定要都符合(其餘非紅字部分不管) ,相對的 find / +perm +0444,〝+〞表示邏輯的 OR,即只要符合其一就成立,故即找到的檔案其權限可能如下:
〝rwx rwx rwr〞 〝r-x --x r--〞 〝rwx --x -wx〞 〝--x r-- ---〞也就是說,三個紅字的部分,只符合其中的一項即可。
$ find ~ -mtime -5 ←搜尋在家目錄內, 5 天內變更過的檔案(單位是天) $ find ~ -mtime +5 ←搜尋在家目錄內, 大於 5 天變更過的檔案 $ find ~ -mtime 5 ←搜尋在家目錄內, 剛好在 5 天前變更過的檔案 |
$ find /tmp -mmin -30 -type f ←搜尋在〝/tmp〞目錄內, 30 分鐘內變更過的檔案(注意單位是分) $ find ~/ -newer ref_file ←搜尋在家目錄內,mtime 較參考檔〝ref_file〞還新的檔案 $ find ~/ -anewer ref_file ←搜尋在家目錄內,atime 較〝ref_file〞還新的檔案 |