1. 程式人生 > >2017-12-15 2周1次課

2017-12-15 2周1次課

2017-12-15 2周1次課

2.6 相對和絕對路徑


  • 絕對路徑: 路徑寫法一定是由跟目錄“/”寫起的。不管在那個路徑下都能通過絕對路徑找到文件從根開始例如

    #ls /etc/sysconfig/network-scripts/ifcfg-ens33


  • 相對路徑: 路徑的寫法不是由跟目錄“/”寫起的。相對當前的目錄。例如

    #ls network-scripts/ifcfg-ens33

  • pwd命令查看所在目錄的位置



2.7 cd命令


  • cd - 表示上次所在的目錄例如:

    [root@hanshuo-10 ~]# cd /etc/sysconfig $用進入sysconfig目錄

    [root@hanshuo-10 sysconfig]# cd - $在sysconfig目錄下使用cd -

    /root $第一次使用cd -後所在目錄

    [root@hanshuo-10 ~]# cd - $在/root裏再次使用cd -

    /etc/sysconfig $第二次使用cd -後所在的目錄

    [root@hanshuo-10 sysconfig]# pwd $ 查看所在的目錄

    /etc/sysconfig

  • cd 用來改變用戶所在目錄的,如果後面什麽都不跟,就直接進入當前用戶的根目錄下例如:

[root@hanshuo-10 ~]# cd /etc $用cd進入etc目錄

[root@hanshuo-10 etc]# cd ¥在etc目錄下直接輸入cd

[root@hanshuo-10 ~]# #進入跟目錄

  • cd .. 進入上一級目錄,例如:

    [root@hanshuo-10 ~]# cd /etc/sysconfig/network-scripts/ $ 用cd進入/etc/sysconfig/network-scripts/

[root@hanshuo-10 network-scripts]# cd .. $用cd ..進入上一級目錄

[root@hanshuo-10 sysconfig]# pwd $查看目錄所在

/etc/sysconfig $所在的目錄


2.8 創建和刪除目錄mkdir/rmdir


  • mkdir是make directory的縮寫,該命令用於創建目錄該目錄的格式為“mkdir [-p] [目錄名稱]”。其中-P可以連續創建目錄,例如:

[root@hanshuo-10 ~]# mkdir /tmp/hanshuo1 $ 創建目錄 /tmp/hanshuo1

[root@hanshuo-10 ~]# ls -ld /tmp/hanshuo1 $查看創建的/tmp/hanshuo1

drwxr-xr-x 2 root root 6 12月 18 22:09 /tmp/hanshuo1 $創建完的/tmp/hanshuo1

[root@hanshuo-10 ~]# date $查看創建時間

2017年 12月 18日 星期一 22:09:47 CST

[root@hanshuo-10 ~]# mkdir -p /tmp/hanshuo1/hanshuo2 $用-P連續創建目錄/tmp/hanshuo1/hanshuo2

[root@hanshuo-10 ~]# ls -ld /tmp/hanshuo1/hanshuo2 $ 查看連續創建目錄/tmp/hanshuo1/hanshuo2

drwxr-xr-x 2 root root 6 12月 18 22:10 /tmp/hanshuo1/hanshuo2 $連續創建完目錄/tmp/hanshuo1/hanshuo2


  • rmdir用於刪除空目錄,後面可以是一個目錄,也可以是多個目錄。該命令只能刪除目錄不能刪除文件。即使加上-P選項也只能刪除一串空目錄,




2.9 rm命令


  • rm命令常會用到,他是一個刪除目錄和文件的一個命令例如:

    [root@hanshuo-10 ~]# mkdir -p hanshuo/hanshuo1/hanshuo2 $ 創建目錄mkdir -p hanshuo/hanshuo1/hanshuo2

[root@hanshuo-10 ~]# touch hanshuo/hanshuo1/hanshuo2/1.txt $創建文件touch hanshuo/hanshuo1/hanshuo2/1.txt

[root@hanshuo-10 ~]# rm hanshuo/hanshuo1/hanshuo2/1.txt $刪除文件rm hanshuo/hanshuo1/hanshuo2/1.txt

rm:是否刪除普通空文件 "hanshuo/hanshuo1/hanshuo2/1.txt"?y $問是否刪除文件點y是刪除n不刪除

[root@hanshuo-10 ~]# ls -ld hanshuo/hanshuo1/hanshuo2/1.txt $ 查看是否刪除hanshuo/hanshuo1/hanshuo2/1.txt

ls: 無法訪問hanshuo/hanshuo1/hanshuo2/1.txt: 沒有那個文件或目錄 $ 已刪除


  • 上邊是詢問是否刪除如果加個-f就會強制刪除。不會詢問例如:

    [root@hanshuo-10 ~]# touch hanshuo/hanshuo1/hanshuo2/1.txt $創建 hanshuo/hanshuo1/hanshuo2/1.txt

    [root@hanshuo-10 ~]# rm -f hanshuo/hanshuo1/hanshuo2/1.txt $刪除hanshuo/hanshuo1/hanshuo2/1.txt

    root@hanshuo-10 ~]# ls -ld hanshuo/hansuo1/hanshuo2/1.txt $查看是否刪除hanshuo/hanshuo1/hanshuo2/1.txt

    ls: 無法訪問hanshuo/hansuo1/hanshuo2/1.txt: 沒有那個文件或目錄 $沒有了 已刪除


  • 上例中,是刪除文件的如果刪除目錄會報錯,即使加上-f也會報錯.使用命令rm刪除目錄時加個-r選項,就可以刪除,-rf可以刪除目錄和文件例如:

    [root@hanshuo-10 ~]# rm -f hanshuo $刪除hanshuo目錄
    rm: 無法刪除"hanshuo": 是一個目錄 $無法刪除目錄
    [root@hanshuo-10 ~]# rm -rf hanshuo $加個-rf 就可以刪除目錄和文件


2017-12-15 2周1次課