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

  • 作成日 2022.10.06
  • mac
mac コマンドで空のディレクトリだけを削除する

macで、コマンドを実行して、空のディレクトリだけを削除する手順を記述してます。

環境

  • ProductName: macOS
  • ProductVersion: 11.5.2
  • BuildVersion: 20G95

空のディレクトリのみを削除

空のディレクトリのみを削除するには、「rmdir」コマンドを使用します。

以下の構成のディレクトリ「d1」で「rmdir」コマンドを実行してみます。

tree

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

3 directories, 1 file

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

rmdir d1

<出力結果>
rmdir: d1: Directory not empty

空のディレクトリではないので削除されません。

次に「d3」を削除してみます。

rmdir d3

「d3」 は、空のディレクトリなので削除されます。

次にディレクトリでない「sample.txt」で「rmdir」コマンドを実行してみます。

rmdir test.txt

<出力結果>
rmdir: test.txt: Not a directory

ファイルなので削除されません。