bash shell 文件與目錄

目錄

目錄列表

文件屬性

過濾輸出列表

處理文件

創建文件

複製文件

重命名文件

 刪除文件

  

 連接文件

處理目錄

創建目錄

刪除目錄

查看文件內容

文件類型

查看整個文件

查看部分文件

文件權限

改變權限

改變所屬關係


目錄列表

pwd:顯示目前的目錄;ls命令輸出的列表是按照字母排序的(按列排序而不是按行排序),選項參數之間可以組合如 ls -FR

 

-a 顯示全部的文件,連同隱藏的文件)一起列出來
-d 僅列出目錄本身,而不是列出目錄內的文件數據
-l 產生長列表格式的輸出,包含文件的屬性與權限等等數據
-F 彩色區分顯示文件和目錄。目錄後面加入(/),可執行文件加入*或@
-R 遞歸選項,列出當前目錄下的子目錄中的文件
  • 文件屬性

$ ls -l
-rw-rw-r--. 1 root user_1   57 Aug 20 17:29 hello.c
-rwxrwxr-x. 1 root user_1 8440 Aug 20 17:29 hello.exe
drwxrwxr-x. 3 root user_1   19 Aug 20 16:39 test1
  • 過濾輸出列表

ls命令支持在命令行中定義過濾器,進行簡單文本匹配的字符串。當用戶指定特定文件的名稱作爲過濾器時,ls命令只會顯示該文件的信息

$ ls -l my_script
-rwxrw-r-- 1 christine christine 54 May 21 11:26 my_script
$

ls命令能夠識別標準通配符,並在過濾器中用它們進行模式匹配:

  1. 問號(?)代表一個字符;可用於過濾器字符串中替代任意位置的單個字符。
  2. 星號(*)代表零個或多個字符。
$ ls -l my_scr?pt
-rw-rw-r-- 1 christine christine 0 May 21 13:25 my_scrapt
-rwxrw-r-- 1 christine christine 54 May 21 11:26 my_script
$ ls -l my*
-rw-rw-r-- 1 christine christine 0 May 21 13:25 my_file
-rw-rw-r-- 1 christine christine 0 May 21 13:25 my_scrapt
-rwxrw-r-- 1 christine christine 54 May 21 11:26 my_script
$

處理文件

  • 創建文件

touch命令創建了指定的新文件,並將用戶名作爲文件的屬主。文件的大小是零,因爲touch命令只創建了一個空文件。

$ touch test_one
$ ls -l test_one
-rw-rw-r-- 1 christine christine 0 May 21 14:17 test_one
$

touch命令還可用來改變文件的修改時間。這個操作並不需要改變文件的內容。

$ ls -l test_one
-rw-rw-r-- 1 christine christine 0 May 21 14:17 test_one
$ touch test_one
$ ls -l test_one
-rw-rw-r-- 1 christine christine 0 May 21 14:35 test_one
$

如果只想改變訪問時間,可用-a參數。使用ls –l命令默認顯示的是修改時間。要想查看文件的訪問時間,需要加入另外一個參數:--time=atime。

$ ls -l test_one
-rw-rw-r-- 1 christine christine 0 May 21 14:35 test_one
$ touch -a test_one
$ ls -l test_one
-rw-rw-r-- 1 christine christine 0 May 21 14:35 test_one
$ ls -l --time=atime test_one
-rw-rw-r-- 1 christine christine 0 May 21 14:55 test_one
$
  • 複製文件

 cp命令需要兩個參數——源對象和目標對象: cp   source destination 。

  • -p:連同文件的屬性一起復制過去,而非使用默認屬性;
  • -i:若目標檔(destination)已經存在時,在覆蓋時會先詢問動作的進行
  • -r:遞歸持續複製,用於目錄的複製行爲;

當兩個參數都是文件名,cp命令將源文件複製成一個新文件,並且以destination命名。新文件就像全新的文件一樣,有新的修改時間。

$ cp test_one test_two
$ ls -l test_*
-rw-rw-r-- 1 christine christine 0 May 21 14:35 test_one
-rw-rw-r-- 1 christine christine 0 May 21 15:15 test_two
$

當兩個參數有一個是目錄,可以將文件複製到目錄中。絕對路徑和相對路徑都可以,另外單點符(.)表示當前工作目錄,能夠簡化該任務。

