在linux中什麼是一個文件的路徑呢,說白了就是文件存在的地方。在linux的世界中,存在着絕對路徑和相對路徑。php
絕對路徑:路徑的寫法必定由根目錄「/」寫起。例如/usr/local/mysql,這就是絕對路徑。html
相對路徑:路徑的寫法不是由根目錄「/」寫起。例如,首先用戶進入到/,而後再進入到home,命令爲cd /home而後cd test,此時用戶所在路徑爲/home/test。第一個cd命令後跟/home,第二個cd命令後跟test,並無斜槓,這個test是相對於/home目錄來說的,因此叫作相對路徑。node
pwd這個命令打印出當前所在目錄mysql
[root@localhost ~]# pwd /root [root@localhost ~]#
cd進入到某個目錄linux
[root@localhost ~]# cd /usr/local/ [root@localhost local]# pwd /usr/local [root@localhost local]#
./指的是當前目錄sql
../指的是當前目錄的上級目錄數據庫
[root@localhost local]# cd /usr/local/lib/ [root@localhost lib]# pwd /usr/local/lib [root@localhost lib]# cd ./ [root@localhost lib]# pwd /usr/local/lib [root@localhost lib]# cd ../ [root@localhost local]# pwd /usr/local [root@localhost local]#
mkdir建立一個目錄windows
mkdir其實就是make directory的縮寫。其語法爲mkdir [-mp] [目錄名稱],其中-m、-p爲其選項。-m:這個參數用來指定要建立目錄的權限,該參數不經常使用。-p:這個參數是很管用的。api
[root@localhost ~]# mkdir /tmp/test/123 mkdir: cannot create directory 鈥tmp/test/123鈥 No such file or directory [root@localhost ~]# ls /tmp/test ls: cannot access /tmp/test: No such file or directory [root@localhost ~]#
當咱們想建立/tmp/test/123目錄,但是提示不能建立,緣由是/tmp/test目錄不存在。數組
[root@localhost ~]# ls /tmp/test ls: cannot access /tmp/test: No such file or directory [root@localhost ~]# mkdir -p /tmp/test/123 [root@localhost ~]# ls /tmp/test 123 [root@localhost ~]#
你看到這裏,是否是明白-p參數的做用了?沒錯,它的做用就是遞歸建立目錄,即便上級目錄不存在,還有一種狀況就是若是你想要建立的目錄存在的話,會提示報錯,而後你加上-p參數,就不會報錯了。
[root@localhost ~]# ls /tmp/test 123 [root@localhost ~]# mkdir /tmp/test/123 mkdir: cannot create directory 鈥tmp/test/123鈥 File exists [root@localhost ~]# mkdir -p /tmp/test/123 [root@localhost ~]#
rmdir刪除一個目錄
[root@localhost ~]# ls /tmp/test/ 123 [root@localhost ~]# rmdir /tmp/test/123/ [root@localhost ~]# ls /tmp/test/ [root@localhost ~]#
rmdir實際上是rmove directory縮寫,其只有一個選項-p相似與mkdir命令,這個參數的做用是將上級目錄一塊兒刪除。舉個例子吧,新建目錄mkdir -p d1/d2/d3,rmdir -p d1/d2/d3至關因而刪除了d/,d1/d2,d1/d2/d3。若是一個目錄目錄中還有目錄,那麼當你直接rmdir該目錄時,會提示該目錄不爲空。若是你非要刪除不爲空的目錄,那你用rm指令吧。
rm刪除目錄或文件
rmdir只能刪除目錄但不能刪除文件,想要刪除一個文件,則要用rm命令了。rm一樣也有不少選項。
-f:強制的意思,若是不加這個選項,當刪除一個不存在的文件時會報錯。
[root@localhost ~]# ls /tmp/111 ls: cannot access /tmp/111: No such file or directory [root@localhost ~]# rm /tmp/111 rm: cannot remove 鈥tmp/111鈥 No such file or directory [root@localhost ~]# rm -f /tmp/111 [root@localhost ~]#
-i:這個選項的做用是,當用戶刪除一個文件時會提示用戶是否真的刪除。
[root@localhost ~]# ls /tmp/test/123/ 1.txt [root@localhost ~]# rm -i /tmp/test/123/1.txt rm: remove regular file 鈥tmp/test/123/1.txt鈥 y [root@localhost ~]# ls /tmp/test/123/ [root@localhost ~]#
若是刪除,輸入y不然輸入n
-r:當刪除目錄時,加該選項,若是不加這個選項會報錯。rm是能夠刪除不爲空的目錄的。
[root@localhost ~]# ls /tmp/test 123 [root@localhost ~]# rm /tmp/test rm: cannot remove 鈥tmp/test鈥 Is a directory [root@localhost ~]# rm -rf /tmp/test [root@localhost ~]# ls /tmp/test ls: cannot access /tmp/test: No such file or directory [root@localhost ~]#
ls在前面的命令中屢次用到它。如今你已經明白它的含義了吧。沒有錯,就是查看某個目錄或者文件,是list的簡寫。ls後能夠跟一個目錄,也能夠跟一個文件。如下是ls的選項,在這裏筆者並無徹底列出,只是列出了平時使用最多的選項。
-a:所有的文檔都列出,包含隱藏的。linux文件系統中一樣也有隱藏文件。這些隱藏文件的文件名是以.開頭的。例如.test、/root/.12三、/root/.ssh等等,隱藏文件能夠是目錄也能夠是普通文件。
-l:詳細列出文件的屬性信息,包括大小、建立時間、所屬主、所屬組等等。
-d:後邊跟目錄,若是不加這個選項則列出目錄下的文件,加上後只列出目錄自己。
[root@localhost ~]# ls /root/ 1.txt case1.sh continue2.sh for3.sh fun.sh myfile test3.sh test.sh wc anaconda-ks.cfg case2.sh continue.sh for.sh if1.sh test test4.sh test.txt while2.sh break2.sh case.sh for1.sh function1.sh if2.sh test1.sh test5.sh until.sh while.sh break.sh code for2.sh function2.sh if3.sh test2.sh test6.sh users [root@localhost ~]#
cp是copy的簡寫,即拷貝格式爲cp [選項] [源文件] [目標文件],例如想把test1拷貝成test2,這樣既可cp test1 test2,如下介紹幾個常見的選項。
-d:這裏涉及到一個「鏈接」的概念。鏈接分爲軟鏈接和硬連接。在之後的章節會詳細解釋,如今你只要明白這裏的軟鏈接跟windows中的款姐方式相似。若是不加這個-d則拷貝軟鏈接時會把軟鏈接的目標文件拷貝過去,而加上後,其實只是拷貝一個鏈接文件(即快捷方式)。
[root@localhost cptest]# touch test [root@localhost cptest]# ln -s test test1 [root@localhost cptest]# ls -l test test1 -rw-r--r--. 1 root root 0 Sep 10 22:25 test lrwxrwxrwx. 1 root root 4 Sep 10 22:25 test1 -> test [root@localhost cptest]# cp test1 test2 [root@localhost cptest]# ls -l test test1 test2 -rw-r--r--. 1 root root 0 Sep 10 22:25 test lrwxrwxrwx. 1 root root 4 Sep 10 22:25 test1 -> test -rw-r--r--. 1 root root 0 Sep 10 22:25 test2 [root@localhost cptest]# cp -d test1 test3 [root@localhost cptest]# ls -l test test1 test2 test3 -rw-r--r--. 1 root root 0 Sep 10 22:25 test lrwxrwxrwx. 1 root root 4 Sep 10 22:25 test1 -> test -rw-r--r--. 1 root root 0 Sep 10 22:25 test2 lrwxrwxrwx. 1 root root 4 Sep 10 22:26 test3 -> test [root@localhost cptest]#
-r:若是你要拷貝一個目錄,必需要加-r選項,不然你是拷貝不了目錄的。
[root@localhost cptest]# mkdir 123 [root@localhost cptest]# ls 123 test test1 test2 test3 [root@localhost cptest]# cp 123 456 cp: omitting directory 鈥23鈥 [root@localhost cptest]# [root@localhost cptest]# cp -r 123 456 [root@localhost cptest]# ls 123 456 test test1 test2 test3 [root@localhost cptest]#
-i:若是遇到一個文件存在的文件,會問是否覆蓋。
[root@localhost cptest]# ls 123 456 test test1 test2 test3 [root@localhost cptest]# touch 111 [root@localhost cptest]# touch 222 [root@localhost cptest]# cp -i 111 222 cp: overwrite 鈥22鈥 n [root@localhost cptest]#
上例中,touch命令,看字面意思就是摸一下,沒有,若是有這個文件,則會改變文件的訪問時間,若是沒有這個文件就會建立這個文件。
-u:該選項僅當目標文件存在時才生效,若是源文件比目標文件新纔會拷貝,不然不作任何動做。
[root@localhost cptest]# rm -rf * [root@localhost cptest]# ls [root@localhost cptest]# echo 111 > 111 [root@localhost cptest]# cat 111 111 [root@localhost cptest]# echo 222 > 222 [root@localhost cptest]# cat 222 222 [root@localhost cptest]# cp -u 111 222 [root@localhost cptest]# cat 222 222 [root@localhost cptest]# echo aaa > 111 [root@localhost cptest]# cp -u 111 222 cp: overwrite 鈥22鈥 y [root@localhost cptest]# cat 222 aaa [root@localhost cptest]#
mv是移動的意思,是move的簡寫。格式爲:mv [選項] [源文件] [目標文件] ,下面介紹幾個經常使用的選項。
-i:和cp的-i同樣,當目標文件存在時詢問用戶是否要覆蓋。
-u:和cp命令的-u選項一個做用,當目標文件存在時才生效,若是源文件比目標文件新纔會移動,不然不作任何操做。
該命令集中狀況;
1)、目標文件時目錄,並且目標文件不存在;
2)、目標文件時目錄,並且目標文件存在;
[root@localhost mvtest]# mkdir aaa [root@localhost mvtest]# ls aaa [root@localhost mvtest]# mv aaa bbb [root@localhost mvtest]# ls bbb [root@localhost mvtest]# mkdir ccc [root@localhost mvtest]# ls bbb ccc [root@localhost mvtest]# mv bbb ccc [root@localhost mvtest]# ls ccc [root@localhost mvtest]#
3)、目標文件不是目錄,並且目標文件不存在;
4)、目標文件不是目錄,並且目標文件存在;
[root@localhost mvtest]# touch aa [root@localhost mvtest]# ls aa [root@localhost mvtest]# mv aa bb [root@localhost mvtest]# ls bb [root@localhost mvtest]# touch cc [root@localhost mvtest]# ls bb cc [root@localhost mvtest]# mv bb cc mv: overwrite 鈥榗c鈥 y [root@localhost mvtest]# ls cc [root@localhost mvtest]#
cat是比較經常使用的一個命令,即查看一個文件的內容並顯示在屏幕上。
-n:查看文件時,把行號也顯示到屏幕。
[root@localhost mvtest]# echo 1111 > 1.txt [root@localhost mvtest]# echo 2222 >> 1.txt [root@localhost mvtest]# cat -n 1.txt 1 1111 2 2222 [root@localhost mvtest]#
上例中出現了一個 「>>」,這個符號跟前面介紹的「>」的做用都是重定向,即把前面輸出的內容輸出到後面的文件中。「>>」是追加的意思,而用「>」,若是文件中有內容則會刪除文件中的內容,而「>>」則不會。
-A:顯示全部東西,包括特殊字符。
[root@localhost mvtest]# echo 111 > 1.txt [root@localhost mvtest]# echo 222 >> 1.txt [root@localhost mvtest]# cat -n 1.txt 1 111 2 222 [root@localhost mvtest]# cat -A 1.txt 111$ 222$ [root@localhost mvtest]# cat -nA 1.txt 1 111$ 2 222$ [root@localhost mvtest]#
tac實際上是cat的反寫,一樣的功能也是反向打印內容到屏幕上。
[root@localhost mvtest]# echo 1234 > 1.txt [root@localhost mvtest]# echo 5678 >> 1.txt [root@localhost mvtest]# cat -nA 1.txt 1 1234$ 2 5678$ [root@localhost mvtest]# tac 1.txt 5678 1234 [root@localhost mvtest]#
more也是用來查看一個文件內容。當文件內容太多,一屏幕不能顯示全,而你用cat確定是看不到前面的內容的,那麼使用more就能夠解決這個問題。當看完一屏後按空格鍵繼續看下一屏。但看完全部內容後就會退出。若是你想要提早退出,只要按q鍵便可。
less做用跟more同樣,但比more好在能夠上翻下翻。空格鍵一樣能夠翻頁,而按j鍵能夠向下移動,按k鍵向上移動。在使用more和less查看某個文件時,你能夠按「/」鍵,而後輸入一個word回車,這樣就能夠找到word了。若是是多個word能夠按「n」鍵顯示下一個。另外你也能夠不按「/」而是按「?」後面一樣跟word來搜索這個word,惟一不一樣的是「/」是在當前行向下搜索,而「?」是在當前行向上搜索。
head後面直接跟文件名,則顯示文件前10行。若是加-n選項則顯示文件前n行。
[root@localhost mvtest]# head /etc/passwd root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt mail:x:8:12:mail:/var/spool/mail:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin [root@localhost mvtest]# head -n 5 /etc/passwd root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin [root@localhost mvtest]#
tail和head同樣,後面直接跟文件名,則顯示文件最後10行。若是加-n選項則顯示文件最後n行。
[root@localhost mvtest]# tail /etc/passwd user_91:x:1095:100::/home/user_91:/bin/bash user_92:x:1096:100::/home/user_92:/bin/bash user_93:x:1097:100::/home/user_93:/bin/bash user_94:x:1098:100::/home/user_94:/bin/bash user_95:x:1099:100::/home/user_95:/bin/bash user_96:x:1100:100::/home/user_96:/bin/bash user_97:x:1101:100::/home/user_97:/bin/bash user_98:x:1102:100::/home/user_98:/bin/bash user_99:x:1103:100::/home/user_99:/bin/bash user_100:x:1104:100::/home/user_100:/bin/bash [root@localhost mvtest]# tail -n 5 /etc/passwd user_96:x:1100:100::/home/user_96:/bin/bash user_97:x:1101:100::/home/user_97:/bin/bash user_98:x:1102:100::/home/user_98:/bin/bash user_99:x:1103:100::/home/user_99:/bin/bash user_100:x:1104:100::/home/user_100:/bin/bash [root@localhost mvtest]#
-f:動態顯示文件的最後10行,若是文件時不斷增長的,則用-f選項。如 tail -f /var/log/messages
一個linux目錄或者文件,都會有一個所屬主和所屬組。所屬主,即文件的擁有者,而所屬組,即該文件所屬主所在的一個組。linux這樣設置文件屬性的目的是爲了文件的安全。例如,test文件的所屬主是user0,而test1文件的所屬主是user1,那麼user1是不能查看test文件的,相應的user0也是不能查看test1文件的。而後又這樣一個應用,我想要建立一個文件同時讓user0和user1來查看怎麼辦呢?
這事「所屬組」就派上用場了,即,建立一個羣組users,讓user0和user1同屬於users組,而後建立一個文件test2,且其所屬組爲users,那麼user0和user1均可以訪問test2文件。
linux文件屬性不只規定了所屬主和所屬組,還規定了所屬主(user)、所屬組(group)以及其餘用戶(others)對文件的權限。
[root@localhost ~]# ls -l total 128 -rw-r--r--. 1 root root 0 Sep 5 22:51 1.txt -rw-------. 1 root root 1259 Jul 31 17:28 anaconda-ks.cfg -rw-r--r--. 1 root root 210 Sep 4 22:18 break2.sh -rw-r--r--. 1 root root 298 Sep 4 22:11 break.sh -rw-r--r--. 1 root root 304 Sep 4 18:32 case1.sh -rw-r--r--. 1 root root 261 Sep 4 18:39 case2.sh -rw-r--r--. 1 root root 146 Sep 6 02:08 case.sh drwxr-xr-x. 2 root root 21 Sep 4 00:46 code -rw-r--r--. 1 root root 178 Sep 4 23:08 continue2.sh -rw-r--r--. 1 root root 301 Sep 4 22:21 continue.sh drwxr-xr-x. 2 root root 28 Sep 10 22:42 cptest
上例中,用ls -l查看當前目錄下的文件,共顯示了9列內容(用空格劃分列),都表明了什麼含義呢?
第一列,包含的東西有該文件類型、所屬主、所屬組已經其餘用戶對該文件的權限。第一列共10位。其中第一位用來描述該文件的類型。上例中,咱們看到的類型有「d」和「-」,其實除了這兩種外還有「l」,「b」、「c」、「s」等。
d:表示該文件是目錄;
-:表示該文件是普通文件;
l:表示該文件是鏈接文件;
[root@localhost ~]# ls -l /etc/rc.local lrwxrwxrwx. 1 root root 13 Jul 31 17:23 /etc/rc.local -> rc.d/rc.local [root@localhost ~]#
b:表示該文件是塊設備文件,好比磁盤分區;
[root@localhost ~]# ls -l /dev/sda* brw-rw----. 1 root disk 8, 0 Sep 10 18:04 /dev/sda brw-rw----. 1 root disk 8, 1 Sep 10 18:04 /dev/sda1 brw-rw----. 1 root disk 8, 2 Sep 10 18:04 /dev/sda2 [root@localhost ~]#
c:表示該文件是串行端口設備,例如鍵盤、鼠標;
s:表示該文件是套接字文件(socket),用於進程間通訊;
後面的9位,每三個爲一組。均爲rwx三個參數的組合。其中r表明可讀,w表明可寫,x表明可執行。前三位爲所屬主(user)的權限,中間三位爲所屬組(group)的權限,最後三位爲其餘非本羣組(others)的權限。
一個文件的屬性爲-rwxr-xr--,它表明的意思是,該文件時普通文件,文件的擁有者可讀可寫可執行,文件所屬組對其有可讀不可寫可執行,其餘用戶對其只可讀。
對於一個目錄來說,打開這個目錄即爲執行這個目錄,因此任何一個目錄必需要有x權限才能打開並查看該目錄。例如一個目錄的屬性爲drwxr--r--其所屬主爲root,那麼除了root外的其餘用戶是不能打開這個目錄的。
第二列,表示爲連接佔用的節點(inode),若爲目錄時,一般與該目錄地下還有多少目錄有關係。
第三列,表示該文件的所屬主。
第四列,表示該文件的所屬組。
第五列,表示該文件的大小。
第六列、第七列、第八列,表示該文件的建立日期或者最近的修改日期,分別爲月份日期以及時間。
第九列,表示文件名。若是文件名前有個.,則表示該文件是隱藏文件。
更改文件的權限,也就是更改所屬主、所屬組以及他們對應的讀寫執行權限。
1)、更改所屬組chgrp
語法:chgrp [組名] [文件名]
[root@localhost chtest]# groupadd testgroup [root@localhost chtest]# touch test1 [root@localhost chtest]# ls -l test1 -rw-r--r--. 1 root root 0 Sep 11 19:58 test1 [root@localhost chtest]# chgrp testgroup test1 [root@localhost chtest]# ls -l test1 -rw-r--r--. 1 root testgroup 0 Sep 11 19:58 test1 [root@localhost chtest]#
2)、更改文件所屬主chown
語法:chown [-R] [帳號名] [文件名] 或 chown [-R] [帳戶名:組名] [文件名]
這裏的-R選項只做用於目錄,做用是級聯更改,即不只更改當前目錄,連目錄中的目錄或者文件所有更改。
[root@localhost chtest]# mkdir test [root@localhost chtest]# useradd user1 [root@localhost chtest]# ls -ld test drwxr-xr-x. 2 root root 6 Sep 11 21:54 test [root@localhost chtest]# touch test/test2 [root@localhost chtest]# ls -l test total 0 -rw-r--r--. 1 root root 0 Sep 11 21:54 test2 [root@localhost chtest]# chown user1 test [root@localhost chtest]# ls -ld test drwxr-xr-x. 2 user1 root 19 Sep 11 21:54 test [root@localhost chtest]# ls -l test total 0 -rw-r--r--. 1 root root 0 Sep 11 21:54 test2 [root@localhost chtest]# chown -R user1:testgroup test [root@localhost chtest]# ls -ld test drwxr-xr-x. 2 user1 testgroup 19 Sep 11 21:54 test [root@localhost chtest]# ls -l test total 0 -rw-r--r--. 1 user1 testgroup 0 Sep 11 21:54 test2 [root@localhost chtest]#
上例中,首先創建一個目錄test,而後在test目錄下建立一個普通文件test2,由於是以root的身份建立的目錄和文件,因此所屬主以及所屬組都是root。chown user1 test這使test的目錄所屬主由root變爲user1,而後test目錄下的test2文件所屬主以及所屬組仍是root。接着chown -R user1:testgroup test這樣把test連同test目錄下的test2的所屬主以及所屬組都改變了。
3)、更改用戶對文件的讀寫執行權限chmod
在linux中爲了方便更改這些權限,linux使用數字代替rwx,具體規則爲(r:4)、(w:2)、(x:1)、( -:0)。
舉個例子,-rwxrwx---用數組表示就是770,具體是這樣來的:
rwx=4+2+1=7;rwx=4+2+1=7;---=0+0+0=0
語法:chmod [-R] [xyz] [文件名](這裏的xyz,表示數字)
-R:選項做用同chown,級聯更改。
值得提一下的是,在linux系統中,默認一個目錄的權限爲755,而一個文件的默認權限是644。
[root@localhost chtest]# ls -ld test drwxr-xr-x. 2 user1 testgroup 19 Sep 11 21:54 test [root@localhost chtest]# ls -l test total 0 -rw-r--r--. 1 user1 testgroup 0 Sep 11 21:54 test2 [root@localhost chtest]# chmod 750 test [root@localhost chtest]# ls -ld test drwxr-x---. 2 user1 testgroup 19 Sep 11 21:54 test [root@localhost chtest]# ls -l test/test2 -rw-r--r--. 1 user1 testgroup 0 Sep 11 21:54 test/test2 [root@localhost chtest]# chmod -R 700 test [root@localhost chtest]# ls -ld test drwx------. 2 user1 testgroup 19 Sep 11 21:54 test [root@localhost chtest]# ls -l test total 0 -rwx------. 1 user1 testgroup 0 Sep 11 21:54 test2 [root@localhost chtest]#
若是你建立一個目錄,而該目錄不想讓其餘人看到內容,則只須要設置成rwxr-----(740)便可。
chmod還支持使用rwx的方式來設置權限。從以前的介紹中咱們能夠發現,基本上這9個屬性分別是(1)user(2)group(3)others三羣。那麼咱們就可使用u、g、o來表明三羣的屬性。此外,a則表明all亦即所有的三羣。那麼讀寫的屬性就能夠寫成r、w、x。也就可使用下面的方式:
chmod | u g o a |
+(加入) -(除去) =(設定) |
r w x |
文檔或目錄 |
如今我想把一個文件設置成這樣的權限rwxr-xr-x(755),使用這樣的方式改變權限的命令爲:
[root@localhost chtest]# ls -l test/test2 -rwx------. 1 user1 testgroup 0 Sep 11 21:54 test/test2 [root@localhost chtest]# chmod u=rwx,og=rx test/test2 [root@localhost chtest]# ls -l test/test2 -rwxr-xr-x. 1 user1 testgroup 0 Sep 11 21:54 test/test2 [root@localhost chtest]#
另外還能夠針對u、g、o、a增長或者減小某個權限(讀、寫、執行),例如:
[root@localhost chtest]# ls -l test/test2 -rwxr-xr-x. 1 user1 testgroup 0 Sep 11 21:54 test/test2 [root@localhost chtest]# chmod u-x test/test2 [root@localhost chtest]# ls -l test/test2 -rw-r-xr-x. 1 user1 testgroup 0 Sep 11 21:54 test/test2 [root@localhost chtest]# chmod a-x test/test2 [root@localhost chtest]# ls -l test/test2 -rw-r--r--. 1 user1 testgroup 0 Sep 11 21:54 test/test2 [root@localhost chtest]# chmod u+x test/test2 [root@localhost chtest]# ls -l test/test2 -rwxr--r--. 1 user1 testgroup 0 Sep 11 21:54 test/test2 [root@localhost chtest]#
which命令用來查找可執行文件的絕對路徑。which命令只能用來查找PATH環境變量中出現的路徑下的可執行文件。
[root@localhost chtest]# which ls alias ls='ls --color=auto' /usr/bin/ls [root@localhost chtest]# which cd /usr/bin/cd [root@localhost chtest]#
whereis經過預先生成的一個文件列表庫去查找跟給出的文件名相關的文件。
語法:whereis [-bmsu] [文件名稱]
-b:只找binary文件;
-m:只找在說明文件manual路徑下的文件;
-s:只找source來源文件;
-u:沒有說明文檔的文件;
[root@localhost chtest]# whereis passwd passwd: /usr/bin/passwd /etc/passwd /usr/share/man/man1/passwd.1.gz [root@localhost chtest]# whereis -b passwd passwd: /usr/bin/passwd /etc/passwd [root@localhost chtest]# whereis -m passwd passwd: /usr/share/man/man1/passwd.1.gz [root@localhost chtest]#
locate相似whereis,也是經過查找預先生成的文件列表庫來告訴用戶要查找的文件在哪裏。後面直接跟文件名。若是你的linux沒有這個命令,請安裝軟件包mlocate,這個軟件包在你的系統安裝盤裏,後綴名是RPM,隨後介紹find命令會告訴你如何查找這個包。若是你裝的CentOS你可使用這個安裝米寧來安裝yum install -y mlocate。
[root@localhost chtest]# yum install -y mlocate Loaded plugins: fastestmirror Determining fastest mirrors * base: mirrors.shu.edu.cn * extras: mirrors.shu.edu.cn * updates: mirrors.shu.edu.cn base | 3.6 kB 00:00:00 extras | 3.4 kB 00:00:00 updates | 3.4 kB 00:00:00 (1/2): extras/7/x86_64/primary_db | 187 kB 00:00:00 (2/2): updates/7/x86_64/primary_db | 5.2 MB 00:00:09 Resolving Dependencies --> Running transaction check ---> Package mlocate.x86_64 0:0.26-8.el7 will be installed --> Finished Dependency Resolution Dependencies Resolved =========================================================================================================== Package Arch Version Repository Size =========================================================================================================== Installing: mlocate x86_64 0.26-8.el7 base 113 k Transaction Summary =========================================================================================================== Install 1 Package Total download size: 113 k Installed size: 379 k Downloading packages: mlocate-0.26-8.el7.x86_64.rpm | 113 kB 00:00:00 Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : mlocate-0.26-8.el7.x86_64 1/1 Verifying : mlocate-0.26-8.el7.x86_64 1/1 Installed: mlocate.x86_64 0:0.26-8.el7 Complete! [root@localhost chtest]# locate passwd locate: can not stat () `/var/lib/mlocate/mlocate.db': No such file or directory [root@localhost chtest]#
這是由於系統沒有生成那個文件列表庫,你可使用updatedb命令當即生成(更新)這個庫。若是你的服務器上正跑着重要的業務,那麼你最好不要去運行這個命令,由於一旦運行,服務器的壓力會變大。這個數據庫默認狀況下每週更新一次。因此你用locate命令去搜索一個文件,正好是在兩次更新時間段內,那你確定是得不到結果的。你能夠到/etc/updated.cnf去配置這個數據庫生成(更新)規則。
[root@localhost chtest]# updatedb [root@localhost chtest]# locate passwd /etc/passwd /etc/passwd- /etc/pam.d/passwd /etc/security/opasswd /usr/bin/gpasswd /usr/bin/grub2-mkpasswd-pbkdf2 /usr/bin/passwd /usr/include/rpcsvc/yppasswd.h /usr/include/rpcsvc/yppasswd.x /usr/lib/firewalld/services/kpasswd.xml /usr/lib64/security/pam_unix_passwd.so /usr/local/openssl-1.1.1-pre8/apps/passwd.c /usr/local/openssl-1.1.1-pre8/apps/passwd.d /usr/local/openssl-1.1.1-pre8/apps/passwd.o /usr/local/openssl-1.1.1-pre8/doc/man1/passwd.pod /usr/local/openssl-1.1.1-pre8/doc/man3/SSL_CTX_set_default_passwd_cb.pod /usr/local/openssl-1.1.1-pre8/test/recipes/20-test_passwd.t /usr/local/openssl-1.1.1-pre8/test/recipes/90-test_sslapi_data/passwd.txt /usr/local/share/doc/openssl/html/man1/openssl-passwd.html /usr/local/share/doc/openssl/html/man1/passwd.html /usr/local/share/doc/openssl/html/man3/SSL_CTX_get_default_passwd_cb.html /usr/local/share/doc/openssl/html/man3/SSL_CTX_get_default_passwd_cb_userdata.html /usr/local/share/doc/openssl/html/man3/SSL_CTX_set_default_passwd_cb.html /usr/local/share/doc/openssl/html/man3/SSL_CTX_set_default_passwd_cb_userdata.html /usr/local/share/doc/openssl/html/man3/SSL_get_default_passwd_cb.html
find這個搜索工具是筆者用的最多的一個,因此請你務必熟悉它。
語法:find [路徑] [參數]
-atime +n:訪問或者執行時間大於n天的文件;
-ctime +n:寫入、更改inode屬性(例如更改全部者、權限或者鏈接)時間大於n天定的文件;
-mtime +n:寫入時間大於n天的文件;
[root@localhost ~]# find /root -atime 1 /root/cptest/111 /root/cptest/222 /root/mvtest/1.txt [root@localhost ~]# find /root -ctime 1 /root/cptest /root/cptest/111 /root/cptest/222 /root/.lesshst /root/test /root/mvtest /root/mvtest/1.txt [root@localhost ~]# find /root -mtime 1 /root/cptest /root/cptest/111 /root/cptest/222 /root/.lesshst /root/test /root/mvtest /root/mvtest/1.txt [root@localhost ~]#
看到這裏,你對這三個time是否是有些暈了,那麼筆者就先給你介紹一下這三個time屬性。
文件Access time,atime是在讀取文件呢或者執行文件時更改的。
文件的Modified time,mtime是在寫入文件時隨文件內容的更改而更改的 。
文件Cteate time,ctime是在寫入文件、更改全部者、權限或者連接設置時隨inode的內容更改而更改的。
所以,更改文件的內容即會更改mtime和ctime,可是文件的ctime可能會在mtime未發生任何變化時更改,例如,更改了文件的權限,但文件呢內容並無變化
ls -l命令可用來列出文件的atime、ctime和mtime。
ls -lc filename:列出文件的ctime
ls -lu filename:列出文件的atime
ls -l filename:列出文件的mtime
[root@localhost ~]# ls -ldc file1 drwxr-xr-x. 3 root root 32 Sep 12 18:11 file1 [root@localhost ~]# ls -ldu file1 drwxr-xr-x. 3 root root 32 Sep 12 18:12 file1 [root@localhost ~]# ls -ld file1 drwxr-xr-x. 3 root root 32 Sep 12 18:11 file1 [root@localhost ~]#
-name filename:直接查找該文件名的文件,這個是使用最多的了。
[root@localhost ~]# find /root -name test3.sh /root/test3.sh /root/test/test3.sh [root@localhost ~]#
-type type:經過文件的類型查找。type包含了f、b、c、d、l、s等等。
[root@localhost ~]# mkdir file1 [root@localhost ~]# mkdir file1/file2 [root@localhost ~]# touch file1/file3 [root@localhost ~]# touch file1/file2/file4 [root@localhost ~]# find ./file1 -type d ./file1 ./file1/file2 [root@localhost ~]# find ./file1 -type f ./file1/file2/file4 ./file1/file3 [root@localhost ~]#
在前面的內容中年簡單介紹了普通文件(-)、目錄(d)等,在linux文件系統中,主要有如下幾種類型的文件。
1)、正規文件(regular file):就是通常類型的文件,當用ls -l查看某個目錄時,第一個屬性爲「-」的文件就是正規文件,或者叫普通文件。正規文件又可分紅純文字文件(ascii)或者二進制文件(binary)。純文本文件時能夠經過cat、more、less等工具直接查看內容的,而二進制文件並不能。例如咱們用的命令/bin/ls這就是一個二進制文件。
2)、目錄(directory):這個容易理解,就是目錄,跟windows下的文件夾一個意思,只不過在linux中咱們不叫文件夾,而是叫目錄。ls -l查看第一個屬性爲「d」。
3)、鏈接檔(link):ls -l查看第一個屬性爲「l」,相似windows下的快捷方式。這種文件在linux中很常見,並且筆者在平常的系統維護運維工做中用的不少。
4)、設備檔(device):與系統周邊相關的一些檔案,一般都集中在/dev這個目錄之下。一般又分爲兩種:
區塊(block)設備檔:就只一些存儲數據,以提供系統存取的接口設備,簡單的說就是硬盤啦。例如你的一號硬盤的代碼是/dev/hda1等等的檔案。第一個屬性是「b」。
字符(character)設備檔:便是一些串行端口的接口設備,例如鍵盤、鼠標等等。第一個屬性是「c」。