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 create
  • f 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.

Share:
7,142
gidyn
Author by

gidyn

Updated on September 18, 2022

Comments

  • gidyn
    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 to grep, then splicing it into the tar 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
      Costas over 9 years
      Do you have --ignore-failed-read option?
    • gidyn
      gidyn over 9 years
      Tried it, still gave an error for files which don't exist at all.
    • Costas
      Costas over 9 years
      Yes, error message is present but archive is created.
  • gidyn
    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
    gidyn over 9 years
    I guess that might work if I --strip-components while extracting.