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.

8
  • I've tried a number of things. How many do I need to document? How thoroughly do I need to document them? Is it enough to list the terminal commands I tried? Also the google searches, the web pages, and then man pages I read? Do you need the narrative of how I progressed from one attempt to the other? What was my thought process? Not trying to be difficult, I'm just someone who when I start explaining something can be overly thorough and am worried it will take me longer to document them than it took to write the question! Commented Mar 16, 2023 at 20:08
  • I rewrote the post. I apologize for the delay. I'm trying guys, even if I'm not the fastest ... Commented Mar 17, 2023 at 18:24
  • I think our fellow users were trying to get some commands from you, i.e. exact commands you have tried and they failed to do what you want. This was the research effort we were expecting. You did not post any such command, but you did not give up either. I would gladly see the commands, but not at the cost of bullying you, therefore I wrote an answer. But frankly "I figured out how to use the Terminal to list all files …" should be followed by the command you used for this. If the answer I posted solves your problem then please accept it. Commented Mar 17, 2023 at 18:58
  • When I first wrote the question I explained the whole problem. When I wrote my first comment I thought the list of commands I had tried would show my attempts to solve this. However, when I started to update the question, I realized I had already solved how to exclude the .DS_Store files (incorrectly it turned out), so really I was only asking how to exclude directories. I thought the commands I tried were not related to that, so I omitted them. This put me in a bind though as far as how to show that I had put in effort, and the best I could come up with at the time was the pages I had read. Commented Mar 19, 2023 at 6:54
  • Sample 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 Commented Mar 19, 2023 at 7:25

1 Answer 1

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

You must log in to answer this question.

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