寫在前面:
在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 也會變為一個無效的目錄連結。