咱們知道Linux的目錄結構爲樹狀結構,最頂級的目錄爲根目錄 /。bash
其餘目錄經過掛載能夠將它們添加到樹中,經過解除掛載能夠移除它們。app
在開始本教程前咱們須要先知道什麼是絕對路徑與相對路徑。less
接下來咱們就來看幾個常見的處理目錄的命令吧:ide
你可使用 man [命令] 來查看各個命令的使用文檔,如 :man cp。spa
在Linux系統當中, ls 命令多是最常被運行的。orm
語法:blog
[root@www ~]# ls [-aAdfFhilnrRSt] 目錄名稱 [root@www ~]# ls [--color={never,auto,always}] 目錄名稱 [root@www ~]# ls [--full-time] 目錄名稱
選項與參數:教程
將家目錄下的全部文件列出來(含屬性與隱藏檔)遞歸
[root@www ~]# ls -al ~
cd是Change Directory的縮寫,這是用來變換工做目錄的命令。圖片
語法:
cd [相對路徑或絕對路徑] #使用 mkdir 命令建立 runoob 目錄 [root@www ~]# mkdir runoob #使用絕對路徑切換到 runoob 目錄 [root@www ~]# cd /root/runoob/ #使用相對路徑切換到 runoob 目錄 [root@www ~]# cd ./runoob/ # 表示回到本身的家目錄,亦便是 /root 這個目錄 [root@www runoob]# cd ~ # 表示去到目前的上一級目錄,亦便是 /root 的上一級目錄的意思; [root@www ~]# cd ..
接下來你們多操做幾回應該就能夠很好的理解 cd 命令的。
pwd 是 Print Working Directory 的縮寫,也就是顯示目前所在目錄的命令。
[root@www ~]# pwd [-P]
選項與參數:
實例:單純顯示出目前的工做目錄:
[root@www ~]# pwd /root <== 顯示出目錄啦~
實例顯示出實際的工做目錄,而非連結檔自己的目錄名而已。
[root@www ~]# cd /var/mail <==注意,/var/mail是一個連結檔 [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 # 看到這裏應該知道爲啥了吧?由於 /var/mail 是連結檔,連結到 /var/spool/mail # 因此,加上 pwd -P 的選項後,會不以連結檔的數據顯示,而是顯示正確的完整路徑啊!
若是想要建立新的目錄的話,那麼就使用mkdir (make directory)吧。
語法:
mkdir [-mp] 目錄名稱
選項與參數:
實例:請到/tmp底下嘗試建立數個新目錄看看:
[root@www ~]# cd /tmp [root@www tmp]# mkdir test <==建立一名爲 test 的新目錄 [root@www tmp]# mkdir test1/test2/test3/test4 mkdir: cannot create directory `test1/test2/test3/test4': No such file or directory <== 沒辦法直接建立此目錄啊! [root@www tmp]# mkdir -p test1/test2/test3/test4
加了這個 -p 的選項,能夠自行幫你建立多層目錄!
實例:建立權限爲 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
上面的權限部分,若是沒有加上 -m 來強制配置屬性,系統會使用默認屬性。
若是咱們使用 -m ,如上例咱們給予 -m 711 來給予新的目錄 drwx--x--x 的權限。
語法:
rmdir [-p] 目錄名稱
選項與參數:
刪除 runoob 目錄
[root@www tmp]# rmdir runoob/
將 mkdir 實例中建立的目錄(/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 <==由於尚有內容,因此沒法刪除! 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) 目標檔(destination) [root@www ~]# cp [options] source1 source2 source3 .... directory
選項與參數:
用 root 身份,將 root 目錄下的 .bashrc 複製到 /tmp 下,並命名爲 bashrc
[root@www ~]# cp ~/.bashrc /tmp/bashrc [root@www ~]# cp -i ~/.bashrc /tmp/bashrc cp: overwrite `/tmp/bashrc'? n <==n不覆蓋,y爲覆蓋
語法:
rm [-fir] 文件或目錄
選項與參數:
將剛剛在 cp 的實例中建立的 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
選項與參數:
複製一文件,建立一目錄,將文件移動到目錄中
[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系統中使用如下命令來查看文件的內容:
你可使用 man [命令]來查看各個命令的使用文檔,如 :man cp。
由第一行開始顯示文件內容
語法:
cat [-AbEnTv]
選項與參數:
檢看 /etc/issue 這個文件的內容:
[root@www ~]# cat /etc/issue CentOS release 6.4 (Final) Kernel \r on an \m
tac與cat命令恰好相反,文件內容從最後一行開始顯示,能夠看出 tac 是 cat 的倒着寫!如:
[root@www ~]# tac /etc/issue Kernel \r on an \m CentOS release 6.4 (Final)
顯示行號
語法:
nl [-bnw] 文件
選項與參數:
實例一:用 nl 列出 /etc/issue 的內容
[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%) <== 重點在這一行喔!你的光標也會在這裏等待你的命令
在 more 這個程序的運行過程當中,你有幾個按鍵能夠按的:
一頁一頁翻動,如下實例輸出/etc/man.config文件的內容:
[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] 文件
選項與參數:
[root@www ~]# head /etc/man.config
默認的狀況中,顯示前面 10 行!若要顯示前 20 行,就得要這樣:
[root@www ~]# head -n 20 /etc/man.config
取出文件後面幾行
語法:
tail [-n number] 文件
選項與參數:
[root@www ~]# tail /etc/man.config # 默認的狀況中,顯示最後的十行!若要顯示最後的 20 行,就得要這樣: [root@www ~]# tail -n 20 /etc/man.config