-1

I have some mp3 on various dir. I did

find $HOME -name *.mp3|pax -wf arch.tar

The command works fine,but when extract, it recreate the full dirs for example

tar -tvf arch.tar

Return

dir1/dir2/sound.mp3
dir3/music.mp3
dir4/dir5/anotherdir/anothermusic.mp3

When extract of course will create a lot of dirs. My desire is a thing like this

find $HOME -name *.mp3|pax -w"possibleoption" -f arch.tar

To obtain an archive like this

/sound.mp3
/music.mp3
/anothermusic.mp3

I know is not safe,but is possible to strip dir path on fly?

p.s= I used pax command for example, but the classical tar is also good

elbarna
  • 12,050
  • 22
  • 92
  • 170

1 Answers1

3

These variations all assume that you've created an archive with the original path stored in the archive itself. They transform the path when listing/extracting the archive.

Using pax with its -s flag:

pax -f archive.tar -s '@.*/@/@' '*.mp3'

Add -r at the start to actually extract the files.

With BSD tar:

tar -tf archive.tar -s '@.*/@/@' '*.mp3'

Change -t to -x to actually extract the files.

With GNU tar:

tar -tPf archive.tar --transform 's@.*/@/@' --show-transformed-names --wildcards '*.mp3'

Change -t to -x to actually extract the files.

The string replacement will remove the path stored in the archive and replace it with a single /.


The equivalent operation for creating an archive with files whose pathnames are read from standard input (transforming the names by replacing the path with /):

pax:

pax -w -f archive.tar -s '@.*/@/@'

BSD tar:

tar -c -f archive.tar -s '@.*/@/@'

GNU tar:

tar -c -f archive.tar --transform='s@.*/@/@' --files-from=-
Kusalananda
  • 320,670
  • 36
  • 633
  • 936
  • pax works fine with find,tar give error if read from stdin with those options – elbarna Apr 10 '18 at 12:30
  • @elbarna See updated answer. – Kusalananda Apr 10 '18 at 12:49
  • Ok,perfect,voted as solution – elbarna Apr 10 '18 at 14:05
  • @Kusalananda is there a reason for not mentioning the oldest free `tar` implementation (`star`)? `star` started in 1982 and is faster and more powerful.. (e.g. built in `find`), `star` is also able to emulate the CLI from `cpio`, `pax`, ... BTW: there are many `pax` implementations and many of them are bad or implement too few of the official features. – schily Jun 12 '20 at 11:22
  • @schily Yes, there is a reason. None of the systems I work with has it installed and I have no experience with it. – Kusalananda Jun 12 '20 at 11:25
  • @Kusalananda you sould fetch a recent `schilytools` and give it a try. None of the other tar implementations implements the features, I need for my every day work and GNU tar in addition is not mature enough to be in a list of useful software I like to use. – schily Jun 12 '20 at 11:35