【rm】Linuxコマンド_ファイルを削除する

LinuxOSでファイルを削除するには、「rm」コマンドを使用します。




↓↓↓ITエンジニアのおすすめ学習・開発環境の詳細へ↓↓↓

「rm」コマンドの使い方(Linux)

カレントディレクトリ上の「test01.txt」ファイルを削除します。

$ ls -lF
total 24
drwxr-xr-x 2 root root 4096 Jul 17 02:21 test01/
-rw-r--r-- 1 root root   36 Jul 18 02:04 test01.txt
drwxr-xr-x 2 root root 4096 Jul 17 02:21 test02/

以下、rmコマンドで対象ファイルを削除します。
最新のOSでは「-i」オプションをを使用しなくても、以下のようにファイルの削除をするか、しないかを確認するようになっています。

  • 削除する場合:y
  • 削除しない場合:n
$ rm test01.txt 
rm: remove regular file 'test01.txt'? y

rmコマンド実行後、ファイルが削除されたことが確認できます。

$ ls -lF
total 16
drwxr-xr-x 2 root root 4096 Jul 17 02:21 test01/
drwxr-xr-x 2 root root 4096 Jul 17 02:21 test02/

ファイルをまとめて削除

rmコマンドでは複数のファイルを一括で削除することが可能です。
以下、「test02.txt」と「test03.txt」ファイルをまとめて削除した実行例となります。

$ ls -lF
total 16
drwxr-xr-x 2 root root 4096 Jul 17 02:21 test01/
drwxr-xr-x 2 root root 4096 Jul 17 02:21 test02/
-rw-r--r-- 1 root root    0 Jul 18 02:07 test02.txt
-rw-r--r-- 1 root root    0 Jul 18 02:07 test03.txt

複数のファイルを削除するには、削除対象ファイル間に「スペース」を入れ実行します。

$ rm test02.txt test03.txt 
rm: remove regular empty file 'test02.txt'? y
rm: remove regular empty file 'test03.txt'? y

rmコマンド実行後、ファイルが削除されたことが確認できます。

$ ls -lF
total 16
drwxr-xr-x 2 root root 4096 Jul 17 02:21 test01/
drwxr-xr-x 2 root root 4096 Jul 17 02:21 test02/

削除の有無を確認せずに強制削除する

上記のように、rmコマンドでファイルを削除する際、通常、ファイルを本当に削除するのか確認メッセージが表示されますが、この確認メッセージを表示せず、強制にファイルを削除したい際は「-r」オプションを実行します。

以下では「test04.txt」を強制削除した実行例となります。

$ ls -lF
total 16
drwxr-xr-x 2 root root 4096 Jul 17 02:21 test01/
drwxr-xr-x 2 root root 4096 Jul 17 02:21 test02/
-rw-r--r-- 1 root root    0 Jul 18 03:29 test04.txt

rmコマンドを実行します。

$ rm -f test04.txt 

確認メッセージが表示されずファイルが削除されたことが確認できます。

$ ls -lF
total 16
drwxr-xr-x 2 root root 4096 Jul 17 02:21 test01/
drwxr-xr-x 2 root root 4096 Jul 17 02:21 test02/

「rm」コマンドオプション

「rm」コマンドのオプションは以下の通りとなります。

$ rm --help
Usage: rm [OPTION]... [FILE]...
Remove (unlink) the FILE(s).

  -f, --force           ignore nonexistent files and arguments, never prompt
  -i                    prompt before every removal
  -I                    prompt once before removing more than three files, or
                          when removing recursively; less intrusive than -i,
                          while still giving protection against most mistakes
      --interactive[=WHEN]  prompt according to WHEN: never, once (-I), or
                          always (-i); without WHEN, prompt always
      --one-file-system  when removing a hierarchy recursively, skip any
                          directory that is on a file system different from
                          that of the corresponding command line argument
      --no-preserve-root  do not treat '/' specially
      --preserve-root[=all]  do not remove '/' (default);
                              with 'all', reject any command line argument
                              on a separate device from its parent
  -r, -R, --recursive   remove directories and their contents recursively
  -d, --dir             remove empty directories
  -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 might be possible to recover
some of its contents, given sufficient expertise and/or time.  For greater
assurance that the contents are truly unrecoverable, consider using shred.

GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
Report rm translation bugs to <https://translationproject.org/team/>
Full documentation at: <https://www.gnu.org/software/coreutils/rm>
or available locally via: info '(coreutils) rm invocation'

「rm」コマンドの使い方は以上となります。

エンジニアのオンライン学習

エンジニアにおすすめのオンライン教材比較
ITエンジニアが自宅で学習ができるオンラインスクール比較

エンジニアのおすすめ学習「Progate」と「Udemy」比較

VPS_比較
最新情報をチェックしよう!