$ cp -i test_one /home/christine/Documents/
$ cp -i test_one Documents/
$ cp -i /etc/NetworkManager/NetworkManager.conf .
$ ls -l NetworkManager.conf
-rw-r--r-- 1 christine christine 76 May 21 15:55 NetworkManager.conf
$

cp命令的-R參數威力強大。可以用它在一條命令中遞歸地複製整個目錄的內容,變爲完整副本

$ ls -Fd *Scripts
Scripts/
$ ls -l Scripts/
total 25
-rwxrw-r-- 1 christine christine 929 Apr 2 08:23 file_mod.sh
-rwxrw-r-- 1 christine christine 254 Jan 2 14:18 SGID_search.sh
-rwxrw-r-- 1 christine christine 243 Jan 2 13:42 SUID_search.sh
$ cp -R Scripts/ Mod_Scripts
$ ls -Fd *Scripts
Mod_Scripts/ Scripts/
$ ls -l Mod_Scripts
total 25
-rwxrw-r-- 1 christine christine 929 May 21 16:16 file_mod.sh
-rwxrw-r-- 1 christine christine 254 May 21 16:16 SGID_search.sh
-rwxrw-r-- 1 christine christine 243 May 21 16:16 SUID_search.sh
$

也可以在cp命令中使用通配符,該命令將所有以script結尾的文件複製到Mod_Scripts目錄中。在這裏,只需要複製一個文件:my_script

$ cp *script Mod_Scripts/
$ ls -l Mod_Scripts
total 26
-rwxrw-r-- 1 christine christine 929 May 21 16:16 file_mod.sh
-rwxrw-r-- 1 christine christine 54 May 21 16:27 my_script
-rwxrw-r-- 1 christine christine 254 May 21 16:16 SGID_search.sh
-rwxrw-r-- 1 christine christine 243 May 21 16:16 SUID_search.sh
$

製表鍵自動補全 

製表鍵自動補全允許你在輸入文件名或目錄名時按一下製表鍵,讓shell幫忙將內容補充完整。我們輸入了命令cp really,然後按製表鍵,shell就將剩下的文件名自動補充完整了

$ ls really*
really_ridiculously_long_file_name
$ cp really_ridiculously_long_file_name Mod_Scripts/
  • 重命名文件

 重命名文件稱爲移動。mv命令可以將文件和目錄移動到另一個位置或重新命名。

  • -f :force 強制的意思,如果目標文件已經存在,不會詢問而直接覆蓋;
  • -i :若目標文件 (destination) 已經存在時,就會詢問是否覆蓋!

移動文件會將文件名從fall更改爲fzll,但inode編號和時間戳保持不變。這是因爲mv隻影響文件名。

$ ls -li f?ll
296730 -rw-rw-r-- 1 christine christine 0 May 21 13:44 fall
$ mv fall fzll
$ ls -li f?ll
296730 -rw-rw-r-- 1 christine christine 0 May 21 13:44 fzll
$

 mv來移動文件的位置,這個操作並沒有改變文件的inode編號或時間戳,

$ ls -li /home/christine/fzll
296730 -rw-rw-r-- 1 christine christine 0 May 21 13:44 /home/christine/fzll
$ ls -li /home/christine/Pictures/
total 0
$ mv fzll Pictures/
$ ls -li /home/christine/Pictures/
total 0
296730 -rw-rw-r-- 1 christine christine 0 May 21 13:44 fzll
$ ls -li /home/christine/fzll
ls: cannot access /home/christine/fzll: No such file or directory
$

mv命令移動文件位置並修改文件名稱,

$ ls -li Pictures/fzll
296730 -rw-rw-r-- 1 christine christine 0 May 21 13:44 Pictures/fzll
$ mv /home/christine/Pictures/fzll /home/christine/fall
$ ls -li /home/christine/fall
296730 -rw-rw-r-- 1 christine christine 0 May 21 13:44 /home/christine/fall
$ ls -li /home/christine/Pictures/fzll
ls: cannot access /home/christine/Pictures/fzll: No such file or directory
  •  刪除文件

bash shell中刪除文件的命令是rm

  • -f :就是 force 的意思,忽略不存在的文件,不會出現警告信息;
  • -i :互動模式,在刪除前會詢問使用者是否動作
  • -r :遞歸刪除啊!最常用在目錄的刪除了!

