I am trying to index some LTO tapes (and some hard disks as well) and would like a simple output, stored in a text file. Something like the example below:

/home/norg/Desktop/empty\ dir/

The "empty dir" folder is, of course, an empty folder. Notice how folders like "Desktop" and "Documents" are only printed because they have contents inside them. In other words, /home/norg/Desktop/ would never be printed, unless it were empty.

I've tried things like ls -RFad /home/norg/ and some find syntaxes to varying success. The trickiest part is having the empty directories printed, but not the full ones. I hope this makes sense. Thanks.

Another thing to mention, I would really prefer this to work in one go, as I will be using this with tape and want to have as little reads as necessary to reduce wear.

  • In the example, does \ belong to the actual filename? or is it there just to escape the space? If the latter, is escaping a must? What other characters should be escaped? Will you accept directories without trailing slashes? Directory is also a file, so "print only files and empty directories" is equivalent to "print files". Do you mean "regular files"? or "non-directories"? (these terms are not equivalent). Please edit and clarify. Aug 28, 2020 at 22:48
  • 1
    question is not clear enough
    – Akuseru
    Aug 29, 2020 at 0:55
  • Read man find, an empty. directory is \( -type d -links 2 \)
    – waltinator
    Aug 29, 2020 at 1:37
  • 1
    @waltinator your claim only applies to some filesystem types,do it is not helpful.
    – schily
    Aug 29, 2020 at 7:33
  • Does this answer your question? Find empty directoris OR directories with no files in them recursively Aug 29, 2020 at 9:36

1 Answer 1

find /home/norg \( -type d -empty \) -o -type f

This will list empty directories and regular files which are below /home/norg.

Although the question title mentions "directories containing files", those are not printed as an item, per the ¡question text.

Also note that it only prints regular files. If you have e.g. fifos or devices, those would not appear (and if you had a folder only containing a device, it would not be printed, either). In case you wanted to also include them, you may use

find  \( -type d -empty \) -o -not -type d

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .