標籤:des http tar color com get
第一個:type--查詢一個命令的類型
-查詢一個命令為內部或者外部命令的命令;
-linux的眾多命令中,有內部命令和外部命令,這時可以用type命令來查詢一個命令到底是屬於內部命令還是屬於外部命令;
-內部命令和外部命令的區分方法:在系統中有儲存位置的為外部命令,沒有儲存位置的為內部命令,因為內部命令和shell是一塊的,因此查不到位置;
-其實type命令主要是輔助來查看一個命令的協助文檔的,如果用help擷取協助,那麼內部命令的擷取方式為“help 命令”,如果為外部命令則為“命令 --help”。
[[email protected] ~]# type mkdir //mkdir的儲存位置在/bin/目錄下,那麼mkdir就是外部命令
mkdir is /bin/mkdir
[[email protected] ~]# type passwd //passwd也是外部命令
passwd is /usr/bin/passwd
[[email protected] ~]# type cd //cd和help兩個命令則為內部命令,因為他們沒有儲存位置,和shell一塊
cd is a shell builtin
[[email protected] ~]# type help
help is a shell builtin
[[email protected] ~]# type mkdir passwd //後面可以跟多個選項同時查詢命令的位置
mkdir is /bin/mkdir
passwd is /usr/bin/passwd
第二個:whatis--查詢命令的man入口
個人認為whatis命令和type命令一樣,也是一個輔助的命令,輔助man這個協助命令的;
後面的選項為一個命令,查詢該命令用man命令查詢時間的入口。
[[email protected] ~]# whatis passwd //可以看到passwd這個命令man入口有1,和5,那麼可以用“man 5//passwd”或者“man 1 passwd”來擷取相應的passwd命令的協助文檔;
passwd (1) - update user‘s authentication tokens
passwd (5) - password file
passwd (rpm) - The passwd utility for setting/changing passwords using PAM
passwd [sslpasswd] (1ssl) - compute password hashes
[[email protected] ~]# whatis mkdir
mkdir (1) - make directories
mkdir (1p) - make directories
mkdir (2) - create a directory
mkdir (3p) - make a directory
第三個:whereis--尋找程式/手冊/源檔案
whereis也是輔助命令,查詢linux系統中命令的位置以及被查詢命令協助文檔的位置;
[[email protected] zhangsp]# whereis pwd mkdir passwd ls //查看pwd,mkdir,passwd,ls這四個外 //部命令在linux系統中存在的位置,以及該命令協助文檔的位置;
pwd: /bin/pwd /usr/share/man/man1p/pwd.1p.gz /usr/share/man/man1/pwd.1.gz
mkdir: /bin/mkdir /usr/share/man/man1p/mkdir.1p.gz /usr/share/man/man1/mkdir.1.gz /usr/share/man/man3p/mkdir.3p.gz /usr/share/man/man2/mkdir.2.gz
passwd: /usr/bin/passwd /etc/passwd /usr/share/man/man5/passwd.5.gz /usr/share/man/man1/passwd.1.gz
ls: /bin/ls /usr/share/man/man1p/ls.1p.gz /usr/share/man/man1/ls.1.gz
第四個:which--尋找可執行檔(外部命令、指令碼)在linux系統中的儲存位置
which命令搜尋的位置為$PATH;
root使用者和普通使用者的$PATH可以用“echo $PATH”命令查看:
例:下面可以看到,因為cd為內部命令,所以不能在$PATH所存在的路徑中找到,其他的都有位置
[[email protected] zhangsp]# which cd ls mkdir pwd
alias ls=‘ls --color=tty‘
/bin/ls
/usr/bin/which: no cd in (/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin)
/bin/mkdir
/bin/pwd
第五個:locate--在資料庫中進行模糊查詢(包括linux中的檔案和文檔)
由於實在索引中搜尋的,所以速度很快,但也有缺點,當不更新索引庫的時候,可能會出現錯誤!
1)使用updatedb建立/更新locate索引庫(使用命令uadatedb命令來更新索引庫)
[[email protected] ~]# updatedb
2)使用locate尋找包含指定關鍵字的檔案/目錄
[[email protected] ~]# locate inittab //可以查到linux系統中所有包含inittab字元的檔案或者目錄
/etc/inittab
/usr/share/man/man5/inittab.5.gz
/usr/share/terminfo/a/ansi+inittabs
/usr/share/vim/vim70/syntax/inittab.vim
[[email protected] ~]# locate mkdir
/bin/mkdir
/usr/bin/gnomevfs-mkdir
/usr/lib64/perl5/5.8.8/x86_64-linux-thread-multi/auto/POSIX/mkdir.al
/usr/share/man/man1/mkdir.1.gz
/usr/share/man/man1p/mkdir.1p.gz
/usr/share/man/man2/mkdir.2.gz
/usr/share/man/man2/mkdirat.2.gz
/usr/share/man/man3p/mkdir.3p.gz
3)刪除/新增加的檔案對象,需要更新索引庫以後才能體現效果
如:在目前的目錄下建立aaa目錄和bb.txt檔案,將bb.txt複製到aaa目錄中,然後使用locate查詢時沒有結果
的,這時更新一下資料庫索引,在此查詢可以查詢到兩個bb.txt檔案存在的位置。
[[email protected] ~]# ls
anaconda-ks.cfg Desktop install.log install.log.syslog
[[email protected] ~]# touch bb.txt
[[email protected] ~]# cp bb.txt aaa/
[[email protected] ~]# ls aaa/
bb.txt
[[email protected] ~]# locate bb.txt //雖然bb.txt存在,在沒有更新資料庫的時間,查詢bb.txt是沒有結果的
[[email protected] ~]# updatedb
[[email protected] ~]# locate bb.txt //更新之後重新查詢可以查詢到結果
/root/bb.txt
/root/aaa/bb.txt
[[email protected] ~]#
第六個:find--精確多條件尋找檔案或者目錄(一般用於特殊檔案的搜尋)
1)使用 -type 按類型尋找
這裡的類型指的是普通檔案(f)、目錄(d)、塊裝置檔案(b)、字元裝置檔案(c)等。塊裝置指的是成塊讀 取資料的裝置(如硬碟、記憶體等),而字元裝置指的是按單個字元讀取資料的裝置(如鍵盤、滑鼠等)。
如:
--1
[[email protected] ~]# find /boot/ -type l //查詢/boot目錄下的符號連結檔案,如果不指定目錄,則預設為目前的目錄 /boot/grub/menu.lst
[[email protected] ~]# ll -h /boot/grub/menu.lst
lrwxrwxrwx 1 root root 11 02-10 21:03 /boot/grub/menu.lst -> ./grub.conf
--2
[[email protected] ~]# find /boot/ -type d //查詢/boot目錄下有哪些目錄,還有其本身
/boot/
/boot/lost+found
/boot/grub
[[email protected] ~]# ll -F /boot/
總計 6333
-rw-r--r-- 1 root root 67857 2012-11-29 config-2.6.18-348.el5
drwxr-xr-x 2 root root 1024 02-10 21:03 grub/
-rw------- 1 root root 2837626 02-10 21:54 initrd-2.6.18-348.el5.img
drwx------ 2 root root 12288 02-11 04:45 lost+found/
-rw-r--r-- 1 root root 118626 2012-11-29 symvers-2.6.18-348.el5.gz
-rw-r--r-- 1 root root 1282424 2012-11-29 System.map-2.6.18-348.el5
-rw-r--r-- 1 root root 2125660 2012-11-29 vmlinuz-2.6.18-348.el5
[[email protected] ~]#
2)使用 -name 按名稱尋找
[[email protected] ~]# find /boot -name ‘*grub*‘ //尋找/boot目錄下名稱包含“grub”的對象
/boot/grub
/boot/grub/grub.conf
3)組合多個條件進行尋找
--可使用-a同時匹配多個條件(都必須滿足), -a可省略,例如可只列出/boot目錄下名稱包含“grub”的普通文 件
[[email protected] ~]# find /boot/ -name ‘*grub*‘ -a -type f
/boot/grub/grub.conf
[[email protected] ~]# find /boot/ -name ‘*grub*‘ -type f
/boot/grub/grub.conf
--可使用-o同時匹配多個條件(只需滿足其中任何一個),-o 不可省略,例如尋找/boot目錄下名稱以vmlinuz開 頭和名稱為menu.lst的對象
[[email protected] ~]# find /boot -name "vmlinuz*" -o -name "menu.lst"
/boot/vmlinuz-2.6.18-348.el5
/boot/grub/menu.lst
註:在寫萬用字元的時候,用單引號和雙引號效果相同
4)使用 -size 按大小尋找,可指定容量單位k(小寫)、M、G
--通過“-size +大小”指定是否超過指定的容量。例如在/boot目錄下尋找容量超過2MB的檔案
[[email protected] ~]# find /boot -size +2M
/boot/initrd-2.6.18-348.el5.img
/boot/vmlinuz-2.6.18-348.el5
[[email protected] ~]# ll -h /boot/initrd-2.6.18-348.el5.img /boot/vmlinuz-2.6.18-348.el5
-rw------- 1 root root 2.8M 02-10 21:54 /boot/initrd-2.6.18-348.el5.img
-rw-r--r-- 1 root root 2.1M 2012-11-29 /boot/vmlinuz-2.6.18-348.el5
--通過“-size -大小”指定是否小於指定的容量,例如在/boot/grub目錄下尋找容量小於2KB的普通檔案
[[email protected] ~]# find /boot/grub/ -size -2k -type f
/boot/grub/device.map
/boot/grub/grub.conf
/boot/grub/stage1
[[email protected] ~]# ll -h /boot/grub/device.map /boot/grub/grub.conf /boot/grub/stage1
-rw-r--r-- 1 root root 63 02-10 21:03 /boot/grub/device.map
-rw------- 1 root root 598 02-10 21:03 /boot/grub/grub.conf
-rw-r--r-- 1 root root 512 02-10 21:03 /boot/grub/stage1
5)使用 -exec 指定可執行語句對尋找結果進行處理
在處理語句中,以 {} 代替尋找結果、最後以 \; 表示處理結束(注意與前面的執行語句之間隔一個空格)。
--上述find尋找/boot目錄下大於2MB檔案操作,可直接用 -exec接上ls命令來列出詳細屬性
[[email protected] ~]# find /boot -size +2M -exec ls -lh {} \;
-rw------- 1 root root 2.8M 02-10 21:54 /boot/initrd-2.6.18-348.el5.img
-rw-r--r-- 1 root root 2.1M 2012-11-29 /boot/vmlinuz-2.6.18-348.el5
--在/boot/grub目錄下尋找容量小於2KB的普通檔案也可以直接用-exec街上ls命令列出詳細屬性
[[email protected] ~]# find /boot/grub/ -size -2k -type f -exec ls -lh {} \;
-rw-r--r-- 1 root root 63 02-10 21:03 /boot/grub/device.map
-rw------- 1 root root 598 02-10 21:03 /boot/grub/grub.conf
-rw-r--r-- 1 root root 512 02-10 21:03 /boot/grub/stage1
6)使用 -mtime 按是否在指定日期內修改過進行尋找
find -name "file?.*" -mtime +30 //在目前的目錄下尋找30天之前(-mtime +30)修改過的檔案
find -name "file?.*" -mtime -30 //在目前的目錄下尋找30天之內(-mtime +30)修改過的檔案
find -name "file?.*" -mtime 30 //在目前的目錄下查詢第30天(-mtime 30)修改過的檔案
關於+30:為>31;-30為<29;30為=30(30-31)
擴充
7)使用 -user 、-group尋找屬於指定使用者、組的文檔
--查詢根目錄下屬於zhangsp使用者的目錄
[[email protected] ~]# find / -user zhangsp -type d //查詢根目錄下屬於zhangsp使用者的目錄
/home/zhangsp
/home/zhangsp/.mozilla
/home/zhangsp/.mozilla/plugins
...
--在/dev/目錄下尋找屬於列印組lp的裝置,並用ls列出其詳細屬性
[[email protected] ~]# find /dev/ -group lp -exec ls -lh {} \;
crw-rw---- 1 root lp 6, 0 02-12 12:36 /dev/lp0
crw-rw---- 1 root lp 99, 3 02-12 12:35 /dev/parport3
crw-rw---- 1 root lp 99, 2 02-12 12:35 /dev/parport2
crw-rw---- 1 root lp 99, 1 02-12 12:35 /dev/parport1
crw-rw---- 1 root lp 99, 0 02-12 12:35 /dev/parport0
[[email protected] ~]#
8)使用 -perm 尋找指定許可權的文檔
在/etc/init.d/目錄下尋找以auto開頭,許可權正好是755的文檔
[[email protected] ~]# find /etc/init.d/ -name ‘auto*‘ -perm 0755
/etc/init.d/autofs
[[email protected] ~]# find /etc/init.d/ -name ‘auto*‘ -perm 0755 -exec ls -lh {} \;
-rwxr-xr-x 1 root root 3.9K 2012-11-12 /etc/init.d/autofs
備忘:touch命令--可以修改已經存在的檔案的修改日期,也可以建立一個空檔案並制定其日期 (參數-t)
在find的第六個沒有進行測試,可以藉助於touch來進行測試;
過程:
[[email protected] test]# date //當前日期為2014年2月12日
2014年 02月 12日 星期三 22:22:57 CST
[[email protected] test]# touch -t 1308121030 file1.doc //建立日期為13年8月12日10點30分的檔案file.doc
[[email protected] test]# touch -t 02101020 file2.doc //不寫年份標示當前年份
[[email protected] test]# touch -t 1109291122 file3.doc //建立file3.doc檔案的日期為11年9月29日11點22分
[[email protected] test]# ll -h file* //用ll可以查看檔案的日期
-rw-r--r-- 1 root root 0 2013-08-12 file1.doc
-rw-r--r-- 1 root root 0 02-10 10:20 file2.doc
-rw-r--r-- 1 root root 0 2011-09-29 file3.doc
[[email protected] test]# ls
file1.doc file2.doc file3.doc
[[email protected] test]# find /home/test/ -name ‘file*‘ -mtime +40
/home/test/file1.doc
/home/test/file3.doc
[[email protected] test]# find /home/test/ -name ‘file*‘ -mtime +40 -exec ls -lh {} \;
//顯示40之前修改的檔案,可以看到有兩個形式上開起來日期比較老的檔案
-rw-r--r-- 1 root root 0 2013-08-12 /home/test/file1.doc
-rw-r--r-- 1 root root 0 2011-09-29 /home/test/file3.doc
[[email protected] test]# find /home/test/ -name ‘file*‘ -mtime -40 -exec ls -lh {} \;
//顯示/home/test/目錄下最近40天修改的檔案
-rw-r--r-- 1 root root 0 02-10 10:20 /home/test/file2.doc
總結:
type,whatis,which,whereis都是針對命令的:
type,whatis一般用來查看該命令是否為內外部命令的;
which搜尋的路徑為該使用者的環境變數,而whereis則在命令相關目錄和命令協助的相關目錄 搜尋;
locate和find是針對檔案:
locate在索引中搜尋,速度快,但是需要更新資料庫;
find在linux檔案系統中搜尋,慢,但是全;
。。。學了那麼久,有的東西學了忘,忘了學,之後還會忘,好記性不如爛筆頭,做一次筆記一個小時,寫一篇部落格需要兩個小時,全當加深記憶了!
以前總是以為一個命令後面只能跟一個選項,後來自己也用過好多選項,如:type pwd whatis passwd這條命令,明明知道type後面可以跟很多選項,還是習慣type pwd,type whatis。。。分開來用,以後得改了,有利於提高效率!