使用 tar 打包相对于路径的文件
pack files relative to path with tar
在一个脚本中,我有一个相对文件路径的列表,用于打包到一个存档中。如果我使用:
# list of some relative file paths determined at runtime
FILES="../path1/file1 ../path1/path2/file2 ../path1/file3 ... more files ..."
# pack files
tar -cavf target.tar.gz $FILES
存档结构是:
path1/file1
path1/path2/file2
path1/file3
...
但我希望相对于运行时确定的路径打包文件 ../path1
:
file1
path2/file2
file3
...
我不想使用 -C
参数,因为那样我还必须修改路径。
有没有简单的解决办法?
我自己回答。唯一的解决方案似乎是使用 realpath
:
编辑相对路径
realpath --relative-to=../path1 ../path1/file1 ../path1/path2/file2 ../path1/file3 \
| tar -acvf target.tar.gz -C ../path1 -T -
也许您的简化示例问题中缺少一个细节,但仅先移动到该相对文件夹是否就足够了?
cd ../path1
FILES="file1 path2/file2 file3"
tar -cavf target.tar.gz $FILES
在一个脚本中,我有一个相对文件路径的列表,用于打包到一个存档中。如果我使用:
# list of some relative file paths determined at runtime
FILES="../path1/file1 ../path1/path2/file2 ../path1/file3 ... more files ..."
# pack files
tar -cavf target.tar.gz $FILES
存档结构是:
path1/file1
path1/path2/file2
path1/file3
...
但我希望相对于运行时确定的路径打包文件 ../path1
:
file1
path2/file2
file3
...
我不想使用 -C
参数,因为那样我还必须修改路径。
有没有简单的解决办法?
我自己回答。唯一的解决方案似乎是使用 realpath
:
realpath --relative-to=../path1 ../path1/file1 ../path1/path2/file2 ../path1/file3 \
| tar -acvf target.tar.gz -C ../path1 -T -
也许您的简化示例问题中缺少一个细节,但仅先移动到该相对文件夹是否就足够了?
cd ../path1
FILES="file1 path2/file2 file3"
tar -cavf target.tar.gz $FILES