0

I'm trying to do a recursive search in Terminal to find all files that are not folders; this is to confirm that a large directory structure has no content in it. Is there a way to do this?

Starting with another SuperUser post on finding files with a specific extension and reading the man page for "find", I figured out how to use the Terminal to list all files that not named .DS_store. This is still a long list though. I think it is only directory paths, but it is too many to go through manually. I have been looking for a way to use FIND or grep to exclude folders/paths from the output without luck, but I feel like there must be a way.

So far I've tried this commands. Using prune:

find "$PWD" -name ".ds" -prune -o -name "*.*" | xargs -0 ls -laht -P 1 find "$PWD" -name ".ds" -prune -o | xargs -0 ls -laht -P 1 find "$PWD" -name ".DS_Store" -prune -o | xargs -0 ls -laht -P 1\n find "$PWD" -name ".DS_Store" -prune -o -name "*.*" | xargs -0 ls -laht -P 1\n

Embarrassing to realize the latter gave me my answer.

Using name/iname:

find -name ds find -iname ds find . -iname ds find . -iname "ds" find . -iname ".ds" find . -iname "\.ds" find . -iname "Store" find . -iname ".DS_Store".

All had print.

Finally I got frustrated with find and went to grep:

find . -print | grep -v ds find . -print | grep -v DS

At this point I thought I was set on excluding .DS_Store files. I didn't realize that I would be excluding any files w/ "DS" in the name.

I also have read the man page, and have been googling for things like, [recursive search macos exclude all folders terminal]. That's how I found the post I reference above, as well as https://www.crybit.com/exclude-directories/

but they all end up being about excluding specific directories, not directories as a category.

Giacomo1968
  • 58,727

1 Answers1

1

In find you find directories using -type d. To find non-directories you need to negate this test: ! -type d.

To find files named .DS_store you use -name .DS_store. To find files not named .DS_store you need to negate this test: ! -name .DS_store.

The following command applies the two negated tests. They are connected by an implicit -a (conjunction, the AND operator):

find . ! -type d ! -name .DS_store

The implicit action is -print. Pathnames of files that pass both tests will be printed.

For comparison, the same command with -a and -print explicitly used:

find . ! -type d -a ! -name .DS_store -print