
寫在前面:
在window的世界里,大家一定很熟悉對檔案和目錄的各種添加、洗掉、更新等操作,同樣,linux 的世界里也少不了這些最基本的技能,這就需要大家掌握一些操作命令,本篇著重于綜合整理作業中對檔案和目錄操作常用的一些命令,簡化大家的學習成本,
ls (列出目錄)
ls(英文全拼:list files): 列出目錄及檔案名
這個可以算是linux 的最常用的了,查看一個目錄下有哪些檔案和子目錄都用這個命令,
常用的引數:
- -a :全部的檔案,連同隱藏檔案( 開頭為 . 的檔案) 一起列出來(常用)
- -l :長資料串列出,包含檔案的屬性與權限等等資料;(常用)
引數保姆級講解:
-a引數,在linux系統中,用 ‘.’ 開頭的檔案和目錄,都屬于隱藏檔案,單用ls命令是不會顯示在終端的,此時配合-a引數就有了很好的作用,
演示截圖:

代碼示例:
[root@10-21-141-81-jhdxyjd test]# touch test1 #創建一個常規檔案
[root@10-21-141-81-jhdxyjd test]# touch .test2 #創建一個隱藏檔案,以 '.'開頭
[root@10-21-141-81-jhdxyjd test]# mkdir test_dir #創建一個常規目錄
[root@10-21-141-81-jhdxyjd test]# mkdir .test_dir1 #創建一個隱藏目錄,以 '.'開頭
[root@10-21-141-81-jhdxyjd test]# ls #ls只顯示常規檔案目錄,隱藏檔案不顯示
test1 test_dir
[root@10-21-141-81-jhdxyjd test]# ls -a #加上-a引數可以全部顯示
. .. test1 .test2 test_dir .test_dir1
[root@10-21-141-81-jhdxyjd test]#
-l引數:這個引數可以說是非常重要和常用的,基本上每一次ls命令的出現,都要配合-l引數一塊操作,但是一致加上這個引數又會顯得有些麻煩,所以linux 提供了一種別名(alias)【下文會有講到】的機制,這樣就把 ls -l 設定成一個命令別名叫做 ll ,這樣每次操作ll 就等同于 ls-l了,
演示截圖:

代碼示例:
[root@10-21-141-81-jhdxyjd test]# ls
test1 test_dir
[root@10-21-141-81-jhdxyjd test]# ls -l #把檔案的屬性都詳細列出來了
total 0
-rw-r--r-- 1 root root 0 Sep 19 09:00 test1
drwxr-xr-x 2 root root 6 Sep 19 09:01 test_dir
[root@10-21-141-81-jhdxyjd test]# ll #等同于ls -l
total 0
-rw-r--r-- 1 root root 0 Sep 19 09:00 test1
drwxr-xr-x 2 root root 6 Sep 19 09:01 test_dir
檔案屬性詳解

第1列是檔案的權限和型別,本例中d打頭的表示目錄,沒有d的表示是檔案
第2串列示檔案的硬鏈接數
第3串列示檔案的所屬主
第4串列示檔案的所屬主
第5串列示檔案的檔案的大小,對于目錄而言:只是目錄本身的大小,而不是里面內容的大小
第6串列示檔案的修改時間
第7列就是檔案表示檔案或者目錄名稱
ls 的模糊匹配查詢:這個在作業中應該是及其常用的,有時候一個目錄下很多的檔案,我們只想要列出指定的部分檔案,或者有時候我們需要查看目錄下子目錄的檔案,也需要使用到,
a.列出指定目錄下,指定的型別的所有檔案,
截圖示例:

代碼示例:
[root@10-21-141-81-jhdxyjd test]# ll ##列出所有的檔案,并顯示屬性
total 0
-rw-r--r-- 1 root root 0 Sep 19 09:35 1.sh
-rw-r--r-- 1 root root 0 Sep 19 09:35 1.txt
-rw-r--r-- 1 root root 0 Sep 19 09:35 2.sh
-rw-r--r-- 1 root root 0 Sep 19 09:35 2.txt
-rw-r--r-- 1 root root 0 Sep 19 09:35 3.sh
-rw-r--r-- 1 root root 0 Sep 19 09:35 3.txt
-rw-r--r-- 1 root root 0 Sep 19 09:00 test1
drwxr-xr-x 2 root root 6 Sep 19 09:01 test_dir
[root@10-21-141-81-jhdxyjd test]# ll 3.* ##列出以3開頭的所有檔案
-rw-r--r-- 1 root root 0 Sep 19 09:35 3.sh
-rw-r--r-- 1 root root 0 Sep 19 09:35 3.txt
[root@10-21-141-81-jhdxyjd test]# ll *.sh ##列出以.sh 結尾的所有檔案
-rw-r--r-- 1 root root 0 Sep 19 09:35 1.sh
-rw-r--r-- 1 root root 0 Sep 19 09:35 2.sh
-rw-r--r-- 1 root root 0 Sep 19 09:35 3.sh
b.列出當前目錄下所有子目錄內的檔案
操作截圖:

