Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exclude only specific folder in tar command

Tags:

shell

archive

tar

I want to tar a directory that looks like this:

dir
└── workspace
└── node_modules
└── subfolder
    └── workspace
    └── node_modules
    └── other_folder

I want to exclude all folders named node_modules and exclude the top level folder called workspace, but no sub folders called workspace. So what I want to end up with is this:

dir
└── subfolder
    └── workspace
    └── other_folder

I'm running this command: tar -czf ./output.tar.gz --exclude=node_modules --exclude=./workspace dir/.

But it's removing all folders called workspace and node_modules, so I instead end up with this:

dir
└── subfolder
    └── other_folder

How do I remove only the specific workspace folder that I want, and not all folders with the same name?

like image 400
stinaq Avatar asked Oct 20 '25 09:10

stinaq


1 Answers

For the required case, possible to use tar excludes:

  • --exclude dir/./folder -- apply to folder directly under dir
  • --exclude folder -- will exclude folder anywhere in the tree

Should be possible to use:

tar -czf ./output.tar.gz --exclude=node_modules --exclude=dir/./workspace dir/.

Of course possible to use --files-from, and to generate the list using another tool. This is usually preferred when the list could be large number of files, vs using xargs.

find dir/. -type f ... | tar cvz ./output.tar.gz -T-
like image 65
dash-o Avatar answered Oct 22 '25 05:10

dash-o



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!