ディレクトリを含むすべてがLinuxのファイルです。ディレクトリは単なるファイルのグループです。
Linux でファイルとディレクトリを削除するコマンドは主に 2 つあります。
rm
rmdir
空のディレクトリを削除する
rmdir
コマンドは、Linux で空のディレクトリを削除するために使用されます。
たとえば、次のコードは、内部にファイルがない「images」ディレクトリを削除します:
$ rmdir images/
rm
も使用できます -d
を使用したコマンド 空のディレクトリを削除するオプション:
$ rm -d images/
注:ディレクトリ内にファイルがある場合、rmdir は使用できません ディレクトリを削除します。 空でないディレクトリで上記のコマンドを試すと、次のようになります:
$ rmdir images/
rmdir: images/: Directory not empty
ディレクトリとその内容を削除する
すべてのコンテンツを含むディレクトリを再帰的に削除するには、rm
を使用します 引数 -r
を持つコマンド .
$ rm -r images/
-rf
を使用して、ディレクトリとそのすべての内容を強制的に削除することもできます
$ rm -rf images/
ファイルを削除
Linux でファイルを削除するには、単純に rm
を使用します コマンド:
$ rm cat.gif
ファイルを強制的に削除する
ファイルを強制的に削除するには、-f
を使用します rm
のオプション コマンド:
$ rm -f cat.gif
ファイルまたはディレクトリを削除する前にプロンプト
ファイルまたはディレクトリを削除する前に確認を求めたい場合は、-i
を使用します rm
のオプション コマンド:
$ rm -i cat.gif
remove cat.gif? y
削除時に冗長にする
削除されたファイルの出力を表示するには、-v
を使用します オプション:
$ rm -v cat.gif
cat.gif
複数のファイルを削除
1 回の操作で複数のファイルを削除するには、*
を使用します
たとえば、次のコードは .gif
のすべての画像を削除します 拡張子:
ls images/
bird.png cat.gif dog.gif
rm *.gif
ls images/
bird.png
完全な rm 使用法
rm 構文
rm [-dfiPRrvW] file ...
以下の表は、rm
の使用法を示しています。 コマンドとそのすべてのオプション。
+--------+---------------------------------------------------------------------------------------------------------------------+-----+-----+
| Option | Description | | |
+--------+---------------------------------------------------------------------------------------------------------------------+-----+-----+
| -d | Attempt to remove directories as well as other types of files. | | |
| -f | Attempt to remove the files without prompting for confirmation, regardless of the file's permissions. | | |
| -i | Request confirmation before attempting to remove each file, regardless of the file's permissions | | |
| -P | Overwrite regular files before deleting them. | | |
| -R | Attempt to remove the file hierarchy rooted in each file argument. | | |
| -r | Same as -R | | |
| -v | Be verbose when deleting files, showing them as they are removed. | | |
| -W | Attempt to undelete the named files. Currently, this option can only be used to recover files covered by whiteouts. | | |
+--------+---------------------------------------------------------------------------------------------------------------------+-----+-----+