0

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 -

  • /var/opt/SM/logs/rq/log.txt
  • /var/opt/DC/mq/mqlog.txt
  • /var/opt/config.cg

The compressed file should have a directory structure -

  • /logs/SM/logs/rq/log.txt
  • /logs/DC/mq/mqlog.txt
  • /logs/config.cg

/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.

  • root/logs/SM/logs/rq/log.txt
  • root/logs/DC/mq/mqlog.txt
  • root/logs/config.cg
p zankat
  • 75
  • 2
  • 14
  • For using an input file to specify the files to archive see https://stackoverflow.com/a/8033898/10622916 for file name transformation see https://unix.stackexchange.com/q/421766/330217 – Bodo Jul 05 '21 at 13:20
  • What mentioned at unix.stackexchange.com/q/421766/330217 is at the time of extraction. I need it at the time of compression. – p zankat Jul 05 '21 at 13:52
  • Did you try it? I guess not. – Bodo Jul 05 '21 at 14:06
  • You likely do not want to replace the absolute path "/var/opt" with the absolute path "/logs". Instead you want to replace the absolute path "/var/opt" with the relative path "logs". – ceving Jul 05 '21 at 14:18
  • When it extracts to some directory say dir, it should extract as /logs/SM/logs/rq/log.txt and not as /var/opt/SM/logs/rq/log.txt. – p zankat Jul 05 '21 at 14:24
  • Does this answer your question? [how to rename files you put into a tar archive using linux 'tar'](https://stackoverflow.com/questions/21790843/how-to-rename-files-you-put-into-a-tar-archive-using-linux-tar) – ceving Jul 05 '21 at 14:28
  • @pzankat Almost no Linux system has the directory "/logs". Why do you want the logs in your root directory? – ceving Jul 05 '21 at 14:33
  • This is going to be implementation-specific, since there is no standard for `tar`. Are you using GNU `tar` (which I assume from the `linux` tag)? – chepner Jul 05 '21 at 14:34
  • Yes, I am using GNU tar. – p zankat Jul 05 '21 at 14:42
  • This worked for me - tar -czf myarchive.tar -T /var/opt/input.txt --transform='s|^var/opt/|logs|' – p zankat Jul 05 '21 at 15:14

3 Answers3

3

Use the transform option:

tar cf archive.tar --transform 's%^var/opt%logs%' /var/opt
ceving
  • 21,900
  • 13
  • 104
  • 178
0

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
Bodo
  • 9,287
  • 1
  • 13
  • 29
0

This has worked for me - tar -czf myarchive.tar -T /var/opt/input.txt --transform='s|^var/opt/|logs|'

p zankat
  • 75
  • 2
  • 14