也可以使用通配符刪除成組的文件。別忘了使用-i選項保護好自己的文件。

  

$ rm -i f?ll
rm: remove regular empty file 'fell'? y
rm: remove regular empty file 'fill'? y
rm: remove regular empty file 'full'? y
$
$ ls -l f?ll
ls: cannot access f?ll: No such file or directory
$
  •  連接文件

 鏈接文件是Linux文件系統的一個優勢。如需要在系統上維護同一文件的兩份或多份副本,除了保存多份單獨的物理文件副本之外,還可以採用保存一份物理文件副本和多個虛擬副本的方法。這種虛擬的副本就稱爲鏈接。鏈接是目錄中指向文件真實位置的佔位符。在Linux中有兩種不同類型的文件鏈接:

  •    符號鏈接

 符號鏈接就是一個實實在在的文件,它指向存放在虛擬目錄結構中某個地方的另一個文件。這兩個通過符號鏈接在一起的文件,彼此的內容並不相同。使用ln命令以及-s選項來創建符號鏈接

$ ls -l data_file
-rw-rw-r-- 1 christine christine 1092 May 21 17:27 data_file
$
$ ln -s data_file sl_data_file
$
$ ls -l *data_file
-rw-rw-r-- 1 christine christine 1092 May 21 17:27 data_file
lrwxrwxrwx 1 christine christine 9 May 21 17:29 sl_data_file -> data_file
$

 注意符號鏈接的名字sl_data_file位於ln命令中的第二個參數位置上。顯示在長列表中符號文件名後的->符號表明該文件是鏈接到文件data_file上的一個符號鏈接

  •  硬鏈接

硬鏈接會創建獨立的虛擬文件,其中包含了原始文件的信息及位置。但是它們從根本上而言是同一個文件。引用硬鏈接文件等同於引用了源文件。用ln命令時不再需要加入額外的參數 

$ ls -l code_file
-rw-rw-r-- 1 christine christine 189 May 21 17:56 code_file
$
$ ln code_file hl_code_file
$
$ ls -li *code_file
296892 -rw-rw-r-- 2 christine christine 189 May 21 17:56
code_file
296892 -rw-rw-r-- 2 christine christine 189 May 21 17:56
hl_code_file
$

處理目錄

  • 創建目錄

mkdir命令創建目錄

  • -m :配置文件的權限喔!直接配置
  • -p :幫助你直接將所需要的目錄(包含上一級目錄)遞歸創建起來!可以根據需要創建缺失的父目錄

要想同時創建多個目錄和子目錄,需要加入-p參數:

$ mkdir -p New_Dir/Sub_Dir/Under_Dir
$
$ ls -R New_Dir
New_Dir:
Sub_Dir
New_Dir/Sub_Dir:
Under_Dir
New_Dir/Sub_Dir/Under_Dir:
$
  • 刪除目錄

刪除目錄的基本命令是rmdir。默認情況下,rmdir命令只刪除空目錄。因爲我們在New_Dir目錄下創建了一個文件my_file,所以rmdir命令拒絕刪除目錄。

$ touch New_Dir/my_file
$ ls -li New_Dir/
total 0
294561 -rw-rw-r-- 1 christine christine 0 May 22 09:52 my_file
$
$ rmdir New_Dir
rmdir: failed to remove 'New_Dir': Directory not empty
$

也可以在整個非空目錄上使用rm命令。使用-r選項使得命令可以向下進入目錄,刪除其中的文件,然後再刪除目錄本身。

$ ls -l My_Dir
total 0
-rw-rw-r-- 1 christine christine 0 May 22 10:02 another_file
$
$ rm -ri My_Dir
rm: descend into directory 'My_Dir'? y
rm: remove regular empty file 'My_Dir/another_file'? y
rm: remove directory 'My_Dir'? y
$
$ ls -l My_Dir
ls: cannot access My_Dir: No such file or directory
$

一口氣刪除目錄及其所有內容的終極大法就是使用帶有-r參數和-f參數的rm命令

$ rm -rf My_Dir

查看文件內容

  • 文件類型

file命令是一個隨手可得的便捷工具。它能夠探測文件的內部,並決定文件是什麼類型的:

