简体   繁体   中英

pack files relative to path with tar

In a script I have a list of relative file paths for packing into an archive. If I use:

# 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

the archive structure is:

path1/file1
path1/path2/file2
path1/file3
...

But I want the files packed relative to a at runtime determined path ../path1 :

file1
path2/file2
file3
...

I do not want to use the -C argument because then I also have to modify the paths. Is there a simple solution?

I answer myself. The only solution seems to edit the relative paths with realpath :

realpath --relative-to=../path1 ../path1/file1 ../path1/path2/file2 ../path1/file3 \
  | tar -acvf target.tar.gz -C ../path1  -T -

Maybe there's a detail missing from your simplified example question, but wouldn't it suffice to just move to that relative folder first?

cd ../path1
FILES="file1 path2/file2 file3"
tar -cavf target.tar.gz $FILES

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM