shantanuo
shantanuo

Reputation: 32218

tar all files to a directory

The following will extract the files in /root/ directory. But it also creates the parent directories under root. What I need is that the files should be exactly under root folder and not in /root/data/mysql/...

# tar -xvf company_raw_2012-02-22.tgz --directory=/root/
data/mysql/company_raw/data_archive_r_20120222.MYD
data/mysql/company_raw/data_archive_r_20120222.MYI
data/mysql/company_raw/data_archive_r_20120222.frm

If that is not possible, how do I write a program to move these files to the required folder?

I have tried the following and it does work.

--strip-components=3

But I do not know how many folder will be there. So the number 3 may change.

Upvotes: 0

Views: 609

Answers (1)

rkhayrov
rkhayrov

Reputation: 10260

Extract everything to the temp directory with full path and then just walk it moving files to the desired destination?

destdir=/root
tmpdir=/root/tmp
rm -rf $tmpdir
mkdir $tmpdir
tar xf archive.tar.gz -C $tmpdir
find -H $tmpdir -type f -exec mv '{}' $destdir \;

Upvotes: 2

Related Questions