Loose-Info.com
Last Update 2014/03/11
TOP - Unix系OS - Linux - rm(1)

ファイルやディレクトリを削除します。

rm オプション1 ファイルパス1 ... オプション1(任意) -f 確認せずに削除する。また、ファイルが存在しない場合でもエラーを返さない -i 削除前に確認する -r ディレクトリを再帰的に削除する ファイルパス1(必須) 削除対象のファイル(ディレクトリ)

※ オプションなどは個人的に重要と考えられるものを記述しており、記載の無いものは、manページや関連書籍などを参照願います。
※ 実行例の記述は、不要と考えられる部分の削除などの修正を行ったものを掲載しています。
※ 実行例の実行環境はUbuntu 12.10です。

(例) オプション無し
$ rm test.txt

実行前
$ ls -l total 4 -rw-r--r-- 1 ******* ******* 5 Dec 23 11:17 test.txt

実行後
$ ls -l total 0

(例) オプション無し 対象がディレクトリの場合
$ rm testdir

実行前
$ ls -l total 4 drwxr-xr-x 2 ******* ******* 4096 Dec 23 11:29 testdir

実行結果
$ rm testdir rm: cannot remove `testdir': Is a directory ← 削除不可

(例) オプション無し 書き込み不可の場合
実行前
$ ls -l total 4 -r--r--r-- 1 ******* ******* 5 Dec 23 11:50 test.txt

実行結果
$ rm test.txt rm: remove write-protected regular file `test.txt'? y ← 確認メッセージ $ ls -l total 0

(例) -f オプション 書き込み不可の場合
実行前
$ ls -l total 4 -r--r--r-- 1 ******* ******* 5 Dec 23 11:50 test.txt

実行結果
$ rm -f test.txt $ ls -l total 0 ← 削除完了

(例) -i オプション
$ rm -i test.txt

実行前
$ ls -l total 4 -rw-r--r-- 1 ******* ******* 5 Dec 23 16:04 test.txt

確認メッセージ
rm: remove regular file `test.txt'?

実行後(確認メッセージで「y」を入力)
$ rm -i test.txt rm: remove regular file `test.txt'? y ← 「y」を入力 $ ls -l total 0

(例) -r オプション
$ rm -r testdir1

実行前
$ ls -Rl .: total 4 drwxr-xr-x 3 testuser testgroup1 4096 Dec 23 16:57 testdir1 ./testdir1: total 4 drwxr-xr-x 3 testuser testgroup1 4096 Dec 23 18:44 testdir2 ./testdir1/testdir2: total 4 drwxr-xr-x 2 testuser testgroup1 4096 Dec 23 18:44 testdir3 ./testdir1/testdir2/testdir3: total 0

実行結果
$ rm -r testdir1 $ ls -Rl .: total 0