簡體   English   中英

使用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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM