-1

The following command will remove all files and folders in the current directory, except for those mentioned.

find . -mindepth 1 ! -path '*testResults*' ! -path '*artifacts*' ! -path '*node_modules*' -exec rm -r {} + 2>/dev/null

My question is: how to also exclude the .git hidden folder from getting deleted?

Cyrus
  • 84,225
  • 14
  • 89
  • 153
Idan Adar
  • 44,156
  • 13
  • 50
  • 89
  • I think you would need to use `-path` and `-prune`... see if https://unix.stackexchange.com/questions/97558/find-combining-path-and-prune-to-exclude-files-and-directories helps – Sundeep Apr 16 '17 at 06:27

1 Answers1

0

Use ! -name '.git':

find . -mindepth 1 ! -path '*testResults*' ... ! -name '.git' ...

The fact that you use ! -path but don't know about ! -name shows that you just pick some random stuff from the internet instead of looking at the right place of information: The man page! So just go ahead and type:

man find
hek2mgl
  • 152,036
  • 28
  • 249
  • 266
  • The fact that you assume stuff is cute. I did read parts of https://ss64.com/bash/find.html but it was just too exhaustive and I missed stuff. Horray for people helping out? – Idan Adar Apr 16 '17 at 06:32
  • Looks like my assumption was correct. And no, the man page of the find command is *not* too exhaustive. `find` just has more than 3 options. On the other hand I do hope my assumption that you downvoted the answer I gave you is wrong because this is something that is personally so poor that I haven't seen it in 6 years stackoverflow. – hek2mgl Apr 16 '17 at 06:47
  • Yes, I downvoted your answer because albeit it being correct, your attitude reeks. Grow some manners. – Idan Adar Apr 16 '17 at 06:55
  • What attitude? The attitude to point you to the `find` man page? Man! As a dev-ops, which you claim to be on your profile, you'll need to look often at the man page of the find command since it has many options and you won't be able to keep them all in in mind. While `-name` is something I know out of my head, I look at the man page at least once a week. Actually it is pretty simple structured: `Options, Expressions, Filters, Actions` where one often needs to lookup something from `Filters and Actions`. Short to remember: Being able to read the `find` man page is *essential*. – hek2mgl Apr 16 '17 at 07:05