代碼示例:
[root@10-21-141-81-jhdxyjd test]# ll
total 0
-rw-r--r-- 1 root root 0 Sep 19 09:35 1.sh
-rw-r--r-- 1 root root 0 Sep 19 09:35 1.txt
-rw-r--r-- 1 root root 0 Sep 19 09:35 2.sh
-rw-r--r-- 1 root root 0 Sep 19 09:35 2.txt
-rw-r--r-- 1 root root 0 Sep 19 09:35 3.sh
-rw-r--r-- 1 root root 0 Sep 19 09:35 3.txt
-rw-r--r-- 1 root root 0 Sep 19 09:00 test1
drwxr-xr-x 2 root root 39 Sep 19 09:41 test_dir
drwxr-xr-x 2 root root 45 Sep 19 09:41 test_dir2
[root@10-21-141-81-jhdxyjd test]# ll */* ##列出當前目錄下的所有子目錄下的所有檔案
-rw-r--r-- 1 root root 0 Sep 19 09:41 test_dir/1.sh
-rw-r--r-- 1 root root 0 Sep 19 09:41 test_dir2/test
-rw-r--r-- 1 root root 0 Sep 19 09:41 test_dir2/test2
-rw-r--r-- 1 root root 0 Sep 19 09:41 test_dir2/test2.txt
-rw-r--r-- 1 root root 0 Sep 19 09:41 test_dir/3.txt
-rw-r--r-- 1 root root 0 Sep 19 09:41 test_dir/4.h
cd (切換目錄)
- cd(英文全拼:change directory):切換目錄
我們看英文的全程就可以了解這個命令的意思了,就是切換不同的目錄時要使用的,非常常用的命令,
常用引數:
-P 如果要切換到的目標目錄是一個符號連接,那么切換到它指向的物理位置目錄,
- 當前作業目錄將被切換到環境變數OLDPWD所表示的目錄,也就是前一個作業目錄,
.. 切換到上級父目錄
引數保姆級講解:
-P :注意,這個P是大寫的,小寫的是不生效的,系統也會有報錯提示,這個引數的意義就在于我們在把一個路徑做了符號鏈接【下文會有講解】之后,普通的cd只會進入這個目錄下,加上-P引數之后,我們就可以進入到這個目錄鏈接到的目錄,也是這個目錄資料真實的存盤目錄,
演示截圖:

代碼演示:
[root@10-21-141-81-jhdxyjd test_dir]# ll
total 0
-rw-r--r-- 1 root root 0 Sep 19 09:41 1.sh
-rw-r--r-- 1 root root 0 Sep 19 09:41 3.txt
-rw-r--r-- 1 root root 0 Sep 19 09:41 4.h
lrwxrwxrwx 1 root root 15 Sep 19 10:05 test_dir -> /data/test_dir/ ##軟鏈接目錄
[root@10-21-141-81-jhdxyjd test_dir]# pwd
/root/test/test_dir ##當前的路徑名稱
[root@10-21-141-81-jhdxyjd test_dir]# cd test_dir/ ##直能進入當前的路徑
[root@10-21-141-81-jhdxyjd test_dir]# pwd
/root/test/test_dir/test_dir
[root@10-21-141-81-jhdxyjd test_dir]# cd ..
[root@10-21-141-81-jhdxyjd test_dir]# cd -P test_dir/ ##進入目錄指向的真實路徑
[root@10-21-141-81-jhdxyjd test_dir]# pwd
/data/test_dir
- 這個橫杠【-】就是一個引數,可以類比于回退的意思,就是你進入一個目錄之后,突然要回到你上一個切過來的目錄,但是上一個目錄太長,記不住,此時就可以用 cd - 操作,
演示截圖:

代碼演示:
[root@10-21-141-81-jhdxyjd test_dir]# pwd ##查看當前所在的目錄
/root/test/test_dir
[root@10-21-141-81-jhdxyjd test_dir]# cd /data/test_dir/ ##進入到一個新的目錄
[root@10-21-141-81-jhdxyjd test_dir]# pwd
/data/test_dir
[root@10-21-141-81-jhdxyjd test_dir]# cd - ##回到上一個切過來的目錄,終端會列印當前所在的目錄
/root/test/test_dir
[root@10-21-141-81-jhdxyjd test_dir]# pwd
/root/test/test_dir
[root@10-21-141-81-jhdxyjd test_dir]#
.. 兩個點,這個應該是非常常用的,表示的是你可以通過兩個點 表示你所在目錄的父目錄,cd .. 即表示切換到上一級的父目錄,在目錄的切換中,既方便,又省事,又快捷,
演示截圖:

