linux經常使用shell命令之文件操做命令

# touch 新建空文件,或更新文件時間標記
touch path/filename
user@computer: ~$ touch myfile  # 在當前目錄下新建myfile
user@computer: ~$ touch /user/local/txt/myfile  # 在指定目錄下新建myfile
user@computer: ~$ touch -c -t 06071700 /myfile  # 將myfile的時間記錄修改成6月7日17:00
複製代碼
# cp 複製文件或目錄
cp [選項] source object
經常使用命令選項
-r:遞歸複製整個目錄樹
-f:   強制覆蓋同名文件
user@computer: ~$ cp /etc/passwd  ~/test
user@computer: ~$ cp -r /etc /tmp
複製代碼
# rm 刪除文件或目錄
rm  [選項] [file|dir]
經常使用命令選項
-r:遞歸刪除整個目錄樹
-f: 即便文件的屬性設置爲只讀,亦直接刪除,無需逐一確認
user@computer: ~$ rm  ~/file
user@computer: ~$ rm -r ~/directory  # 刪除目錄時,應該加上-r選項,不然會失敗
複製代碼
# mv 移動文件或目錄; 若目標位置與源位置相同,則至關於更名
mv [選項] 源文件或目錄 目標文件或目錄
user@computer: ~$ mv /root/pic/*.png /usr/local/share/pic  # 將/root/pic目錄下的全部後綴名爲」*.png」的文件移動到/usr/local/share/pic目錄下
user@computer: ~$ mv /root/pic/kpic.png /root/pic/life.png # 把kpic.png文件更名爲life.png
複製代碼
# find 查找文件或目錄
find [查找範圍] [查找條件]
經常使用查找條件
-name:按文件名稱查找
-user:按文件屬主查找
-type:按文件類型查找 取值:[f|d|l|p]	
-size: 按大小查找 取值:[+|-] n 單位爲字節或塊

user@computer: ~$ find /etc -name p* -type f  # 查找etc下以p開頭的文件
user@computer: ~$ find /etc -user root  # 查找etc下屬主爲root的文件或目錄

-o:邏輯或
-a:邏輯與
user@computer: ~$ find /etc -size +2048 -a -size -20480  # 在/etc目錄下查找大於1MB小於10MB的文件

-exec: 對查找到的結果進行操做
find [條件] -exec shell命令 {} \;  # {}表示找到的結果集
user@computer: ~$ find / -type f –size 0 –exec ls –l {} \;  # 查看文件長度爲0的普通文件,並列出完整路徑
複製代碼
# ln 連接文件,給系統中已有的某個文件指定另一個可用於訪問的名稱。 
默認建立文件的一個硬連接
-s 建立一個軟連接,其做用至關於windows中的快捷方式
user@computer: ~$ ln file1 file2  # 建立了file1的硬連接file2 刪除其中之一對雙方均無影響
user@computer: ~$ ln –s file1 file3  # 建立file1的軟鏈接file3,刪除file1後file3失效,若是從新給一個與file1同路徑同名文件,連接文件又會恢復; 刪除file3對file1無影響
複製代碼
相關文章
相關標籤/搜索