1. 程式人生 > >相對路徑和絕對路徑 目錄的創建 cd mkdir rm 等命令

相對路徑和絕對路徑 目錄的創建 cd mkdir rm 等命令

linux mkdir rmdir rm cd 等簡單命令的使用

1. 相對路徑和絕對路徑

若用比喻的話, 相對路徑以個人當前位置為中心,絕對路徑以地球為中心。

相對路徑,對於個人而言,是隨時都可能變化的。

而絕對路徑,就跟我們現實中的街道地址一樣,幾乎都沒有變化的。

linux中,相對路徑,都是針對於你現在,所工作的目錄而言的。

而絕對路徑,就是從根目錄開始的, 找任何路徑都從根目錄開始,就可以理解為絕對路徑,其它的,就可以理解為相對路徑。

2. 目錄的創建 :

mkdir 命令, 所帶的參數有 -p

創建一個目錄可以用:

mkdir dirname

創建多個層級目錄可以用

mkdir -p dirname/dirname1/dirname2

同時創建多個目錄用

mkdir dir dir1 dir2 或 mkdir {dir1,dir2,dir3}

如:

nfyx@nfyx:~/test$ ls
nfyx@nfyx:~/test$ mkdir 11
nfyx@nfyx:~/test$ ls
11
nfyx@nfyx:~/test$ mkdir 22/33
mkdir: 無法創建目錄"22/33": 沒有那個文件或目錄
nfyx@nfyx:~/test$ mkdir -p 22/33
nfyx@nfyx:~/test$ ls 22/33
nfyx@nfyx:~/test$ cd 22/33

nfyx@nfyx:~/test/22/33$ pwd
/home/nfyx/test/22/33
nfyx@nfyx:~/test/22/33$ cd ../../
nfyx@nfyx:~/test$ pwd
/home/nfyx/test
nfyx@nfyx:~/test$ mkdir 44 55
nfyx@nfyx:~/test$ ls
11 22 44 55
nfyx@nfyx:~/test$ mkdir {66,77}
nfyx@nfyx:~/test$ ls
11 22 44 55 66 77
nfyx@nfyx:~/test$


2 目錄刪除 rmdir (只能刪除空目錄)

rmdir dirname (它的用處不是很大)

如:

nfyx@nfyx:~/test$ ls
11 22 44 55 66 77
nfyx@nfyx:~/test$ rmdir 11
nfyx@nfyx:~/test$ ls
22 44 55 66 77
nfyx@nfyx:~/test$ touch ./22/aa
nfyx@nfyx:~/test$ rmdir 22
rmdir: 刪除 ‘22‘ 失敗: 目錄非空


3. rm命令(這是經常用的命令)

rm filename (若不帶參數的話,只能刪除一個文件)

rm -r dirname 刪一個目錄 帶r參數

rm -rf dirname 或 filename 強制刪除目錄或文件, 這命令也是最常用的。

如:

nfyx@nfyx:~/test$ ls
44 55 66 77
nfyx@nfyx:~/test$ touch aa
nfyx@nfyx:~/test$ ls
44 55 66 77 aa
nfyx@nfyx:~/test$ rm aa
nfyx@nfyx:~/test$ ls
44 55 66 77
nfyx@nfyx:~/test$ rm 44
rm: 無法刪除‘44‘: 是一個目錄
nfyx@nfyx:~/test$ rm -r 44
nfyx@nfyx:~/test$ ls
55 66 77
nfyx@nfyx:~/test$ touch ./55/aa
nfyx@nfyx:~/test$ rm -r 55
nfyx@nfyx:~/test$ ls
66 77
nfyx@nfyx:~/test$ vim aa
nfyx@nfyx:~/test$ ls
66 77 aa
nfyx@nfyx:~/test$ rm aa
nfyx@nfyx:~/test$ ls
66 77
nfyx@nfyx:~/test$


4. cd 命令(改變目錄或理解為,進入到指定目錄)


cd ./dirname (進入一個目錄,即進入相對目錄)

註釋: ./ 表示當前目錄下的***

.. 表示當前目錄的上一層目錄 (即 cd .. 就表示進入上一層目錄(父目錄))

cd /path/to/dirname (進入一個絕對路徑的目錄)

如:

nfyx@nfyx:~/test$ ls
66 77
nfyx@nfyx:~/test$ cd 66
nfyx@nfyx:~/test/66$ pwd
/home/nfyx/test/66
nfyx@nfyx:~/test/66$ cd /home/nfyx/test/77
nfyx@nfyx:~/test/77$ pwd
/home/nfyx/test/77
nfyx@nfyx:~/test/77$


2017.10.24

本文出自 “牛糞也香” 博客,請務必保留此出處http://ainfyx.blog.51cto.com/724466/1975532

相對路徑和絕對路徑 目錄的創建 cd mkdir rm 等命令