Centos常用命令之:ls和cd


在使用centos這個linux系統的時候,我們總是免不了需要查看當前目錄中的內容,需要切換到別的目錄,新建刪除等等一系列在window中非常普通的操作。

那在linux中這些操作是什么樣的呢。

 

在linux中,所有的目錄都是以'/'為根目錄的樹形結構。

我們在使用路徑的時候分為絕對路徑和相對路徑。

絕對路徑:以‘/’開始的路徑

相對路徑:以‘.’或'..'開始的路徑

 

ls:列出目錄中的內容

[fuwh@localhost ~]$ ls
Desktop  Documents  Downloads  Music  Pictures  Public  stu  Templates  Videos  服務
[fuwh@localhost ~]$

首先解釋下第一行:fuwh@localhost ~:fuwh就是當前登陸的用戶名,localhost就是代表登陸的主機,由於我登錄的是本地的所以就是localhost

最后面的~代表的是當前的所在目錄,如果我切換到別的目錄的話呢?

[fuwh@localhost stu]$ pwd
/home/fuwh/stu
[fuwh@localhost stu]$

pwd命令就是查看自己當前所在的目錄。可以看到,我切換到/home/fuwh目錄里面的stu文件夾里面去的時候,~就變成了stu,就代表當前所在目錄。

那么顯示~的時候,我們用pwd命令查看

[fuwh@localhost ~]$ pwd
/home/fuwh
[fuwh@localhost ~]$

這個時候是在/home/fuwh,這就是代表當前登陸的用戶fuwh的家目錄。這個時候就顯示的是~。

 

繼續回到ls這個命令。

我們輸入ls,回車后,列出了當前文件夾下的內容。但是只有名字信息。如果我們需要查看更詳細的信息,可以通過 man ls這個命令來查看。

man,就是manual的意思。看以查看任意命令的具體解釋。

那我們就來看看ls的具體使用方式:

格式: ls [選項]  [文件或目錄]

選項:

  -l  顯示詳細信息

  -a 顯示所有文件,包括隱藏文件

  -i  顯示inode

  -t :依時間排序,而不是用檔名。

  -r :將排序結果反向輸出,例如:原本檔名由小到大,反向則為由大到小;

文件或目錄

  這個可寫可不寫,寫的時候就是列出指定目錄的內容,不寫的話就是當前目錄。

  我們先看看ls -l

[fuwh@localhost ~]$ ls -l
總用量 40
drwxr-xr-x. 2 fuwh fuwh 4096 7月  13 22:45 Desktop
drwxr-xr-x. 2 fuwh fuwh 4096 7月  13 22:45 Documents
drwxr-xr-x. 2 fuwh fuwh 4096 7月  13 22:45 Downloads
drwxr-xr-x. 2 fuwh fuwh 4096 7月  13 22:45 Music
drwxr-xr-x. 2 fuwh fuwh 4096 7月  13 22:45 Pictures
drwxr-xr-x. 2 fuwh fuwh 4096 7月  13 22:45 Public
drwxrwxr-x. 2 fuwh fuwh 4096 7月  15 14:41 stu
drwxr-xr-x. 2 fuwh fuwh 4096 7月  13 22:45 Templates
drwxr-xr-x. 2 fuwh fuwh 4096 7月  13 22:45 Videos
drwxr-xr-x. 2 root root 4096 7月  21 21:57 服務
[fuwh@localhost ~]$

在很多發行版的linux中,可以使用ll 來代替ls -l這個命令。

我們來看看結果中的第一行,Desktop這個文件夾的詳細信息。

drwxr-xr-x. 2 fuwh fuwh 4096 7月  13 22:45 Desktop

d:第一個字符d代表這是一個目錄。'-':則代表這是一個文件。‘l’:則代表這是一個超鏈接。

 

然后是后面的9位:rwxr-xr-x

這9位代表的是對該目錄的操作權限信息,需要分為xxx  xxx  xxx的三個三位的來看。

第一個三位代表文件擁有者的權限。

第二個三位代表同群組的權限。

第三個三位代表其他人的操作權限。

其中,每一個三位又代表什么呢?

第一位代表讀權限,如果是‘r’則表示可讀。如果是'-'則表示不可讀。

第二位代表寫權限,如果是‘w’則表示可寫。如果是'-'則表示不可寫。

第三位代表執行權限,如果是‘x’則表示可執行。如果是'-'則表示不可執行。

后面的兩個fuwh  fuwh代表什么呢?

第一個代表的是文件的擁有者。后面的代表的是文件所屬的群組。

再后面的4096這個數字代表的是文件的大小,默認單位是byte。

再往后的時間代表的是改文檔最后被修改的時間。

最后面的就是改文檔的名字了。

 

