Linux基础之文件管理三兄弟(cp、mv、rm)
? ? 我們?nèi)粘9ぷ髦袑?duì)于文件的操作用到最多的幾個(gè)我覺得應(yīng)該是是復(fù)制、剪切、移動(dòng)、重命名、刪除這五個(gè)。因此今天主要介紹關(guān)于上面五個(gè)操作在Linux中實(shí)現(xiàn)涉及到的三個(gè)命令:cp、mv、rm。
? ? cp是copy的簡(jiǎn)寫,從名字我們大致就能知道它的作用,它主要是用來復(fù)制文件的。雖然這個(gè)命令很簡(jiǎn)單,可是它有很多細(xì)節(jié)需要我們注意,如果稍有忽略那么命令很有可能無法達(dá)到我們想要的結(jié)果。
以下信息來自man手冊(cè)
NAME
? ? ? ?cp - copy files and directories
單源復(fù)制(源文件為文本文件) ?
其命令格式為:
? ? ? ?cp [OPTION]... [-T] SOURCE DEST
如果DEST不存在:則事先創(chuàng)建此文件,并復(fù)制源文件的數(shù)據(jù)流至DEST中;
如果DEST存在:1、如果DEST是非目錄文件,則覆蓋目標(biāo)文件;2、如果DEST是目錄文件,則先在DEST目錄下創(chuàng)建同名文件并復(fù)制數(shù)據(jù)流
多源復(fù)制(源文件為目錄文件,且其下有多個(gè)文件)
其命令格式:
? ? ? ?cp [OPTION]... SOURCE... DIRECTORY
? ? ? ?cp [OPTION]... -t DIRECTORY SOURCE...
如果DEST不存在:報(bào)錯(cuò)
如果DEST存在:1、如果DEST是非目錄文件,報(bào)錯(cuò);2、如果DEST是目錄文件,則分別復(fù)制每個(gè)文件至目標(biāo)目錄中,并保持原名。
常用選項(xiàng):
? ? ?-i, --interactive ?
? ? ? ? ? ? ? prompt before overwrite (overrides a previous -n option)
? ? ? ? ? ? ? ? ? ? ?交互式復(fù)制,覆蓋之前提醒用戶確認(rèn)
? ? -R, -r, --recursive
? ? ? ? ? ? ? copy directories recursively
? ? ? ? ? ? ? ? ? ? ? ? ? ?遞歸復(fù)制目錄
? ? -d ? ? same as --no-dereference --preserve=links
? ? ? ? ? ? ? ? ? 復(fù)制符號(hào)鏈接文件本身,而非其指向的源文件
? ? -a, --archive
? ? ? ? ? ? ? same as -dR --preserve=all
? ? ? ? ? ? ? ? ? ? ? ? ? 用于實(shí)現(xiàn)歸檔
? ? -f, --force
? ? ? ? ? ? ? if ?an existing destination file cannot be opened, remove it and try again (this ?ignored when the -n option is also used)
? ? ? ? ? ? ? ? ? ? ? 強(qiáng)制覆蓋目標(biāo)文件
? ? --preserve[=ATTR_LIST]
? ? ? ? ? ? ? preserve the specified attributes (default: mode,ownership,timestamps), if possible ?additional attributes: context, links, xattr, all
? ? ? ? ? ? mode:權(quán)限 ? ? ? ? ? ? ? ? ? ? ? ownership:屬主和屬組
? ? ? ? ? ? timestamps:時(shí)間戳 ? ? ? ? ? ? ? context:安全標(biāo)簽
? ? ? ? ? ? xattr:擴(kuò)展屬性 ? ? ? ? ? ? ? ? ?links:符號(hào)鏈接
? ? ? ? ? ? all:上述所有屬性
? ? -v, --verbose
? ? ? ? ? ? ? explain what is being done
? ? ? ? ? ? ? 詳細(xì)展示命令的運(yùn)行過程
看了上面的內(nèi)容下面通過幾個(gè)例子來對(duì)cp命令及其選項(xiàng)有一個(gè)更具體的認(rèn)識(shí)
1、使用別名命令,每日將/etc/目錄下所有文件,備份到/testdir/下獨(dú)立的新目錄下,并要求新目錄格式為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"因?yàn)?etc/下文件很多,因此需要等一會(huì)。
[root@localhost?~]#?ls?/testdir/ bakup2016-08-01結(jié)果滿足要求,下面解釋下使用cp那三個(gè)選項(xiàng)的原因,給出的要求是首先要將/etc目錄下的所有文件都備份,因此需要使用遞歸選項(xiàng)-r,后面要求備份過程可見,于是又加上-v,至于-d是因?yàn)橐3謧浞菸募c源文件的一致性,雖然沒有明確要求,不過加上-d顯得更好點(diǎn)。
2、先創(chuàng)建/testdir/rootdir目錄,再復(fù)制/root所有下文件到該目錄內(nèi),并要求保留原有權(quán)限。
[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?桌面依然是復(fù)制目錄依然是使用遞歸選項(xiàng)-r,這次使用--preserve主要是為了滿足保留權(quán)限這一要求。由結(jié)果可知滿足要求。
3、復(fù)制/etc/system-release文件,比較加-d與不加-d有何區(qū)別
[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:?沒有那個(gè)文件或目錄? ? 由結(jié)果的比較我們可以知道加-d我們復(fù)制的是原始文件,若目標(biāo)是鏈接文件我們復(fù)制的只是鏈接文件,而不是原始文件。
mv 是move縮寫,主要作用是移動(dòng)和重命名文件
? mv [OPTION]... [-T] SOURCE DEST
?mv [OPTION]... SOURCE... DIRECTORY
? mv [OPTION]... -t DIRECTORY SOURCE...
在同一路徑下,mv作用為重命名
在不同路徑下,mv作用為移動(dòng)文件或剪切
常用選項(xiàng):
?? ? ?-v, --verbose
? ? ? ? ? ? ? explain what is being done
? ? ? -i ? ? prompt before every removal ?
以上兩種選項(xiàng)上文已有例子說明,這里不再具體演示。
下面以實(shí)驗(yàn)說明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?t32、將/test/目錄下的t3文件移動(dòng)至/testdir/目錄下
rm - remove files or directories 移除文件或目錄
格式: rm [OPTION]... FILE...
常用選項(xiàng):
? ? -f, --force ? 強(qiáng)制刪除
? ? ? ? ? ? ? ignore nonexistent files and arguments, never prompt
? ? -i ? ? prompt before every removal ? 交互模式
? ? -r, -R, --recursive ? ?遞歸刪除
? ? ? ? ? ? ? remove directories and their contents recursively
? ? -v, --verbose ? ? ? ? ?顯示刪除詳細(xì)過程
? ? ? ? ? ? ? explain what is being done
下面以實(shí)驗(yàn)來具體演示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用戶下其默認(rèn)情況下“cp=cp -i”
2、刪除/test/目錄下的file文件夾
[root@localhost?test]#?rm?file/ rm:?無法刪除"file/":?是一個(gè)目錄 [root@localhost?test]#?rm?-f?file/ rm:?無法刪除"file/":?是一個(gè)目錄我們發(fā)現(xiàn)僅僅使用rm且加上強(qiáng)制刪除選項(xiàng)也無法將目錄給刪除。
刪除目錄需要加上-r遞歸,刪除目錄連帶將其下面的所有文件都給刪掉。
[root@localhost?test]#?rm?-rfv?file/ 已刪除"file/file1" 已刪除目錄:"file/"其刪除過程是先從最底層的文件一個(gè)個(gè)刪除之后向上再次刪除一直刪到最上層為止。
轉(zhuǎn)載于:https://blog.51cto.com/chawan/1833275
總結(jié)
以上是生活随笔為你收集整理的Linux基础之文件管理三兄弟(cp、mv、rm)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux 第五天: (08月01日)
- 下一篇: Linux的I/O多路复用机制之--se