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
  • 53,069
  • 19
  • 162
  • 212
  • 2
    Can you post your attempts at doing this? What have you done so far to achieve this goal? This site is not a free scripting service. The community here helps those who help themselves. So if you have attempted to do something to achieve this goal, please post your code/script/formula and the community can attempt to help you. As it stands this question reads as a free scripting request and will most likely be closed as a result. – Giacomo1968 Mar 16 '23 at 19:34
  • 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! – Jordan Elpern-Waxman Mar 16 '23 at 20:08
  • 1
    “Is it enough to list the terminal commands I tried?” Just show the actual terminal commands and you should be fine. Nobody needs to know you searched Google (you are not the first to use Google). Just show some basic effort and the terminal commands should be fine. – Giacomo1968 Mar 16 '23 at 20:27
  • Voting to close because despite leaving a comment very quickly responding to the request for more details, the original poster has not added any new details. Please add more details, @JordanElpern-Waxman. As I said in a previous comment, just show the actual terminal commands and you should be fine. Nobody needs to know you searched Google (you are not the first to use Google). Just show some basic effort and the terminal commands should be fine. – Giacomo1968 Mar 17 '23 at 15:02
  • I rewrote the post. I apologize for the delay. I'm trying guys, even if I'm not the fastest ... – Jordan Elpern-Waxman Mar 17 '23 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](https://superuser.com/help/accepted-answer). – Kamil Maciorowski Mar 17 '23 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. – Jordan Elpern-Waxman Mar 19 '23 at 06: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` – Jordan Elpern-Waxman Mar 19 '23 at 07:25
  • 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. – Jordan Elpern-Waxman Mar 19 '23 at 07:32
  • @KamilMaciorowski I don't think the three comments with my commands are that helpful, definitely not as is, definitely not for future people as @ SeñorCMasMas puts it, but I'm also not sure what would be helpful (would any of it be helpful in the original question?). I wrote them with the goal of getting feedback from you and others on what would be helpful. Then I'll delete everything besides what is helpful. – Jordan Elpern-Waxman Mar 19 '23 at 07:35
  • @SeñorCMasMas sorry if I came across as defensive. I'm re-reading my first comment and in retrospect I can see that it reads a bit prickly. I appreciate your welcoming message and of course all of the free expert help! There are some questions I'm still figuring out about the site culture; where would the best place be to ask them? – Jordan Elpern-Waxman Mar 19 '23 at 07:48
  • @JordanElpern-Waxman I just edited your question to add all of the coding examples you posted as comments because when I posted this as a comment: “Just show the actual terminal commands and you should be fine. Nobody needs to know you searched Google (you are not the first to use Google). Just show some basic effort and the terminal commands should be fine.” Actually terminal commands is useful. This is not useful, “I also have read the man page, and have been googling for things…” Sorry, but can you explain how you feel saying that is useful? Nobody wants to know you Googled things? – Giacomo1968 Mar 19 '23 at 14:01
  • @Giacomo1968 I get your point. The fact of having used Google is not useful. I do think sharing which search terms you used & which result pages you read could be, particularly if you explain why each did not solve the problem. Generally speaking I *do* think trying to find information that could contain the answer is an attempt to solve a problem, albeit of a different type than trial and error at the command line. For someone who codes but doesn't know terminal scripting, looking for documentation or examples might be a better start than total stabs in the dark in the terminal. – Jordan Elpern-Waxman Apr 25 '23 at 14:55

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
Kamil Maciorowski
  • 69,815
  • 22
  • 136
  • 202