10

I know it is possible to exclude a particular folder by a command like this:

tar --exclude='/srv/www/project/node_modules' -zcvf /backup/project.tgz .

My question how to exclude any folder named node_modules anywhere within the entire /srv/www directory, to exclude it and exclude all folders under it?

W.M.
  • 203
  • 2
  • 7
  • If what you mean is you want to exclude all directories that start with `node_modules` in their names like `node_modules123` and `node_modules_new` etc. then append a `*` to the excluded directory name and use the command like so `tar -zcvf /backup/project.tar.gz --exclude "/srv/www/project/node_modules*" /srv/www/` and please [edit] your question and make it clear. Thank you – Raffa Oct 26 '19 at 19:33
  • @Raffa, I have just re-edited my question and made it clearer. – W.M. Oct 26 '19 at 19:40
  • This isn't an answer but an FYI. When using the `--exclude` option, the syntax of the filename must match the path being tared. In your example, the **exclude** option should be `--exclude=./srv/www/project/node_modules` in order for the match to occur and the directory to be excluded – PatS Sep 28 '22 at 18:31

1 Answers1

15

To exclude the directory named node_modules wherever it is located under /srv/www/ even if there are multiple copies of it under different sub-directories, just do not specify a path in the --exclude part and use it like this:

tar -zcvf /backup/project.tar.gz --exclude "node_modules" /srv/www/

This will exclude all directories and files named exactly node_modules and all sub-directories and files under them anywhere in /srv/www/.

Raffa
  • 24,905
  • 3
  • 35
  • 79
  • When using `zip`: https://askubuntu.com/questions/1410521/how-to-zip-a-lot-of-folders-but-exclude-any-subfolder-recursively-called-node#comment2530636_1410530 – Ryan Jan 09 '23 at 22:06