[kwlug-disc] tar --directory help please

Ronald Barnes ron at ronaldbarnes.ca
Wed Jun 4 15:36:41 EDT 2025


Chris Irwin wrote on 2025-06-04 11:51:

> tar -cf /tmp/test.tar \
>          --directory /etc/systemd/system/ \
>          mailman3.service \
>          mailman3-web.service

This indeed works, but is useless if I have some overrides - folders 
like mailman3.d (?) with files inside.

Need those wildcards...


> Personally, in this scenario I always found it easier to use pushd/popd 
> in scripts since you're only including files relative to one directory. 
> It's less weird, and easier to read. As a bonus, you can use shell 
> wildcards again.
> 
>      $ pushd /etc/systemd/system
>      $ tar -czf /tmp/test.tar mailm*
>      $ popd

This is a clever solution and bonus for being legible and simple.


> All that said, it looks like in your example, you're trying to preserve 
> the full path (hence --directory=/).  You don't need to use --directory 
> for that. Just provide the full path to the files, that will be 
> preserved in the zip.
> 
>      $ tar -cf /tmp/test.tar /etc/systemd/system/mailm*

This causes the mildly annoying warning:

tar: Removing leading `/' from member names

Which, in cron jobs, generates emails.

Redirecting errors / warnings to /dev/null isn't ideal in such situations.



I now feel like I should make an effort to use pushd and popd on at 
least some occasions.

Bonus is, I'd probably make the typo I just made (and corrected): `poopd`.



More information about the kwlug-disc mailing list