特定のフォルダの下のファイルとフォルダを削除する方法

特定のフォルダの下のファイルとフォルダを削除する方法

私たちは皆、ファイルを削除するためにこのセキュリティオプションを使用できることを知っています。

  find /path/to/directory/ -mindepth 1 -mtime +5 -delete

/path/to/directory/フォルダの下のフォルダとファイルを削除するにはどうすればよいですか?構文は何ですか?

削除オプションは空でないフォルダを削除できません。

 find /var/tmp -type d -mindepth 1 -mtime +5 -delete
 find: warning: you have specified the -mindepth option after a non-option    argument -type, but options are not positional (-mindepth affects tests   specified before it as well as those specified after it).  Please specify  options before other arguments.

 find: cannot delete `/var/tmp/foreman-ssh-cmd-1b987fef-10ca-4204-bf4b-441f28a3db07': Directory not empty
 find: cannot delete `/var/tmp/foreman-ssh-cmd-2687d337-2b60-4f20-b581-a70807c22cb9': Directory not empty
 find: cannot delete `/var/tmp/foreman-ssh-cmd-faedbb3a-7756-4c96-8a40-a4a2001b5fb3': Directory not empty

ベストアンサー1

どのオプションも使用しなかった-typeため、指定された基準(正確にここにある)に一致するすべての項目がファイル、ディレクトリ(空の場合)など、何もfind削除されます()。-delete-mindepth 1 -mtime +5

空のディレクトリのみを削除したい場合:

find /path/to/directory/ -mindepth 1 -type d -mtime +5 -delete

削除するファイルを実際に削除する前に確認するのが最善です。削除-delete:

find /path/to/directory/ -mindepth 1-type d -mtime +5

完全性のためにファイルとディレクトリのみを検索するには、次の場所にOR構造を挿入しますfind

find /path/to/directory/ -mindepth 1 \( -type f -o -type d \) -mtime +5 

空でないディレクトリも削除するには、次のようにrmします-exec

find /path/to/directory/ -mindepth 1 \( -type f -o -type d \) -mtime +5 -exec rm -r {} +

-f必要に応じて追加してくださいrm

おすすめ記事