Tar a list of files which don't all exist
7,142
Solution 1
you can try
tar cf ar.tar $(ls a b c d )
where
c
for createf ar.tar
specify tar file$(ls a b c d)
will list to stdin which file are realy present (and give error for other)
Solution 2
If it is ok to have the full path of the files in the tar archive you can do:
tar -c -f ar.tar $(readlink -e a b c d)
The -e
option to readlink will canonicalize existing filenames and silently ignore any others.
Author by
gidyn
Updated on September 18, 2022Comments
-
gidyn over 1 year
I'm running a command from a script like
tar -c -f ar.tar a b c d
where b, c, and d may not exist, and may be directories. The solutions that I've come up with are piping the output of
ls -d
togrep
, then splicing it into thetar
command, or turning on extended globs for@(a|b|c|d)
.Is there a neater way of doing this? I'm on Debian Wheezy, which doesn't seem to have an
--include
parameter.-
Costas over 9 yearsDo you have
--ignore-failed-read
option? -
gidyn over 9 yearsTried it, still gave an error for files which don't exist at all.
-
Costas over 9 yearsYes, error message is present but archive is created.
-
-
gidyn over 9 years
tar cf ar.tar $(ls -d a b c d 2>/dev/null)
works perfectly .. but I think it's cleaner with extglob -
gidyn over 9 yearsI guess that might work if I
--strip-components
while extracting.