Mario
Mario

Reputation: 15

tar without retaining the directory structure

I would like to tar a directory without retaining the directory structure, for example I am tarring like this

shell_exec('tar czf '. __DIR__ .'/tmp/'.$entry.'.tar.gz /home/minecraft/multicraft/servers/'.$entry);

This works fine and correctly but when I open the .tar.gz it is shown like this

enter image description here

I would like to not retain the directory structure just the folder contents without the directory structure. So like, the .tar.gz contains /home/minecraft/multicraft/servers/server1 I just would like to only have the contents of the directory server1 in the .tar.gz

contents of server1:

enter image description here

If anyone could be of any help on this, it would be greatly appreciated!

Upvotes: 0

Views: 1002

Answers (2)

An alternative way would be to pre-process the tar archive before extracting it. Then you would have several steps:

  1. process the tar archive, probably using tardy (probably with -Remove_Prefix option); you then could remove any prefix in the tar ball and you'll get new a modified tar archive.

  2. extract from that new modified archive, using tar -xf; you might also pass -C to change directory (as suggested by Grebneke's answer)

  3. Remove the new modified tar archive (and perhaps the original one, if needed)

This approach is quite general, since  tardy is a quite powerful processor. However, you might need more disk space (potentially twice the space occuped by the archived files, since you probably need the processed tar archive to be uncompressed).

Upvotes: 0

grebneke
grebneke

Reputation: 4494

Use tar -C to change directory before creating the archive:

tar cz -C /path/to/dir . -f /path/to/archive.tar.gz
# archive.tar.gz gets contents of /path/to/dir as root entry:
# ./
# ./data.txt

If I got your PHP-code correctly, it would be something like:

$tar_dir = "/home/minecraft/multicraft/servers/$entry";
$archive = __DIR__ . "/tmp/$entry.tar.gz";
shell_exec("tar cz -C $tar_dir . -f $archive");

Upvotes: 1

Related Questions