$ file my_file
my_file: ASCII text
$ file New_Dir
New_Dir: directory
$
  • 查看整個文件

cat命令是顯示文本文件中所有數據的得力工具

  • -n :參數會給所有的行加上行號,nl命令有同樣效果
  • -b:如果只想給有文本的行加上行號
  • -T:如果不想讓製表符出現;用^I字符組合去替換文中的所有制表符
$ cat test1
hello
This is a test file.
That we'll use to test the cat command.
$ cat -n test1
1 hello
2
3 This is a test file.
4
5
6 That we'll use to test the cat command.
$ cat -b test1
1 hello
2 This is a test file.
3 That we'll use to test the cat command.
$ cat -T test1
hello
This is a test file.
That we'll use to^Itest the cat command.
$

more命令是分頁工具,會在顯示每頁數據之後停下來;less命令的操作和more命令基本一樣,一次顯示一屏的文件文本。除了支持和more命令相同的命令集,它還包括更多的選項,可以輸入man less瀏覽對應的手冊頁

  • 查看部分文件

cat或more在加載完整個文件之後才能看到。如果數據是在文件的末尾,怎麼查看。

tail命令會顯示文件最後幾行的內容默認情況下,它會顯示文件的末尾10行

  • -n:參數來修改所顯示的行數
  • -f:允許你在其他進程使用該文件時查看文件的內容;tail命令會保持活動狀態,並不斷顯示添加到文件中的內容。用於實時監測系統日誌
$ tail log_file
$ tail -n 2 log_file

head命令會顯示文件開頭那些行的內容。默認情況下,它會顯示文件前10行的文本

  • -n:參數來修改所顯示的行數,允許你在破折號後面輸入想要顯示的行數
$ head -5 log_file

文件權限

$ ls –l
total 68
-rw-rw-r-- 1 rich rich 50 2010-09-13 07:49 file1.gz
-rw-rw-r-- 1 rich rich 23 2010-09-13 07:50 file2
-rw-rw-r-- 1 rich rich 48 2010-09-13 07:56 file3
-rw-rw-r-- 1 rich rich 34 2010-09-13 08:59 file4
  1. [ d ]是目錄、[ - ]是文件、[c]字符型設備、[l]連接、[n]網絡設備、[b]塊設備;

  2. 3組三字符編碼,每一組定義3種訪問權限:[r]對象可讀、[w]對象可寫、[x]對象可執行、[-]沒有權限。

  3. 3組權限分別對應對象的3個安全級別:[對象的屬主、對象的屬組、系統其他用戶]

Linux文件權限碼
權限 二進制值 八進制值
--- 000 0
--x 001

1

-w- 010 2
-wx 011 3
r-- 100 4
r-x 101 5
rw- 110 6
rwx 111 7
  • 改變權限

chmod命令用來改變文件和目錄的安全性設置 

chmod options mode file

options: -R : 進行遞歸(recursive)的持續變更,亦即連同次目錄下的所有文件都會變更

mode:可以採用八進制模式或者符號模式進行設置

  • 八進制:直接用期望賦予文件的標準3爲八進制權限碼就可以
$ chmod 777 file.txt
  • 符號模式的mode爲:[ugoa][+-=][rwxXstugo]

 

權限作用對象
u 代表用戶
g 代表組
o 代表其他用戶
a 代表上述所有用戶
+ 增加權限
- 移除權限
= 將權限設置成後面的值
rwx 讀-寫-執行
X 如果對象是目錄或它已經有執行權限,則賦予執行權限
u 將權限設置和屬主一樣
g 將權限設置和屬組一樣
o 將權限設置和其他用戶一樣
$ chmod o+r file
$ chmod u-x file

 

  • 改變所屬關係

chown命令用來改變文件的屬主,chgrp命令用來改變文件的默認屬組。

chown options owner[.group] file
$ chown dan newfile
$ ls -l newfile
-rw-rw-r-- 1 dan rich 0 Sep 20 19:16 newfile
$ chgrp shared newfile
$ ls -l newfile
-rw-rw-r-- 1 rich shared 0 Sep 20 19:16 newfile

chown命令也支持同時改變文件的屬主和屬組

$ chown dan.shared newfile
$ ls -l newfile
-rw-rw-r-- 1 dan shared 0 Sep 20 19:16 newfile

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章