[kwlug-disc] tar --directory help please

John Van Ostrand john at vanostrand.com
Wed Jun 4 14:23:39 EDT 2025


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.
Shell doesn't know it's a parameter and the file glob is after the = sign.
It presumes the entire string is a file glob.

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.
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

or if you could quote it, it would be:

tar
--directory="/etc/systemd/mailman.3.service /etc/systemd/mailman3-web.service"

And it would fail because there's not directory of that (combined) name.

And that second file name would be a separate parameter. I think only one
--directory makes sense so you wouldn't want to tha/etc/t anyway. It's two
commands if you want.

But let's say you actually wanted to expand that and pass it to command.
You could expand it outside like this:

FNAME=/etc/systemd/mailman3* tar --directory="$FNAME" ...

Or you could:

tar --directory=$(echo /etc/systemd/mailman3*)

or even:

tar --directory="$(echo /etc/systemd/mailman3*)"

But again, that will certainly not do what you want it to.

So what do you want to do? You want to backup each directory separately?
Then try this:

tar --directory=/etc/systemd/mailman3.service -cf mailman3.tar
tar --directory=/etc/systemd/mailman3-web.service -cf mailman3-web.tar

of if you don't know what directories exist you'd have to loop

for dir in /etc/systemd/mailman3*; do
  base=$(basename "$dir")
  tar --directory="$dir" -cf "${base}.tar"
done

 Or do you want to backup both in the same Tar file:

tar --directory=/etc/systemd -cf mailman3.tar mailman3*

I've not used --directory before. I had to look it up. I grew up with SCO
and HP-UX so I used:

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

In case you're wondering it means:
  ( - start a subshell
 cd /etc/systemd  - Change to that directory
  && - If successful, then run the following command
  tar - do the tar thing, the -f - means output to STDOUT
  ) end of shell
  > - Redirect output to the file using the current shell (meaning not in
the changed-to direcotry

It seems to me the --directory method is easier.

On Wed, Jun 4, 2025 at 2:01 PM Ronald Barnes via kwlug-disc <
kwlug-disc at kwlug.org> wrote:

> Anton Avramov wrote on 2025-06-04 10:43:
>
> > to pass the wildcard to tar us single quotes like so:
> > 'etc/systemd/system/mailm*'
> >
> > Give it a go let us know
>
> # tar -vvvcf ffs.tar --directory=/ 'etc/systemd/system/mailman*'
> tar: etc/systemd/system/mailman*: Cannot stat: No such file or directory
> tar: Exiting with failure status due to previous errors
>
>
> Single or double quotes are giving me the same results.
>
>
> There's gotta be a way to do this without using `cd /` first!
>
>
> I've even tried "escaping" the wildcard:
>
> # tar -vvvcf ffs.tar --directory=/ 'etc/systemd/system/mailman\*'
> tar: etc/systemd/system/mailman\\*: Cannot stat: No such file or directory
> tar: Exiting with failure status due to previous errors
>
>
>
> (Thanks for the suggestion Anton!)
>
>
> _______________________________________________
> kwlug-disc mailing list
> To unsubscribe, send an email to kwlug-disc-leave at kwlug.org
> with the subject "unsubscribe", or email
> kwlug-disc-owner at kwlug.org to contact a human being.
>


-- 
John Van Ostrand
At large on sabbatical
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.kwlug.org/pipermail/kwlug-disc_kwlug.org/attachments/20250604/7cbf1c34/attachment.htm>


More information about the kwlug-disc mailing list