[英]how to use relative path in .sh in order to make my tar files include only the folder tar is created from instead of its fullpath
[英]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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.