1

I tried to tar specific files for example from: /home/user/Desktop/source/file.txt to /home/user/bin/tar_dir/files.tar without having folders path in tar file so after unpacking I'd have only files next to files.tar.

I found a few questions like that however presented solutions worked for me only if I have specific folder and tar all the files inside this folder. The solutions that worked for files were working only if I am in the folder with the files to tar. I wanted to run the command without going to source folder using command cd etc. I'm also using tar --append to add files to existing tar file.

I'm new in bash and stuck with this for 2 days and don't have any ideas. Do you have any suggestions?

Tranbi
  • 11,407
  • 6
  • 16
  • 33
K M
  • 11
  • 2

1 Answers1

2

If it's only one file:

$ tar cf /tmp/t.tar -C ~/tmp transpose.sh ; tar tf /tmp/t.tar 
transpose.sh

So the trick is to go to the target dir with tar -C.

$ ls ~/tmp/transpose.sh 
/home/jaroslav/tmp/transpose.sh

If the files are in different folders:

$ tar -cf- ~/tmp/transpose.sh /var/log/Xorg.1.log '--transform=s/.*\///' | tar tf -
tar: Removing leading `/' from member names
tar: Removing leading `/' from hard link targets
transpose.sh
Xorg.1.log

This uses a sed substitute expression s/// to replace .*/ with nothing.

From the GNU tar manual:

File name transformations

  --strip-components=NUMBER
    Strip NUMBER leading components from file names on extraction.

  --transform=EXPRESSION, --xform=EXPRESSION
    Use sed replace EXPRESSION to transform file names.