Linux-Shell之檔案操作
學些了Linux Shell命令列使用,對於檔案的操作除了記下來,但還沒有找到相應方法掌握,常常混亂。資料庫有“增刪改查”,因而同樣按理來分類檔案操作方法,較為清晰。同時作為檔案,有許可權、大小等檔案屬性,因而加入“看”來看檔案屬性。這樣就變為“增刪看改查”。
1. 檔案操作之“增”——建立檔案/目錄
- mkdir——建立目錄
mkdir dir1 dir2 dir3 # create multiple directories
mkdir -m 777 dir # new directory with specific permissions
mkdir -p pdir/cdir # where pdir may not exist
mkdir -v dir # echo message when new directory created
- touch——建立檔案
touch file1 file2 file3 # create multiple files with various type
- cp / mv —— 複製和移動檔案,相當於建立了新檔案。
cp dir1/file dir2/file # copy file
cp -r dir1/ dir2/ # copy directory
cp dir/*.txt dir2/*.txt # copy files with regex
mv dir/a.txt dir2/a.txt # move file, change its path
mv -f a.txt b.txt # move and rename file, forcible
mv dir/*.txt dir2/*.txt # move files with regex
- 重定向—建立檔案
> file # redirect output to file
>> file # redirect and append output to file
- vi ———建立檔案
vi file1 file2 file3 # vim-create multiple files :n(next file) :N(the last)
- gedit——建立檔案
gedit file # only create one file once in .txt format
- nano——建立檔案
nano file # only create one file once in .txt format
- ln——建立連結檔案
ln file filelink # hard link file
ln -s file filelink # soft link file
2. 檔案操作之“刪”——刪除檔案/目錄
- rm ———刪除檔案或目錄
rm file1 file2 dir1 # remove multiple files or directories
rm *.txt # remove files with Regex
rm -rf dir # remove directories and contents recursively and forcible
rm -riv dir # remove directory and contents, prompt and echo
3. 檔案操作之“看”——看檔案/目錄屬性
這裡主要介紹要“看”的檔案屬性包括——檔案型別、檔案許可權、檔案大小以及檔案系統型別。
- Linux檔案型別
在Linux某個檔案目錄下開啟命令列輸入ls -al之後便可以看到所有檔案,以及檔案的相關屬性,一般結果如下所示:
drwxr-xr-x 2 xxx xxx 4096 7月 21 21:47 .
drwxr-xr-x 33 xxx xxx 4096 7月 21 21:26 ..
-rw-r--r-- 1 xxx xxx 0 7月 21 21:47 file.txt
第一列中如[drwxr-xr-x]的第一個字元代表檔案型別。在Linux中檔案型別及其對應的字元有:
[- ] —— 普通檔案型別,包括純文字檔案(ASCII)、二進位制檔案(binary)、資料格式的檔案(data)和各種壓縮檔案等。
[d] ——目錄檔案型別(directory)。
[b] —— 塊裝置檔案型別(block),如硬碟等等。
[c] —— 字元裝置檔案型別(character),串列埠裝置,如滑鼠、鍵盤等。
[s] —— 套接字檔案型別(socket),這類檔案通常用在網路資料連線,常在 /var/run目錄中看到。
[p] —— 管道檔案型別(pipe)。
[l ] —— 連結檔案(link)。
因而上面三個檔案分別為兩個目錄檔案和一個普通檔案。
- Linux檔案許可權
第一列中[drwxr-xr-x]除第一個字元後面的表示檔案許可權,共三組,以[rwx]來表示——r表示讀許可權,w表示寫許可權,x表示可執行許可權。第一組表示檔案所有者的許可權[u],第二組表示與所有者同組使用者的許可權[g],第三組表示其他組使用者的許可權[o]。 Linux檔案系統型別
Linux常用的檔案系統包括ext4、BtrFS、XFS、Reiser4、JFS2等。檔案系統包含使用者資料和元資料,而元資料就是檔案的相關屬性。ls ——檢視檔案或目錄(list),幾乎是最常用的Linux命令。其有多種選項,也有多個別名alias。
ls -alF # list all files and directories, -a(all), -l(long format), -F(append indicator)
ls -l file # list properties of the file
ls -l dir # list content under the diretory
l # alias of ls -CF
ll # alias of ls -alF
la # alias of ls -a
- file ——用來檢視檔案型別。不像windows中一般需要檢視檔名字尾來確定檔案型別,在Linux中一般通過識別檔案頭來決定檔案的具體型別。
file a.txt # show the file name and its type
file -b a.txt # show briefly
file -c a.txt # show the process of check
file -l # show all file type
file -z a.zip # try to show content of compressed file
file -L linkfile # show the file type of linked file
file *.txt # show all file type with regex
- stat —— 檢視檔案或者檔案系統狀態。其比ls更加具體。
stat fileordir # show the status of file or directory
stat -f fileordir # show the file system status
stat -t fileordir # show briefly
stat -c %A fileordir # show specific property of file
# %b number of blocks allocated
# %F file type
# %w time of file birth
# %x time of last access
# %y time of last data modification
stat *.txt # show all file status with regex
- df —— 檢視檔案所在檔案系統磁碟使用狀況。
df # show all file system disk usage status
df -hBM fileordir # show disk status in MB format
df -T fileordir # show disk usage status and file system type
- du —— 顯示目錄或者檔案的大小。
du fileordir # show file or directory size
du -hBM fileordir # show size in MB and human-readable
du -a dir # show all content size under directory
du -s dir # only show total size of the dir
du -hBk *.txt # show all file size matching regex
3. 檔案操作之“改”——更改檔案/目錄屬性
當我們可以檢視檔案的屬性時,就可能要修改相應的檔案屬性。下面介紹一些常用的檔案屬性修改命令。
- rename —— 重新命名檔名。
rename -v file1 file2 # rename file1 to file2 and echo
rename "s/AA/aa/" * # rename with regex, AA to aa
rename "s//.txt//.php" * # rename *.txt to *.php
rename "s/$//.txt/" * # rename * to *.txt
rename "s//.txt//" * # rename *.txt to *
- chmod —— 更改檔案許可權。
chmod 777 fileordir # change rights of file or dir
chmod u+x fileordir # make owner have execute right
chmod 666 -R dir # change rights recursively
- touch —— 更新檔案時間。
touch fileordir # change access and modify time
touch -d "12:03:45" file # use specific time to update
touch -c file # if file doesnot exist, do nothing
touch -r file1 file2 # apply time of file1 to file2
4. 檔案操作之“查”——查詢檔案/目錄等
系統中有大量檔案,當沒有圖形化的檔案管理器,我們想要找到某個檔案是該怎麼辦呢?
- find —— 查詢檔案和目錄。find是一個功能強大而豐富的查詢檔案工具,可以按目錄、檔案屬性、正則進行檔案搜尋,並接著執行相應操作。
find dir # list all files under directory
find dir -name filename # find file. or -iname
find . -name "*.txt" -o -name "*.pdf"
# find txt and pdf file
find ./ -regex .*s.*\.gz # regex
find . -type [bcdpflsD] # find different type files
find . -type f -ctime -7 # find files modified within 7 days
find . -type f -size +10M # find files size bigger than 10M
find . -type f -name "*.txt" -delete
# find and delete
find .-type f -user root -exec chown tom {} \;
# -exec -ok, find and execute command
find . -empty # find all empty files
- locate —— 通過檔案資料庫/var/lib/locatedb來查詢檔案,這個資料庫中含有本地所有檔案資訊,其搜尋速度更快,但是由於資料庫是系統自動一天更新一次,不能及時更新,新增新檔案要及時生效可以使用updatedb或者locate -u來實現手動更新。
locate /etc/sh # find files begin with sh under etc/
locate -i ./file # ignore case
locate -c ./*.txt # show number of files found
locate -e ./*.txt # find files only exists, not be del
locate -n 3 ./*.txt # only find 3 files matching regex
- tree —— 在命令列中把當前目錄包括其內所有檔案用目錄書的方式顯示出來。在命令列中查詢時,也算是找檔案比較方便的一種。
tree -CF # with colour and type
tree -L 2 # only two layer
tree -P *.txt # list all *.txt files
總結
本文主要總結了Linux中對檔案進行操作的相關命令,對提升效率大有裨益,下次再面對檔案的操作不至於束手無策。討論完檔案操作,關於對於檔案的內容的操作請見下文。