mac コマンドでディレクトリを削除する

  • 作成日 2023.12.04
  • mac
mac コマンドでディレクトリを削除する

macで、コマンドを実行して、ディレクトリを削除する手順を記述してます。「rm」コマンドでオプション「-r」を使用します。オプション「-i」を使用すると削除するディレクトリを1つずつ選択して削除することができます。

環境

  • OS macOS Monterey
  • SHELL /bin/zsh

ディレクトリを削除

ディレクトリを削除するには、「rm」コマンドのオプション「-r, -R」を使用します。

以下の構成のディレクトリ「d1」を削除してみます。

% tree

<出力結果>
.
|-- d1
|   |-- d2
|   |   `-- d3
|   `-- d4
`-- test.txt

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

% rm -r d1

実行結果を確認します。

% tree

<出力結果>
.
`-- test.txt

ディレクトリ「d1」が再帰的に削除されていることが確認できます。
※オプション「-R」でも結果は同じです。

また、オプション「-i」を使用とすると削除するディレクトリを1つずつ指定できます。

% rm -ri d1

<出力結果>
examine files in directory d1? y
examine files in directory d1/d2? y
examine files in directory d1/d2/d3? y
remove d1/d2/d3? y
remove d1/d2? y
remove d1? y