命令簡介:
rm命令用來刪除Linux系統中的檔案或目錄。通常情況下rm不會刪除目錄,你必須通過指定參數-r或-R來刪除目錄。另外rm通常可以將該檔案或目錄恢複(注意,rm刪除檔案其實只是將指向資料區塊的索引點(information nodes)釋放,只要不被覆蓋,資料其實還在硬碟上。如果想要保證檔案的內容無法複原,可以使用命令shred 。 另外一般還是要慎用rm -rf *這樣的命令。
執行許可權 :All User
指令所在路徑:/bin/rm
命令文法:
rm [OPTION]... FILE...
命令參數:
|
參數 |
長參數 |
描敘 |
|
-f |
--force |
強制移除,忽略不存在的檔案,從不給出提示。 |
|
-i |
--interactive |
互動模式刪除檔案,刪除檔案前給出提示。 |
|
-r |
--recursive |
遞迴的刪除目錄下面檔案以及子目錄下檔案。 |
|
-R |
--recursive |
遞迴的刪除目錄下面檔案以及子目錄下檔案。 |
|
-v |
--verbose |
顯示運行時詳細資料 |
|
--help |
顯示命令線上協助 |
|
--version |
顯示命令版本資訊 |
使用樣本:
1:查看擷取rm命令的協助資訊。
[root@DB-Server kerry]# man rmRM(1) User Commands RM(1) NAME rm - remove files or directories SYNOPSIS rm [OPTION]... FILE... DESCRIPTION This manual page documents the GNU version of rm. rm removes each specified file. By default, it does not remove directories. If a file is unwritable, the standard input is a tty, and the -f or --force option is not given, rm prompts the user for whether to remove the file. If the response is not affirmative, the file is skipped. OPTIONS Remove (unlink) the FILE(s). -f, --force ignore nonexistent files, never prompt -i, --interactive prompt before any removal --no-preserve-root do not treat 鈥?鈥?specially (the default) --preserve-root fail to operate recursively on 鈥?鈥? -r, -R, --recursive remove directories and their contents recursively -v, --verbose explain what is being done --help display this help and exit --version output version information and exit By default, rm does not remove directories. Use the --recursive (-r or -R) option to remove each listed directory, too, along with all of its contents. To remove a file whose name starts with a 鈥?鈥? for example 鈥?foo鈥? use one of these commands: rm -- -foo rm ./-foo Note that if you use rm to remove a file, it is usually possible to recover the contents of that file. If you want more assurance that the contents are truly unrecoverable, consider using shred. AUTHOR Written by Paul Rubin, David MacKenzie, Richard Stallman, and Jim Meyering. REPORTING BUGS Report bugs to <bug-coreutils@gnu.org>. COPYRIGHT Copyright 漏 2006 Free Software Foundation, Inc. This is free software. You may redistribute copies of it under the terms of the GNU General Public License <http://www.gnu.org/licenses/gpl.html>. There is NO WARRANTY, to the extent permitted by law. SEE ALSO chattr(1), shred(1) The full documentation for rm is maintained as a Texinfo manual. If the info and rm programs are properly installed at your site, the command info rm should give you access to the complete manual. rm 5.97 June 2011 RM(1)
[root@DB-Server kerry]# rm --helpUsage: rm [OPTION]... FILE...Remove (unlink) the FILE(s). -f, --force ignore nonexistent files, never prompt -i, --interactive prompt before any removal --no-preserve-root do not treat `/' specially (the default) --preserve-root fail to operate recursively on `/' -r, -R, --recursive remove directories and their contents recursively -v, --verbose explain what is being done --help display this help and exit --version output version information and exit By default, rm does not remove directories. Use the --recursive (-r or -R)option to remove each listed directory, too, along with all of its contents. To remove a file whose name starts with a `-', for example `-foo',use one of these commands: rm -- -foo rm ./-foo Note that if you use rm to remove a file, it is usually possible to recoverthe contents of that file. If you want more assurance that the contents aretruly unrecoverable, consider using shred. Report bugs to bug-coreutils@gnu.org.
2:刪除指定檔案
如下所示,該目錄下有20個檔案,刪除指定檔案後,只剩下19個檔案了。
[oracle@DB-Server bdump]$ ls -lrt | wc -l 20 [oracle@DB-Server bdump]$ rm gsp_lgwr_7826.trc [oracle@DB-Server bdump]$ ls -lrt | wc -l 19
3:刪除檔案顯示運行時詳細資料
[oracle@DB-Server bdump]$ rm -v gsp_lgwr_4617.trc removed `gsp_lgwr_4617.trc'
4:互動模式下刪除檔案
如下所示,輸入y後就會刪除檔案,不想刪除則資料n
[oracle@DB-Server bdump]$ rm -iv gsp_lgwr_5005.trc rm: remove regular file `gsp_lgwr_5005.trc'? y removed `gsp_lgwr_5005.trc' [oracle@DB-Server bdump]$ rm -iv gsp_lgwr_7826.trc rm: remove regular file `gsp_lgwr_7826.trc'? n
5: 遞迴刪除某個目錄下檔案以及子目錄
如下所示,kerry目錄下有test1.txt、 test2.txt 檔案,以及子目錄tmp, 而子目錄下又有檔案file1、file2.
[root@DB-Server ~]# tree kerrykerry|-- test1.txt|-- test2.txt`-- tmp |-- file1 `-- file2 1 directory, 4 files[root@DB-Server ~]# rm -rf kerry/
6: 刪除空目錄
[root@DB-Server ~]# mkdir kerry [root@DB-Server ~]# rm -r kerry/ rm: remove directory `kerry/'? y
7:刪除目錄bdump下的所有trc檔案類型的檔案。
[oracle@DB-Server bdump]$ lsalert_GSP.log gsp_lgwr_20025.trc gsp_lgwr_5023.trc gsp_p000_4637.trc gsp_p000_4718.trc gsp_p000_5025.trc gsp_p001_4639.trc gsp_p001_4720.trc gsp_p001_5027.trcgsp_lgwr_19915.trc gsp_lgwr_4719.trc gsp_lgwr_5089.trc gsp_p000_4682.trc gsp_p000_4739.trc gsp_p000_5043.trc gsp_p001_4684.trc gsp_p001_4741.trc gsp_p001_5045.trc[oracle@DB-Server bdump]$ rm -v *.trcremoved `gsp_lgwr_19915.trc'removed `gsp_lgwr_20025.trc'removed `gsp_lgwr_4719.trc'removed `gsp_lgwr_5023.trc'removed `gsp_lgwr_5089.trc'removed `gsp_p000_4637.trc'removed `gsp_p000_4682.trc'removed `gsp_p000_4718.trc'removed `gsp_p000_4739.trc'removed `gsp_p000_5025.trc'removed `gsp_p000_5043.trc'removed `gsp_p001_4639.trc'removed `gsp_p001_4684.trc'removed `gsp_p001_4720.trc'removed `gsp_p001_4741.trc'removed `gsp_p001_5027.trc'removed `gsp_p001_5045.trc'
以上所述是本文給大家介紹的Linux命令學習總結之rm命令,希望對大家有所協助。