1

I need to check if a directory has any files inside it, recursively, but don't care if there is empty sub directories.

I can't use the classic

[ "$(ls -A /E/Nuvem/Músicas)" ] && echo "Not Empty" || echo "Empty"

because it considers sub directories too.

I've tried check whole folder size, but each empty folder has 4kb. And I couldn't do anything with find

EDIT: I'm uploading files to the AWS S3 cloud and I don't need to send empty folders, I just keep them to help organize new files.

codeforester
  • 39,467
  • 16
  • 112
  • 140
TNT
  • 819
  • 1
  • 8
  • 28
  • Why not `find`? – Cyrus Aug 26 '20 at 16:34
  • How to use find for this? – TNT Aug 26 '20 at 16:36
  • 1
    Does this answer your question? [Checking from shell script if a directory contains files](https://stackoverflow.com/questions/91368/checking-from-shell-script-if-a-directory-contains-files) – 0stone0 Aug 26 '20 at 16:37
  • `[ -n "$(find /tmp/folder -prune -empty)" ] && echo 'empty' || echo 'not empty'` – 0stone0 Aug 26 '20 at 16:38
  • @0stone0 This question and answers seems not to ignore sub directories. I've tryed your solution, but it shows 'not empty' with a folder with an empty subfolder, where I expect 'empty'. – TNT Aug 26 '20 at 17:00

1 Answers1

2

Check if this would work for you:

[ "$(find /E/Nuvem/Músicas -maxdepth 1 -type f)" ] && echo "Not Empty" || echo "Empty"

EDIT: If you would like to check subfolders for files, just ignore the -maxdepth 1 option:

[ "$(find /E/Nuvem/Músicas -type f)" ] && echo "Not Empty" || echo "Empty"
Rfroes87
  • 668
  • 1
  • 5
  • 15
  • If the folder has a subfolder with a file inside it, the result should be "Not Empty" and this test shows "Empty"... But removing `-maxdepth 1` seems to work. – TNT Aug 26 '20 at 16:54