代碼演示:
[root@10-21-141-81-jhdxyjd test_dir]# pwd ##查看當前目錄
/root/test/test_dir
[root@10-21-141-81-jhdxyjd test_dir]# cd .. ##切換到上一級目錄
[root@10-21-141-81-jhdxyjd test]# pwd ##查看已經切換到上一級的父目錄
/root/test
作業常用命令簡化總結:
- cd # 進入用戶主目錄;
- cd / # 進入根目錄
- cd ~ # 進入用戶主目錄;
- cd .. # 回傳上級目錄(若當前目錄為“/“,則執行完后還在“/";".."為上級目錄的意思);
- cd ../.. # 回傳上兩級目錄;
- cd !$ # 把上個命令的引數作為cd引數使用,
alias(設定別名)
Linux alias命令用來設定指令的別名,對一些較長的命令進行簡化,使用alias時,必須使用單引號將原來的命令包含,防止特殊字符導致錯誤,
語法格式:
alias 別名='原命令 -選項/引數'
a.設定別名:
以上文提到的ls -l 等于 ll 為例:
![]()
代碼示例:
[root@10-21-141-81-jhdxyjd test]# alias ll='ls -l'
這樣ls -l 就等同于ll,命令操作能達到同樣的效果,
查看別名:alias -p
在終端會列印出所有的已經設定的別名
演示截圖:

b.洗掉別名:
當然能設定別名,等你不想要了,或者想修改了,也可以洗掉名別,還是以ll 別名為例,
演示截圖:
![]()
代碼示例:
[root@10-21-141-81-jhdxyjd test]# unalias ll
此時在查看已經設定的別名,就沒有ll了,當然你在輸入ll,系統也會提示報錯,沒有這個命令的,

企業使用小技巧推薦:
有時候我們為了安全考慮,會將ssh 的埠又默認的改為我們指定的埠,這樣帶來的不便就是我們在登錄主機時就需要用-P引數指定埠(8888為例)登錄,這樣很不方便,此時就可以利用別名機制簡化 ssh -p 8888 為 ssh5,這樣就方便很多,
ln(鏈接檔案)
ln(英文全拼:link files)命令是一個非常重要命令,
它的功能是為某一個檔案在另外一個位置建立一個同步的鏈接,
當我們需要在不同的目錄,用到相同的檔案時,我們不需要在每一個需要的目錄下都放一個必須相同的檔案,我們只要在某個固定的目錄,放上該檔案,然后在 其它的目錄下用ln命令鏈接(link)它就可以,不必重復的占用磁盤空間,
基本語法:
ln [引數][源檔案或目錄][目標檔案或目錄]
深度講解 :
Linux檔案系統中,有鏈接(link)的概念,其實我們也可以理解為檔案或目錄的別名,鏈接分為兩種 : 硬鏈接(hard link)與軟鏈接(symbolic link)不同點:
硬鏈接表示一個檔案或者目錄可以有多個名稱,
軟鏈表示的是產生一個特殊的檔案或者目錄,該檔案或者目錄的內容是指向另一個檔案或者目錄的位置,
硬鏈接只能存在同一個檔案系統中,而軟鏈接卻可以跨越不同的檔案系統,相同點:
不論是硬鏈接或軟鏈接都不會將原本的檔案或者目錄復制一份,只會占用非常少量的存盤空間,
軟鏈接:
- 1.軟鏈接,以路徑的形式存在,類似于Windows作業系統中的快捷方式
- 2.軟鏈接可以 跨檔案系統 ,硬鏈接不可以
- 3.軟鏈接可以對一個不存在的檔案名進行鏈接
- 4.軟鏈接可以對目錄進行鏈接
硬鏈接:
- 1.硬鏈接,以檔案副本的形式存在,但不占用實際空間,
- 2.不允許給目錄創建硬鏈接
- 3.硬鏈接只有在同一個檔案系統中才能創建
命令引數
常用引數:
- -s 軟鏈接(符號鏈接)
演示截圖:

代碼示例:
[root@10-21-141-81-jhdxyjd test_dir]# ll
total 0
-rw-r--r-- 1 root root 0 Sep 19 09:41 1.sh
-rw-r--r-- 1 root root 0 Sep 19 09:41 3.txt
-rw-r--r-- 1 root root 0 Sep 19 09:41 4.h
[root@10-21-141-81-jhdxyjd test_dir]# ln -s /data/test_dir test_dir ##創建軟連接
[root@10-21-141-81-jhdxyjd test_dir]# ll
total 0
-rw-r--r-- 1 root root 0 Sep 19 09:41 1.sh
-rw-r--r-- 1 root root 0 Sep 19 09:41 3.txt
-rw-r--r-- 1 root root 0 Sep 19 09:41 4.h
lrwxrwxrwx 1 root root 14 Sep 19 11:34 test_dir -> /data/test_dir ##軟連接目錄生成
需要注意的是,如果不小心洗掉了 test_dir 資料不會丟失,在重新做一個上述的鏈接即可,但是如果洗掉了 /data/test_dir 那么資料就會丟失,test_dir 也會變為一個無效的目錄鏈接,

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/301630.html
標籤:其他
