rm -rとrm -fの違いは何ですか?


0

マニュアルから:

  • -f、-force

    存在しないファイルを無視し、プロンプトを表示しない

  • -r、-R、-再帰

    ディレクトリの内容を再帰的に削除します

このオプションの説明は異なりますが、空のフォルダー(この例ではrmdirなし)を削除しようとすると同じ結果になります。

-fと比較してエラーや何かを出力しません-rこれは唯一の違いですか、あるオプションが別のオプションよりも優れている特定のタイプの状況があるか、このオプションの1つが他のオプションが単に機能しない状況ですか?


どのシステムを使用していますか?
テクラフ

2
空のディレクトリで、削除するものがないため、同じ結果得られます。ファイルとディレクトリのツリーで試してください。異なる結果が得られます。
木梅

同じ結果が得られるとはどういう意味ですか?rm -r emptydirそのディレクトリを削除しますが、削除しrm -f emptydirません。これらは2つの完全に異なるコマンドラインオプションであり、それぞれドキュメントに記載されているとおりに実行します。
エグモント

回答:


2

これは、CentOSのmanページの説明です。

-f, --force
    ignore nonexistent files, never prompt

-r, -R, --recursive
    remove directories and their contents recursively

私が収集したものから(以下のいくつかのコメントのおかげで)、以下はフラグ-r-fフラグに当てはまります:

-r

  • 隠しファイルやサブディレクトリを含むディレクトリのコンテンツを再帰的に削除します
  • 構成によっては、許可を要求する場合があります(たとえば、--interactiveフラグを使用する場合)。一部のディストリビューションはデフォルトでこれを行います。
  • ディレクトリを削除するために使用することができ、あなたがそうしたい場合は、単純に(例えば:それをディレクトリのパスを与えます/path/to/directory

-f

  • ディレクトリのコンテンツを再帰的に削除するのではなく、指定されたパス(example/file1またはなどexample/*)に直接一致するファイルのみを削除します。
  • サブディレクトリを削除しない
  • 基本的yes to allにWindows で許可を求めない

以下にいくつかの例を示しますが、それらはすべて次の構造から始まります。

example/
  file1
  file2
  file3
  .file
  dir/
    file1
    file2
    file3
    .file

これらの例では、デフォルトで詳細モードと対話モードを有効にしました。一部のディストリビューションはこれを行いますが、他のディストリビューションは行いません。

rmの例

$ rm example
rm: cannot remove `example': Is a directory

ご覧のとおりrm、デフォルトではディレクトリを削除しません。

rmの例-f

$ rm example -f
rm: cannot remove `example': Is a directory

-fフラグを使用しても、ディレクトリを削除することはできません。

rmの例-r

$ rm example -r
rm: descend into directory `example'? yes
rm: remove regular empty file `example/file3'? yes
  removed `example/file3'
rm: remove regular empty file `example/file2'? yes
  removed `example/file2'
rm: descend into directory `example/dir'? yes
rm: remove regular empty file `example/dir/.file'? yes
  removed `example/dir/.file'
rm: remove regular empty file `example/dir/file3'? yes
  removed `example/dir/file3'
rm: remove regular empty file `example/dir/file2'? yes
  removed `example/dir/file2'
rm: remove regular empty file `example/dir/file1'? yes
  removed `example/dir/file1'
rm: remove directory `example/dir'? yes
  removed directory: `example/dir'
rm: remove regular empty file `example/file1'? yes
  removed `example/file1'
rm: remove directory `example'? yes
  removed directory: `example'

ご覧のとおり、すべてのファイルとディレクトリの許可を求められますが、隠しファイルも削除されます。

rmの例/ * -f

$ rm example/* -f
rm: cannot remove `example/dir': Is a directory
removed `example/file1'
removed `example/file2'
removed `example/file3'

ここでは、許可を求められることはなく、ディレクトリは削除されず、隠しファイルでもありません。

rmの例/ * -r

$ rm example/* -r
rm: descend into directory `example/dir'? yes
rm: remove regular empty file `example/dir/.file'? yes
  removed `example/dir/.file'
rm: remove regular empty file `example/dir/file3'? yes
  removed `example/dir/file3'
rm: remove regular empty file `example/dir/file2'? yes
  removed `example/dir/file2'
rm: remove regular empty file `example/dir/file1'? yes
  removed `example/dir/file1'
rm: remove directory `example/dir'? yes
  removed directory: `example/dir'
rm: remove regular empty file `example/.file'? yes
  removed `example/file'
rm: remove regular empty file `example/file1'? yes
  removed `example/file1'
rm: remove regular empty file `example/file2'? yes
  removed `example/file2'
rm: remove regular empty file `example/file3'? yes
  removed `example/file3'

ここで、隠しファイルを含め、サンプルディレクトリの内容(ディレクトリ自体ではない)が削除されます。


アスタリスクは、無制限の再帰を意味しません。それはサブディレクトリ(例と一致した場合example/subdir)、それはそれ以上下降しないだろう、とrmがしますまだそれらのディレクトリを無視します。(一部のシェル**には再帰があります。)
荒廃

ええ、私はそれを知りませんでした。答えを更新します。
cascer1

また、*通常は(私はそれを無視するように意図されたとし、「ドット」ファイルと一致しない...、それは無視してしまったすべてので、隠しファイル)をrm -r dir/*必ずしもディレクトリを空にしないことがありますのみ。dir/.gitたとえばスキップします。
grawity

おかげで、との違いを適切に表示するように答えを更新した-r-f思います。
cascer1

みんなの入力に感謝します(他のリプレイを含む)!すべての回答を組み合わせることで、これをよりよく理解することができました。誰かが私のOSを尋ねました-私はUbuntu / Linuxを使用しています。
ケルベロス

0

rm -r mydirmydirディレクトリとそのすべてのコンテンツが削除されます。

rm -f mydirディレクトリを削除しません(空でもコンテンツでもない)。エラーを報告します:

  • BSD / OS Xの場合:

    rm: mydir/: is a directory
    
  • GNU / Linuxの場合:

    rm: cannot remove 'mydir': Is a directory
    

rm指定された引数に関係なく動作するコマンドの可能な説明(可能性の高いものから低いものへ):

  1. rm定義されたシェルエイリアスがあり、定義済みのパラメーター(など-r)をrmコマンドに渡します
  2. rmまた、実際のコマンドに追加のパラメータを渡すというスクリプトを呼び出しています
  3. カスタムrm実行可能ファイルがあります

を実行すると、最初の2つの可能性を確認でき/bin/rm -f mydirます。

弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.