歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> Linux基礎知識之文件管理命令(cp、mv、rm)

Linux基礎知識之文件管理命令(cp、mv、rm)

日期:2017/2/28 13:50:17   编辑:Linux教程

我們日常工作中對於文件的操作用到最多的幾個我覺得應該是是復制、剪切、移動、重命名、刪除這五個。因此今天主要介紹關於上面五個操作在Linux中實現涉及到的三個命令:cp、mv、rm。

cp是copy的簡寫,從名字我們大致就能知道它的作用,它主要是用來復制文件的。雖然這個命令很簡單,可是它有很多細節需要我們注意,如果稍有忽略那麼命令很有可能無法達到我們想要的結果。

以下信息來自man手冊
NAME
cp - copy files and directories

單源復制(源文件為文本文件)
其命令格式為:

cp [OPTION]... [-T] SOURCE DEST
如果DEST不存在:則事先創建此文件,並復制源文件的數據流至DEST中;
如果DEST存在:1、如果DEST是非目錄文件,則覆蓋目標文件;2、如果DEST是目錄文件,則先在DEST目錄下創建同名文件並復制數據流

多源復制(源文件為目錄文件,且其下有多個文件)
其命令格式:
cp [OPTION]... SOURCE... DIRECTORY
cp [OPTION]... -t DIRECTORY SOURCE...
如果DEST不存在:報錯
如果DEST存在:1、如果DEST是非目錄文件,報錯;2、如果目錄是目錄文件,則分別復制每個文件至目標目錄中,並保持原名。

常用選項:
-i, --interactive
prompt before overwrite (overrides a previous -n option)
交互式復制,覆蓋之前提醒用戶確認

-R, -r, --recursive
copy directories recursively
遞歸復制目錄
-d same as --no-dereference --preserve=links
復制符號鏈接文件本身,而非其指向的源文件
-a, --archive
same as -dR --preserve=all
用於實現歸檔
-f, --force
if an existing destination file cannot be opened, remove it and try again (this ignored when the -n option is also used)
強制覆蓋目標文件
--preserve[=ATTR_LIST]
preserve the specified attributes (default: mode,ownership,timestamps), if possible additional attributes: context, links, xattr, all
mode:權限 ownership:屬主和屬組
timestamps:時間戳 context:安全標簽
xattr:擴展屬性 links:符號鏈接
all:上述所有屬性
-v, --verbose
explain what is being done
詳細展示命令的運行過程


--------------------------------------------------------------------------------
看了上面的內容下面通過幾個例子來對cp命令及其選項有一個更具體的認識

1、使用別名命令,每日將/etc/目錄下所有文件,備份到/testdir/下獨立的新目錄下,並要求新目錄格式為backupYYYY-mm-dd,備份過程可見
[root@localhost ~]# mkdir /testdir
[root@localhost ~]# cp -rdv /etc/ /testdir/bakup$(date +%F)
"/etc/rsyslog.conf" -> "/testdir/bakup2016-08-01/rsyslog.conf"
"/etc/vimrc" -> "/testdir/bakup2016-08-01/vimrc"
"/etc/pulse" -> "/testdir/bakup2016-08-01/pulse"
"/etc/pulse/client.conf" -> "/testdir/bakup2016-08-01/pulse/client.conf"
"/etc/pulse/daemon.conf" -> "/testdir/bakup2016-08-01/pulse/daemon.conf"
"/etc/pulse/default.pa" -> "/testdir/bakup2016-08-01/pulse/default.pa"
"/etc/pulse/system.pa" -> "/testdir/bakup2016-08-01/pulse/system.pa"
...
..
.
"/etc/screenrc" -> "/testdir/bakup2016-08-01/screenrc"

因為/etc/下文件很多,因此需要等一會。
12 [root@localhost ~]# ls /testdir/
bakup2016-08-01

結果滿足要求,下面解釋下使用cp那三個選項的原因,給出的要求是首先要將/etc目錄下的所有文件都備份,因此需要使用遞歸選項-r,後面要求備份過程可見,於是又加上-v,至於-d是因為要保持備份文件與源文件的一致性,雖然沒有明確要求,不過加上-d顯得更好點。

