[kwlug-disc] tar --directory help please

Ronald Barnes ron at ronaldbarnes.ca
Wed Jun 4 15:29:40 EDT 2025


John Van Ostrand wrote on 2025-06-04 11:23:

> The wildcard isn't being expanded by the shell because it's attached to 
> the --directory=. The shell tries to expand --directory=/etc/systemd/ 
> mailman* and there's nothing that begins with --directory= in the 
> current directory.

I don't understand how --directory=/ gets expanded to 
--directory=/etc/systemd/mailman*?

It should try to expand etc/systemd/system/mailman* once it's switched 
to /, and the expansion should result in two files matching.

Clearly this isn't what's happening (making the --directory almost 
entirely useless except for the scenario Chris outlined).


> You can experiment with this using echo --directory=/etc/system/mailman* 
> and see it's not expanding it. However echo /etc/systemd/mailman* would 
> expand.

# echo /etc/systemd/mailman*
/etc/systemd/mailman*

No expansion.


> Now keep this in mind. If it does expand to more than one filename it 
> will break the tar command. It would expand to something like
> 
> tar --directory=/etc/systemd/mailman3.service /etc/systemd/ 
> mailman3.web.service

I don't think it works that way, where --directory becomes what I 
specified plus the final file name(s).

The echo result seems to agree.


For example, it does not turn this into 
--directory=/etc/systemd/system/mailman3.service:

# tar --directory=/ -vcf ffs.tar etc/systemd/system/mailman3.service 
etc/systemd/system/mailman3-web.service

etc/systemd/system/mailman3.service
etc/systemd/system/mailman3-web.service



> FNAME=/etc/systemd/mailman3* tar --directory="$FNAME" ...
> 
> Or you could:
> 
> tar --directory=$(echo /etc/systemd/mailman3*)
> 
> or even:
> 
> tar --directory="$(echo /etc/systemd/mailman3*)"

Those should work, but so ugly.





>   Or do you want to backup both in the same Tar file:
> 
> tar --directory=/etc/systemd -cf mailman3.tar mailman3*

That's close to what I want! However...


# tar --directory=/etc/systemd/system -vcf /tmp/ffs.tar mailman3*
tar: mailman3*: Cannot stat: No such file or directory
tar: Exiting with failure status due to previous errors






> (cd /etc/systemd && tar -cvf - mailman3*) > mailman3.tar

That's kind of what I'd been using, and the best option so far:

(cd / ; tar -cvf /tmp/test.tar etc/systemd/system/mailman3*)

Archives the files & folders that I want, no warnings about "stripping 
leading '/'", captures paths, and leaves me back where I started.



Today was the day I was going to figure out a better way™, with 
--directory= since that's ostensibly what it's for.

Today is also the day I learned there isn't a better way.



More information about the kwlug-disc mailing list