處理文件和文件夾操作相關(guān)命令
1.創(chuàng)建文件:touch
touch命令最常被使用在這個場景:
- 創(chuàng)建一個空的文件
- 修改某個文件的目前日期(mtime和atime)
在Linux系統(tǒng)中,一個文件有三種時間:
| 時間 | 含義 | 解釋 |
|---|---|---|
| modification time(mtime) | 文件的“內(nèi)容數(shù)據(jù)”被更改,該時間更新 | 修改文件的內(nèi)容 |
| status time(ctime) | 文件的“狀態(tài)”被更改,該時間更新 | 修改文件的屬性或權(quán)限 |
| access time(atime) | 文件的內(nèi)容被取用,該時間更新 | 例如使用cat讀取文件 |
PS:通過ls -l命令查看文件,顯示的是文件mtime。
通過touch命令創(chuàng)建文件:
LxiindeMacBook-Pro:Desktop Lxiin$ touch test.txt
LxiindeMacBook-Pro:Desktop Lxiin$ ls -l test.txt
-rw-r--r-- 1 kingsoft staff 0 6 16 11:35 test.txt
2.創(chuàng)建目錄:mkdir
mkdir的幾個參數(shù):
-
-m:配置文件夾的權(quán)限:-m [權(quán)限]
LxiindeMacBook-Pro:test Lxiin$ mkdir test1
LxiindeMacBook-Pro:test Lxiin$ mkdir -m 777 test2
LxiindeMacBook-Pro:test Lxiin$ ls -l
total 0
drwxr-xr-x 2 Lxiin staff 64 6 16 11:44 test1
drwxrwxrwx 2 Lxiin staff 64 6 16 11:44 test2
-
-p:遞歸創(chuàng)建文件夾,即可創(chuàng)建文件夾的上級目錄
LxiindeMacBook-Pro:test Lxiin$ mkdir test
LxiindeMacBook-Pro:test Lxiin$ mkdir test/test1/test2
mkdir: test/test1: No such file or directory
LxiindeMacBook-Pro:test Lxiin$ mkdir -p test/test1/test2
LxiindeMacBook-Pro:test Lxiin$ tree
.
└── test
└── test1
└── test2
3 directories, 0 files
3.刪除文件/文件夾:rm/rmdir
-
rm的幾個參數(shù):
①-i:交互式刪除,每一個刪除項(xiàng)都會進(jìn)行詢問
②-r:刪除目錄并遞歸地刪除目錄中的文件和目錄
③-f:強(qiáng)制刪除,忽略不存在文件,沒有任何提示
LxiindeMacBook-Pro:test1 Lxiin$ rm -i test1.txt
remove test1.txt? y
LxiindeMacBook-Pro:test1 Lxiin$ mkdir test2
LxiindeMacBook-Pro:test1 Lxiin$ rm -r test2/
LxiindeMacBook-Pro:test1 Lxiin$ touch test.txt
LxiindeMacBook-Pro:test1 Lxiin$ rm -f test.txt
-
rmdir只能刪除空目錄,所以刪除非空目錄需要使用rm -r [dir]
LxiindeMacBook-Pro:test1 Lxiin$ touch test2/test.txt
LxiindeMacBook-Pro:test1 Lxiin$ rmdir test2/
rmdir: test2/: Directory not empty
4.查看目錄文件的內(nèi)容:ls
-
-l:以長格式查看文件和目錄 -
-a:顯示以點(diǎn)開頭的文件和目錄,即隱藏目錄 -
-R:遇到目錄遞歸顯示 -
-d:只列出目錄 -
-S:按照大小進(jìn)行展示 -
-t:按照時間進(jìn)行展示
5.切換和查看當(dāng)前的工作目錄:cd/pwd
LxiindeMacBook-Pro:test Lxiin$ cd
LxiindeMacBook-Pro:~ Lxiin$ cd ~/Desktop/test
LxiindeMacBook-Pro:test Lxiin$ pwd
/Users/Lxiin/Desktop/test/
6.查看文件內(nèi)容:cat/tac/head/tail/more/less/rev/od
cat:查看文件的內(nèi)容
LxiindeMacBook-Pro:test1 Lxiin$ cat test.txt
abcd
1234
tac:將每個指定文件按行倒置并寫到標(biāo)準(zhǔn)輸出
LxiindeMacBook-Pro:test1 Lxiin$ tac test.txt
1234
abcd
rev:顯示最后一個字符到第一個字符
LxiindeMacBook-Pro:test1 Lxiin$ rev test.txt
dcba
4321
7.拷貝:cp
用法:cp ([參數(shù)]) [目標(biāo)文件] [目標(biāo)目錄]
LxiindeMacBook-Pro:test1 Lxiin$ tree
.
├── test.txt
└── test2
1 directory, 1 file
LxiindeMacBook-Pro:test1 Lxiin$ cp test.txt test2/
LxiindeMacBook-Pro:test1 Lxiin$ tree
.
├── test.txt
└── test2
└── test.txt
1 directory, 2 files
8.移動:mv
mv命令可用來進(jìn)行文件、文件夾的改名,和移動文件和文件夾。
- 文件/文件夾改名:
mv [源文件/文件夾] [需要更改的名稱]
LxiindeMacBook-Pro:test Lxiin$ ls
test1
LxiindeMacBook-Pro:test Lxiin$ mv test1/ test2
LxiindeMacBook-Pro:test Lxiin$ ls
test2
LxiindeMacBook-Pro:test Lxiin$
- 文件/文件夾移動:
mv [源文件/文件夾] [移動的目標(biāo)文件/文件夾]
LxiindeMacBook-Pro:test Lxiin$ ls
test.txt test1
LxiindeMacBook-Pro:test Lxiin$ mv test.txt test1
LxiindeMacBook-Pro:test Lxiin$ tree
.
└── test1
└── test.txt
1 directory, 1 file
9.文件重命名:rename
rename相比于mv重命名文件,優(yōu)勢在于可以批量操作:將txt后綴的文檔批量修改為rtf后綴的文檔
LxiindeMacBook-Pro:test1 Lxiin$ rename .txt .rtf *.txt
10.查找文件:find
使用方法:find [PATH] [option] [action]
- 與時間有關(guān)的參數(shù):
-atime-ctime-mtime
查找當(dāng)前目錄下過去24小時有改動的文件(其中0代表過去的24小時,換成3則是3天前的24小時內(nèi)):
LxiindeMacBook-Pro:test1 Lxiin$ find . -mtime 0
.
./test1.txt
./test.txt
- 與用戶或用戶組名有關(guān)的參數(shù):
-uid-gid-user-group
查找當(dāng)前目錄下Lxiin創(chuàng)建的文件:
LxiindeMacBook-Pro:test1 Lxiin$ find . -user Lxiin
.
./test1.txt
./test.txt
LxiindeMacBook-Pro:test1 Lxiin$ ls -l
total 0
-rw-r--r-- 1 Lxiin staff 0 6 17 20:32 test.txt
-rw-r--r-- 1 Lxiin staff 0 6 17 20:37 test1.txt
- 與文件權(quán)限及名稱有關(guān)的參數(shù):
-name-type-perm
查找當(dāng)前目錄下名稱為‘test.txt’的文件:
LxiindeMacBook-Pro:test1 Lxiin$ find . -name test.txt
./test.txt
查找當(dāng)前目錄下名稱帶有'.txt'后綴的文件:
LxiindeMacBook-Pro:test1 Lxiin$ find . -name '*.txt'
./test1.txt
./test.txt
11.創(chuàng)建鏈接:ln
使用方法:ln [-sf] [源文件] [目標(biāo)文件]
-
-s:如果不加任何參數(shù)進(jìn)行鏈接,就是硬鏈接 -
-f:如果目標(biāo)文件存在,就將目標(biāo)文件直接刪除后再創(chuàng)建
將‘test.txt’文件創(chuàng)建一個符號鏈接:
LxiindeMacBook-Pro:test1 Lxiin$ ln -s test.txt test
LxiindeMacBook-Pro:test1 Lxiin$ ls -ls
total 0
0 lrwxr-xr-x 1 kingsoft staff 8 6 18 23:14 test -> test.txt
0 -rw-r--r-- 1 kingsoft staff 0 6 17 20:32 test.txt
12.壓縮和解壓縮:gzip/gunzip
壓縮‘test.txt‘文件:
LxiindeMacBook-Pro:test1 Lxiin$ gzip test.txt
LxiindeMacBook-Pro:test1 Lxiin$ ls
test.txt.gz
解壓’test.txt.gz‘文件:
LxiindeMacBook-Pro:test1 Lxiin$ gunzip test.txt.gz
LxiindeMacBook-Pro:test1 Lxiin$ ls
test.txt