我們知道Linux的目錄結(jié)構(gòu)為樹狀結(jié)構(gòu),最頂級的目錄為根目錄 /。
其他目錄通過掛載可以將它們添加到樹中,通過解除掛載可以移除它們。
在開始本教程前我們需要先知道什么是絕對路徑與相對路徑。
絕對路徑:
路徑的寫法,由根目錄 / 寫起,例如: /usr/share/doc 這個目錄。
相對路徑:
接下來我們就來看幾個常見的處理目錄的命令吧:
你可以使用 man [命令] 來查看各個命令的使用文檔,如 :man cp。
在Linux系統(tǒng)當(dāng)中, ls 命令可能是最常被運行的。
語法:
[root@www ~]# ls [-aAdfFhilnrRSt] 目錄名稱 [root@www ~]# ls [--color={never,auto,always}] 目錄名稱 [root@www ~]# ls [--full-time] 目錄名稱
選項與參數(shù):
將家目錄下的所有文件列出來(含屬性與隱藏檔)
[root@www ~]# ls -al ~
cd是Change Directory的縮寫,這是用來變換工作目錄的命令。
語法:
cd [相對路徑或絕對路徑]
#使用 mkdir 命令創(chuàng)建 30vps 目錄 [root@www ~]# mkdir 30vps #使用絕對路徑切換到 30vps 目錄 [root@www ~]# cd /root/30vps/ #使用相對路徑切換到 30vps 目錄 [root@www ~]# cd ./30vps/ # 表示回到自己的家目錄,亦即是 /root 這個目錄 [root@www 30vps]# cd ~ # 表示去到目前的上一級目錄,亦即是 /root 的上一級目錄的意思; [root@www ~]# cd ..
接下來大家多操作幾次應(yīng)該就可以很好的理解 cd 命令的。
pwd 是 Print Working Directory 的縮寫,也就是顯示目前所在目錄的命令。
[root@www ~]# pwd [-P]
選項與參數(shù):
實例:單純顯示出目前的工作目錄:
[root@www ~]# pwd /root <== 顯示出目錄啦~
實例顯示出實際的工作目錄,而非連結(jié)檔本身的目錄名而已。
[root@www ~]# cd /var/mail <==注意,/var/mail是一個連結(jié)檔 [root@www mail]# pwd /var/mail <==列出目前的工作目錄 [root@www mail]# pwd -P /var/spool/mail <==怎么回事?有沒有加 -P 差很多~ [root@www mail]# ls -ld /var/mail lrwxrwxrwx 1 root root 10 Sep 4 17:54 /var/mail -> spool/mail # 看到這里應(yīng)該知道為啥了吧?因為 /var/mail 是連結(jié)檔,連結(jié)到 /var/spool/mail # 所以,加上 pwd -P 的選項后,會不以連結(jié)檔的數(shù)據(jù)顯示,而是顯示正確的完整路徑啊!
如果想要創(chuàng)建新的目錄的話,那么就使用mkdir (make directory)吧。
語法:
mkdir [-mp] 目錄名稱
選項與參數(shù):
實例:請到/tmp底下嘗試創(chuàng)建數(shù)個新目錄看看:
[root@www ~]# cd /tmp [root@www tmp]# mkdir test <==創(chuàng)建一名為 test 的新目錄 [root@www tmp]# mkdir test1/test2/test3/test4 mkdir: cannot create directory `test1/test2/test3/test4': No such file or directory <== 沒辦法直接創(chuàng)建此目錄啊! [root@www tmp]# mkdir -p test1/test2/test3/test4
加了這個 -p 的選項,可以自行幫你創(chuàng)建多層目錄!
實例:創(chuàng)建權(quán)限為 rwx--x--x 的目錄。
[root@www tmp]# mkdir -m 711 test2 [root@www tmp]# ls -l drwxr-xr-x 3 root root 4096 Jul 18 12:50 test drwxr-xr-x 3 root root 4096 Jul 18 12:53 test1 drwx--x--x 2 root root 4096 Jul 18 12:54 test2
上面的權(quán)限部分,如果沒有加上 -m 來強制配置屬性,系統(tǒng)會使用默認屬性。
如果我們使用 -m ,如上例我們給予 -m 711 來給予新的目錄 drwx--x--x 的權(quán)限。
語法:
rmdir [-p] 目錄名稱
選項與參數(shù):
刪除 30vps 目錄
[root@www tmp]# rmdir 30vps/
將 mkdir 實例中創(chuàng)建的目錄(/tmp 底下)刪除掉!
[root@www tmp]# ls -l <==看看有多少目錄存在? drwxr-xr-x 3 root root 4096 Jul 18 12:50 test drwxr-xr-x 3 root root 4096 Jul 18 12:53 test1 drwx--x--x 2 root root 4096 Jul 18 12:54 test2 [root@www tmp]# rmdir test <==可直接刪除掉,沒問題 [root@www tmp]# rmdir test1 <==因為尚有內(nèi)容,所以無法刪除! rmdir: `test1': Directory not empty [root@www tmp]# rmdir -p test1/test2/test3/test4 [root@www tmp]# ls -l <==您看看,底下的輸出中test與test1不見了! drwx--x--x 2 root root 4096 Jul 18 12:54 test2
利用 -p 這個選項,立刻就可以將 test1/test2/test3/test4 一次刪除。
不過要注意的是,這個 rmdir 僅能刪除空的目錄,你可以使用 rm 命令來刪除非空目錄。
cp 即拷貝文件和目錄。
語法:
[root@www ~]# cp [-adfilprsu] 來源檔(source) 目標(biāo)檔(destination) [root@www ~]# cp [options] source1 source2 source3 .... directory
選項與參數(shù):
-a:相當(dāng)於 -pdr 的意思,至於 pdr 請參考下列說明;(常用)
-d:若來源檔為連結(jié)檔的屬性(link file),則復(fù)制連結(jié)檔屬性而非文件本身;
-f:為強制(force)的意思,若目標(biāo)文件已經(jīng)存在且無法開啟,則移除后再嘗試一次;
-i:若目標(biāo)檔(destination)已經(jīng)存在時,在覆蓋時會先詢問動作的進行(常用)
-l:進行硬式連結(jié)(hard link)的連結(jié)檔創(chuàng)建,而非復(fù)制文件本身;
-p:連同文件的屬性一起復(fù)制過去,而非使用默認屬性(備份常用);
-r:遞歸持續(xù)復(fù)制,用於目錄的復(fù)制行為;(常用)
-s:復(fù)制成為符號連結(jié)檔 (symbolic link),亦即『捷徑』文件;
-u:若 destination 比 source 舊才升級 destination !
用 root 身份,將 root 目錄下的 .bashrc 復(fù)制到 /tmp 下,并命名為 bashrc
[root@www ~]# cp ~/.bashrc /tmp/bashrc [root@www ~]# cp -i ~/.bashrc /tmp/bashrc cp: overwrite `/tmp/bashrc'? n <==n不覆蓋,y為覆蓋
語法:
rm [-fir] 文件或目錄
選項與參數(shù):
將剛剛在 cp 的實例中創(chuàng)建的 bashrc 刪除掉!
[root@www tmp]# rm -i bashrc rm: remove regular file `bashrc'? y
如果加上 -i 的選項就會主動詢問喔,避免你刪除到錯誤的檔名!
語法:
[root@www ~]# mv [-fiu] source destination [root@www ~]# mv [options] source1 source2 source3 .... directory
選項與參數(shù):
復(fù)制一文件,創(chuàng)建一目錄,將文件移動到目錄中
[root@www ~]# cd /tmp [root@www tmp]# cp ~/.bashrc bashrc [root@www tmp]# mkdir mvtest [root@www tmp]# mv bashrc mvtest
將某個文件移動到某個目錄去,就是這樣做!
將剛剛的目錄名稱更名為 mvtest2
[root@www tmp]# mv mvtest mvtest2
Linux系統(tǒng)中使用以下命令來查看文件的內(nèi)容:
你可以使用 man [命令]來查看各個命令的使用文檔,如 :man cp。
由第一行開始顯示文件內(nèi)容
語法:
cat [-AbEnTv]
選項與參數(shù):
檢看 /etc/issue 這個文件的內(nèi)容:
[root@www ~]# cat /etc/issue CentOS release 6.4 (Final) Kernel \r on an \m
tac與cat命令剛好相反,文件內(nèi)容從最后一行開始顯示,可以看出 tac 是 cat 的倒著寫!如:
[root@www ~]# tac /etc/issue Kernel \r on an \m CentOS release 6.4 (Final)
顯示行號
語法:
nl [-bnw] 文件
選項與參數(shù):
實例一:用 nl 列出 /etc/issue 的內(nèi)容
[root@www ~]# nl /etc/issue 1 CentOS release 6.4 (Final) 2 Kernel \r on an \m
一頁一頁翻動
[root@www ~]# more /etc/man_db.config # # Generated automatically from man.conf.in by the # configure script. # # man.conf from man-1.6d ....(中間省略).... --More--(28%) <== 重點在這一行喔!你的光標(biāo)也會在這里等待你的命令
在 more 這個程序的運行過程中,你有幾個按鍵可以按的:
一頁一頁翻動,以下實例輸出/etc/man.config文件的內(nèi)容:
[root@www ~]# less /etc/man.config # # Generated automatically from man.conf.in by the # configure script. # # man.conf from man-1.6d ....(中間省略).... : <== 這里可以等待你輸入命令!
less運行時可以輸入的命令有:
取出文件前面幾行
語法:
head [-n number] 文件
選項與參數(shù):
[root@www ~]# head /etc/man.config
默認的情況中,顯示前面 10 行!若要顯示前 20 行,就得要這樣:
[root@www ~]# head -n 20 /etc/man.config
取出文件后面幾行
語法:
tail [-n number] 文件
選項與參數(shù):
[root@www ~]# tail /etc/man.config # 默認的情況中,顯示最后的十行!若要顯示最后的 20 行,就得要這樣: [root@www ~]# tail -n 20 /etc/man.config