2、先創建/testdir/rootdir目錄,再復制/root所有下文件到該目錄內,並要求保留原有權限。
[root@localhost ~]# mkdir /testdir/rootdir
[root@localhost ~]# cp -r --preserve=mode,ownership /root /testdir/rootdir/
[root@localhost ~]# ll /root /testdir/rootdir/root/
/root:
總用量 20
-rw-------. 1 root root 1172 7月 20 00:38 anaconda-ks.cfg
drwxr-xr-x. 2 root root 6 7月 21 18:52 CST
-rw-r--r--. 1 root root 14 8月 1 10:49 file
-rw-------. 1 root root 1220 7月 19 16:46 initial-setup-ks.cfg
-rw-r--r--. 1 root root 4282 8月 1 12:17 test.txt
-rw-r--r--. 1 root root 0 8月 1 12:20 tr
drwxr-xr-x. 2 root root 6 7月 22 09:52 公共
drwxr-xr-x. 2 root root 6 7月 22 09:52 模板
drwxr-xr-x. 2 root root 6 7月 22 09:52 視頻
drwxr-xr-x. 2 root root 6 7月 22 09:52 圖片
drwxr-xr-x. 2 root root 6 7月 22 09:52 文檔
drwxr-xr-x. 2 root root 6 7月 22 09:52 下載
drwxr-xr-x. 2 root root 6 7月 22 09:52 音樂
drwxr-xr-x. 2 root root 6 7月 22 09:52 桌面
/testdir/rootdir/root/:
總用量 20
-rw-------. 1 root root 1172 8月 1 22:06 anaconda-ks.cfg
drwxr-xr-x. 2 root root 6 8月 1 22:06 CST
-rw-r--r--. 1 root root 14 8月 1 22:06 file
-rw-------. 1 root root 1220 8月 1 22:06 initial-setup-ks.cfg
-rw-r--r--. 1 root root 4282 8月 1 22:06 test.txt
-rw-r--r--. 1 root root 0 8月 1 22:06 tr
drwxr-xr-x. 2 root root 6 8月 1 22:06 公共
drwxr-xr-x. 2 root root 6 8月 1 22:06 模板
drwxr-xr-x. 2 root root 6 8月 1 22:06 視頻
drwxr-xr-x. 2 root root 6 8月 1 22:06 圖片
drwxr-xr-x. 2 root root 6 8月 1 22:06 文檔
drwxr-xr-x. 2 root root 6 8月 1 22:06 下載
drwxr-xr-x. 2 root root 6 8月 1 22:06 音樂
drwxr-xr-x. 2 root root 6 8月 1 22:06 桌面

依然是復制目錄依然是使用遞歸選項-r,這次使用--preserve主要是為了滿足保留權限這一要求。由結果可知滿足要求。

3、復制/etc/system-release文件,比較加-d與不加-d有何區別

[root@localhost ~]# cp /etc/system-release /test/t1
[root@localhost ~]# cp -d /etc/system-release /test/t2
[root@localhost ~]# ll /test/
總用量 2
-rw-r--r--. 1 root root 38 8月 1 22:22 t1
lrwxrwxrwx. 1 root root 14 8月 1 22:22 t2 -> CentOS-release
[root@localhost test]# cat t1
CentOS Linux release 7.2.1511 (Core)
[root@localhost test]# cat t2
cat: t2: 沒有那個文件或目錄

由結果的比較我們可以知道加-d我們復制的是原始文件,若目標是鏈接文件我們復制的只是鏈接文件,而不是原始文件。

--------------------------------------------------------------------------------
mv 是move縮寫,主要作用是移動和重命名文件

mv [OPTION]... [-T] SOURCE DEST
mv [OPTION]... SOURCE... DIRECTORY
mv [OPTION]... -t DIRECTORY SOURCE...
在同一路徑下,mv作用為重命名
在不同路徑下,mv作用為移動文件或剪切
常用選項:
-v, --verbose
explain what is being done
-i prompt before every removal
以上兩種選項上文已有例子說明,這裡不再具體演示。

--------------------------------------------------------------------------------
下面以實驗說明mv的具體作用

1、在/test/目錄下更改t1文件名為t3
[root@localhost test]# ll
總用量 0
-rw-r--r--. 1 root root 0 8月 1 22:36 t1
[root@localhost test]# mv t1 t3
[root@localhost test]# ll
總用量 0
-rw-r--r--. 1 root root 0 8月 1 22:36 t3

2、將/test/目錄下的t3文件移動至/testdir/目錄下

[root@localhost test]# ll /testdir/
總用量 0
[root@localhost test]# mv t3 /testdir/
[root@localhost test]# ll
總用量 0
[root@localhost test]# ll /testdir/
總用量 0
-rw-r--r--. 1 root root 0 8月 1 22:36 t3

--------------------------------------------------------------------------------
rm - remove files or directories 移除文件或目錄

格式: rm [OPTION]... FILE...
常用選項:
-f, --force 強制刪除
ignore nonexistent files and arguments, never prompt
-i prompt before every removal 交互模式

-r, -R, --recursive 遞歸刪除
remove directories and their contents recursively
-v, --verbose 顯示刪除詳細過程
explain what is being done


--------------------------------------------------------------------------------
下面以實驗來具體演示rm的作用

1、刪除/test/目錄下t1文件
[root@localhost test]# rm t1
rm:是否刪除普通空文件 "t1"?y
[root@localhost test]# ll
總用量 0
drwxr-xr-x. 2 root root 18 8月 1 22:46 file

在root用戶下其默認情況下“cp=cp -i”
2、刪除/test/目錄下的file文件夾
[root@localhost test]# rm file/
rm: 無法刪除"file/": 是一個目錄
[root@localhost test]# rm -f file/
rm: 無法刪除"file/": 是一個目錄

我們發現僅僅使用rm且加上強制刪除選項也無法將目錄給刪除。

刪除目錄需要加上-r遞歸,刪除目錄連帶將其下面的所有文件都給刪掉。
[root@localhost test]# rm -rfv file/
已刪除"file/file1"
已刪除目錄:"file/"

其刪除過程是先從最底層的文件一個個刪除之後向上再次刪除一直刪到最上層為止。

Copyright © Linux教程網 All Rights Reserved