tar 不存在的文件列表

gid*_*dyn 11 shell tar wildcards

我正在从脚本运行命令,例如

tar -c -f ar.tar a b c d
Run Code Online (Sandbox Code Playgroud)

其中 b、c 和 d 可能不存在,并且可能是目录。我提出的解决方案是将ls -dto的输出通过管道传输grep,然后将其拼接到tar命令中,或者打开@(a|b|c|d).

有没有更简洁的方法来做到这一点?我在 Debian Wheezy 上,它似乎没有--include参数。

Arc*_*mar 9

你可以试试

 tar cf ar.tar $(ls a b c d )
Run Code Online (Sandbox Code Playgroud)

在哪里

  • c 为创造
  • f ar.tar 指定tar文件

  • $(ls a b c d) 将列出真正存在的文件(并为其他文件提供错误)

  • `tar cf ar.tar $(ls -dabcd 2>/dev/null)` 完美运行.. 但我认为使用 extglob 更干净 (2认同)