How can I exclude a directory in find and rsync but include specific files?

April 22, 2025 2 answers
Asked by staple_removal
I'm trying to figure out how to use find and rsync to manage my dotfiles. Specifically, I want to exclude the .mozilla folder and a few other directories, but I still need to keep certain files related to Firefox, like login data. Right now, I've been using a command like: find -path '*/.*' -not -path '*/.cache/*' -not -path '*/.mozilla/*' -path '*/.mozilla/firefox/*.default-release/{autofill-profiles,signedInUser,prefs}.js*' > dotfiles However, this command gives me an empty output. I need some guidance on how to exclude most things in a directory while still including specific files. I also want to know how to apply this for rsync (and possibly tar) later on.

Answers

snowshoe_binding_
For rsync, if you have a set of file names that you know won't change, you can list them in a text file and use the --files-from=foo.txt option. Just keep in mind that using this method won't maintain recursion, so you might need to run another rsync call for additional depth if that's important.
Reply to this answer
umami_4
The way -path works is more like shell globbing and it doesn't handle brace expansions. Consider breaking your task into two partsโ€”it simplifies things. For example, you could use commands like: find . ( -path ./.cache -o -path ./.mozilla ) -prune -o -type f -name '.*' -print And separately target .mozilla/firefox/*.default to find the specific files like prefs.js or autofill-profiles.js. It makes it cleaner and more manageable.

Trellis: Exactly! Splitting your approach is much clearer, and it really helps in the long run.

Reply to this answer

Add your answer

Answers are reviewed before publication.

Related Discussions