How to create flat tar archive

2019-04-05 11:54发布

问题:

my tree command returns

tmp
`-- t
    `-- e
        |-- 23.ps
        `-- s
            |-- adsf.ps
            `-- t
                `-- dupa.ps

How can I create archive ps.tar.gz in tmp directory with following structure:

tmp
|-- ps.tar.gz
|   |-- 23.ps
|   |-- adsf.ps
|   `-- dupa.ps
`-- t
    `-- e
        |-- 23.ps
        `-- s
            |-- adsf.ps
            `-- t
                `-- dupa.ps

?

回答1:

outfile=$(pwd)/ps.tar;find . -type f | while read file; do 
    tar rf $outfile -C $(dirname $file) $(basename $file)
done
gzip $outfile


回答2:

for GNU tar you can use the --xform argument. It takes a sed expression and applies it on each file name. So for removing all characters up to the last /, you may use:

tar -c --xform s:^.*/:: your-files


标签: gzip tar