linux案例2

文件操作命令 —— touch、cp
touch命令
用途:新建空文件,或更新文件時間標記
格式:touch 文件名…
cp命令
用途:複製(Copy)文件或目錄
格式:cp [選項]… 源文件或目錄… 目標文件或目錄
常用命令選項
-r:遞歸複製整個目錄樹

文件操作命令 —— rm、mv
rm命令
用途:刪除(Remove)文件或目錄
格式:rm [選項]… 文件或目錄
常用命令選項
-r:遞歸刪除整個目錄樹
-f
rmdir命令
用途:刪除(directory)目錄
mv命令
用途:移動(Move)文件或目錄
—— 若如果目標位置與源位置相同,則相當於改名
格式:mv [選項]… 源文件或目錄… 目標文件或目錄

案例:
[root@localhost /]#
[root@localhost /]# ls 顯示當前目錄內容
a boot etc lib media opt root sbin sys usr
bin dev home lib64 mnt proc run srv tmp var
[root@localhost /]# touch /a/a.txt 在/a/創建a.txt文件
[root@localhost /]# ls /a/ 顯示/a/下目錄內容
a.txt b
[root@localhost /]# cd /a 切換到/a目錄下
[root@localhost a]# touch aa 在當前目錄下創建aa文件(注意:只要用touch命令創建的肯定是文件,可以不加擴展名)
[root@localhost a]# ls 顯示當前目錄內容
aa a.txt b
[root@localhost a]# ls –l 以長格式顯示abc下所有子目錄和文件的信息
總用量 0
-rw-r–r--. 1 root root 0 3月 1 15:35 aa -開頭的代表文件
-rw-r–r--. 1 root root 0 3月 1 15:33 a.txt -開頭的代表文件
drwxr-xr-x. 3 root root 14 3月 1 15:31 b d開頭的代表文件目錄
[root@localhost a]# cp aa /opt/abc/ 拷貝aa到/opt/abc/目錄下
[root@localhost a]# ls 顯示當前目錄內容
aa a.txt b aa文件還在當前目錄
[root@localhost a]# ls /opt/abc/ 顯示/opt/abc/下文件內容
a1 aa aa文件已經拷貝到/opt/abc/目錄下
[root@localhost a]# cp -r b /opt/abc/ 拷貝b目錄到/opt/abc/目錄下(前面加-r)
[root@localhost a]# ls 顯示當前目錄內容
aa a.txt b b目錄還在當前目錄
[root@localhost a]# ls /opt/abc/ 顯示/opt/abc/下文件內容
a1 aa b b目錄已經拷貝到/opt/abc/目錄下
[root@localhost a]# cd /opt/abc/b 切換到/opt/abc/b目錄
[root@localhost b]# ls 顯示當前目錄內容
c
[root@localhost b]#
[root@localhost b]# cd /a/ 切換到/a/目錄
[root@localhost a]# ls 顯示當前目錄內容
aa a.txt b
[root@localhost a]# mv a 移動a,按tab鍵,兩下顯示當前目錄以a開頭的內容
aa a.txt
[root@localhost a]# mv a.txt /opt/abc/ 移動a.txt文件到/opt/abc/
[root@localhost a]# ls 顯示當前目錄內容
aa b a.txt文件已被移走
[root@localhost a]# ls /opt/abc/ 顯示/opt/abc/目錄內容
a1 aa a.txt b a.txt文件已被移到/opt/abc/目錄中
[root@localhost a]# cd b/ 切換到b目錄
[root@localhost b]# ls 顯示當前目錄內容
c
[root@localhost b]# mv c /opt/abc/ 移動c目錄到/opt/abc/
[root@localhost b]# ls 顯示當前目錄內容
[root@localhost b]# 內容爲空,c目錄已被移走
[root@localhost b]# cd /opt/abc 切換到/opt/abc/目錄
[root@localhost abc]# ls –l 以長格式顯示當前目錄下所有子目錄和文件的信息
總用量 0
drwxr-xr-x. 2 root root 6 3月 1 09:41 a1
-rw-r–r--. 1 root root 0 3月 1 15:42 aa
-rw-r–r--. 1 root root 0 3月 1 15:33 a.txt
drwxr-xr-x. 3 root root 14 3月 1 15:44 b
drwxr-xr-x. 2 root root 6 3月 1 15:31 c
[root@localhost abc]# rm c 刪除c目錄(錯誤方法)
rm: 無法刪除"c": 是一個目錄 無法刪除c,因爲它是一個目錄
[root@localhost abc]# rm -r c 刪除c目錄(正確方法,前面加-r)
rm:是否刪除目錄 “c”?y 是否刪除,是輸入y,否輸入n
[root@localhost abc]# rm -r b 刪除b目錄
rm:是否進入目錄"b"? y 是否進入b目錄,是輸入y,否輸入n(因爲b目錄裏有子目錄)
rm:是否刪除目錄 “b/c”?n 是否刪除目錄,是輸入y,否輸入n
[root@localhost abc]# ls –l 以長格式顯示當前目錄下所有子目錄和文件的信息
總用量 0
drwxr-xr-x. 2 root root 6 3月 1 09:41 a1
-rw-r–r--. 1 root root 0 3月 1 15:42 aa
-rw-r–r--. 1 root root 0 3月 1 15:33 a.txt
drwxr-xr-x. 3 root root 14 3月 1 15:44 b b目錄依然存在
[root@localhost abc]# ls -l b/ 以長格式顯示當b目錄下所有子目錄和文件的信息
總用量 0
drwxr-xr-x. 2 root root 6 3月 1 15:44 c c目錄依然存在
[root@localhost abc]# ls
a1 aa a.txt b
[root@localhost abc]# rm -r -f b 強制刪除b目錄及以下所有子目錄
[root@localhost abc]# ls 顯示當前目錄內容
a1 aa a.txt b目錄及以下子目錄已被刪除
[root@localhost abc]#

文件操作命令 —— find
find命令
用途:用於查找文件或目錄
格式:find [查找範圍] [查找條件]
常用查找條件
-name:按文件名稱查找
-user: 按文件屬主查找
-type: 按文件類型查找
f 查找文件
d 查找目錄
l 鏈接
p 管道
-size 按大小查找

結合操作演示過程,詳細講解find查找工具的相關用法,主要包括:
按名稱查找:關鍵字爲“-name”,根據目標文件的部分名稱查找,允許使用“*”及“?”通配符。
按文件屬主查找:關鍵字爲“-user”,根據文件是否屬於目標用戶進行查找。
按文件類型查找:關鍵字爲“-type”,根據文件的類型進行查找,這裏的類型指的是普通文件(f)、目錄(d)
案例:
[root@localhost abc]#
[root@localhost abc]# cd 切換目錄(默認切換到家目錄)
[root@localhost ~]# pwd 顯示當前目錄(~代表/root目錄)
/root
[root@localhost ~]# find / -name “aa” 查找名字爲aa的所有文件或目錄
/usr/share/locale/aa
/opt/abc/aa
/a/aa
[root@localhost ~]# find / -name “aa” -type f 查找名字爲aa文件類型爲文件的所有文件
/opt/abc/aa
/a/aa
[root@localhost ~]# find / -user “student” -type d 查找屬主student文件類型爲目錄的所有目錄
/home/student
/home/student/.mozilla
/home/student/.mozilla/extensions
/home/student/.mozilla/plugins
[root@localhost ~]#

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