Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rename Directory Name Before tar Happens

I have a directory e.g. /var/tmp/my-dir/ that I frequently compress with the following command:

$ cd /var/tmp/ $ tar -zcf my-dir.tar.gz my-dir/* 

Later, when I untar my-dir.tar.gz, it'll create my-dir/ in the current directory. It sounds like the my-dir directory is "wrapped" inside the tarball. Is there a tar option to rename my-dir to e.g. your-dir before the actual tarring happens. So that ...

$ tar -zxf my-dir.tar.gz # So that ... this creates your-dir/, instead of my-dir/ 

Thanks.

like image 348
moey Avatar asked Mar 15 '12 23:03

moey


People also ask

What does the command tar XVZF * .tar do?

gz using option -xvzf : This command extracts files from tar archived file. tar.


2 Answers

Which tar?

GNU Tar accepts a --transform argument, to which you give a sed expression to manipulate filenames.

For example, to rename during unpacking:

tar -zxf my-dir.tar.gz --transform s/my-dir/your-dir/ 

BSD tar and S tar similarly have an -s argument, taking a simple /old/new/ (not a general sed expression).

like image 120
ephemient Avatar answered Oct 01 '22 11:10

ephemient


For mac works -s flag.

Rename on compress:

tar -zcf my-dir.tar.gz -s /^my-dir/your-dir/ my-dir/*

Rename on extract:

tar -zxf my-dir.tar.gz -s /^my-dir/your-dir/

like image 44
DamneD Avatar answered Oct 01 '22 11:10

DamneD