Reputation: 135
I want to create a tar file of a list of files. The list of files should be read from a separate file named input.txt.
This is the content of the input.txt -
The compressed file should have a directory structure -
/var/opt/ if exists in input file path, it should be replaced with /logs.
This is the expected output when I extract the tar file in say root directory.
Upvotes: 1
Views: 2825
Reputation: 135
This has worked for me - tar -czf myarchive.tar -T /var/opt/input.txt --transform='s|^var/opt/|logs|'
Upvotes: 1
Reputation: 23764
Use the transform option:
tar cf archive.tar --transform 's%^var/opt%logs%' /var/opt
Upvotes: 4
Reputation: 9845
The script below demonstrates a solution based on these questions and answers:
The commands are tested with GNU tar
only.
#! /bin/sh
set -x
# prepare environment
mkdir -p foo/bar
rm -f input.txt
rm -f output.tar
for i in 1 2 3
do
echo $i > foo/bar/file$i
echo foo/bar/file$i >> input.txt
done
# create archive with file name transformation
tar cvf output.tar --transform='s|^foo/bar|baz|' -T input.txt
# list archive contents
tar tvf output.tar
output
+ mkdir -p foo/bar
+ rm -f input.txt
+ rm -f output.tar
+ for i in 1 2 3
+ echo 1
+ echo foo/bar/file1
+ for i in 1 2 3
+ echo 2
+ echo foo/bar/file2
+ for i in 1 2 3
+ echo 3
+ echo foo/bar/file3
+ tar cvf output.tar '--transform=s|^foo/bar|baz|' -T input.txt
foo/bar/file1
foo/bar/file2
foo/bar/file3
+ tar tvf output.tar
-rw-r--r-- bmeissner/1049089 2 2021-07-05 16:11 baz/file1
-rw-r--r-- bmeissner/1049089 2 2021-07-05 16:11 baz/file2
-rw-r--r-- bmeissner/1049089 2 2021-07-05 16:11 baz/file3
Upvotes: 0