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
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:
If anyone could be of any help on this, it would be greatly appreciated!
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");
An alternative way would be to pre-process the tar archive before extracting it. Then you would have several steps:
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.
extract from that new modified archive, using tar -xf
; you might also pass -C
to change directory (as suggested by Grebneke's answer)
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).