我們再在命令中加入-a選項來看看。

[fuwh@localhost ~]$ ll -a
總用量 160
drwxrwxrwx. 27 fuwh fuwh 4096 7月  21 21:57 .
drwxrwxr-x.  5 root root 4096 7月  23 14:30 ..
-rw-------.  1 fuwh fuwh 1357 7月  23 15:51 .bash_history
-rw-r--r--.  1 fuwh fuwh   18 3月  23 09:11 .bash_logout
-rw-r--r--.  1 fuwh fuwh  176 3月  23 09:11 .bash_profile
-rw-r--r--.  1 fuwh fuwh  124 3月  23 09:11 .bashrc
drwxr-xr-x.  3 fuwh fuwh 4096 7月  14 22:37 .cache
drwxr-xr-x.  5 fuwh fuwh 4096 7月  14 22:12 .config
drwx------.  3 fuwh fuwh 4096 7月  13 22:45 .dbus
drwxr-xr-x.  2 fuwh fuwh 4096 7月  13 22:45 Desktop
-rw-r--r--.  1 fuwh fuwh   32 7月  14 21:49 .dmrc
drwxr-xr-x.  2 fuwh fuwh 4096 7月  13 22:45 Documents
drwxr-xr-x.  2 fuwh fuwh 4096 7月  13 22:45 Downloads
-rw-------.  1 fuwh fuwh   16 7月  13 22:45 .esd_auth
drwx------.  4 fuwh fuwh 4096 7月  14 21:49 .gconf
drwxr-xr-x.  6 fuwh fuwh 4096 7月  14 22:37 .gnome2
drwx------.  2 fuwh fuwh 4096 7月  14 22:37 .gnome2_private
drwxrwxr-x.  3 fuwh fuwh 4096 7月  13 22:46 .gnote
drwx------.  2 fuwh fuwh 4096 7月  14 21:49 .gnupg
-rw-rw-r--.  1 fuwh fuwh  132 7月  14 21:49 .gtk-bookmarks
drwx------.  2 fuwh fuwh 4096 7月  13 22:45 .gvfs
-rw-------.  1 fuwh fuwh  620 7月  14 21:49 .ICEauthority
-rw-r--r--.  1 fuwh fuwh  633 7月  14 21:49 .imsettings.log
drwx------.  3 fuwh fuwh 4096 7月  14 22:28 .kde
drwxr-xr-x.  3 fuwh fuwh 4096 7月  13 22:45 .local
drwxr-xr-x.  5 fuwh fuwh 4096 7月  14 22:37 .mozilla
drwxr-xr-x.  2 fuwh fuwh 4096 7月  13 22:45 Music
drwxr-xr-x.  2 fuwh fuwh 4096 7月  13 22:45 .nautilus
drwxr-xr-x.  2 fuwh fuwh 4096 7月  13 22:45 Pictures
drwxr-xr-x.  2 fuwh fuwh 4096 7月  13 22:45 Public
drwx------.  2 fuwh fuwh 4096 7月  13 22:45 .pulse
-rw-------.  1 fuwh fuwh  256 7月  13 22:45 .pulse-cookie
drwx------.  2 fuwh fuwh 4096 7月  13 22:45 .ssh
drwxrwxr-x.  2 fuwh fuwh 4096 7月  15 14:41 stu
drwxr-xr-x.  2 fuwh fuwh 4096 7月  13 22:45 Templates
drwxr-xr-x.  2 fuwh fuwh 4096 7月  13 22:45 Videos
-rw-------.  1 fuwh fuwh 5663 7月  14 22:42 .xsession-errors
-rw-------.  1 fuwh fuwh 2638 7月  13 22:46 .xsession-errors.old
drwxr-xr-x.  2 root root 4096 7月  21 21:57 服務
[fuwh@localhost ~]$

可以發現,這時候列出了很多以'.'開頭的文件,這些就是隱藏文件了。

其中有兩個‘.’和‘..’兩個文件。第一個代表當前目錄,第二個代表上一個目錄。

-h:這個選項用來顯示文件大小的時候以比較容易看的方式顯示 ,比如多少兆 

 

cd:切換目錄。

當我們需要進入到別的目錄的時候,就需要使用到cd這個命令。 ‘

cd這個命令主要有以下幾種使用方式;

cd [~]:進入當前用戶的家目錄,比如我是fuwh這個用戶登陸的,則進入到/home/fuwh目錄

cd 目錄名:進入到指定的目錄

cd .:就代表當前目錄

cd ..:代表進入上一層目錄,比如我現在在/home/fuwh下的時候,使用該命令,則會進入到/home目錄下。

cd -:代表前一個工作目錄,比如我們經常需要在兩個目錄之間切換,則可以使用這種快捷方式。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM