LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Is this possible? Create a tar.gz of a directory and place it in a different directory without all of the directories nested in the path? (https://www.linuxquestions.org/questions/linux-software-2/is-this-possible-create-a-tar-gz-of-a-directory-and-place-it-in-a-different-directory-without-all-of-the-directories-nested-in-the-path-4175702819/)

wh33t 10-29-2021 11:35 PM

Is this possible? Create a tar.gz of a directory and place it in a different directory without all of the directories nested in the path?
 
Lemme explain. I wanna back up webroot located at /var/www/html/webroot by creating a file called webroot.tar.gz but I wanna place it in /var/www/html/archive, in the archive itself I only want to see the webroot directory, and it's files contained with in. I do not want to open the archive and see var, then www, then html, then webroot. I have a feeling I need to be using the -C option but I can't for the life of me figure out how.

Any tips? I am hoping to do this in a single command.

Update: I figured it out! Thanks for reading anyhow.

Code:

tar -czf /var/www/html/archive/webroot.tar.gz -C /var/www/html/ webroot

MadeInGermany 10-30-2021 01:49 AM

-C changes the start directory.
You can achieve the same with
Code:

( cd /var/www/html && tar -czf /var/www/html/archive/webroot.tar.gz webroot )

pan64 10-30-2021 03:50 AM

you must not put the tar created into the source directory tree (conflict between -C <dir> and -f <file>). Or at least do it in two steps: first create that tar somewhere outside and finally move it to the final location. (post #2 is ok, because webroot is packed into archive).

MadeInGermany 10-30-2021 09:46 AM

Good point in principle,
but here the webroot aka /var/www/html/webroot is outside.

wh33t 10-30-2021 10:03 AM

Quote:

Originally Posted by MadeInGermany (Post 6296957)
-C changes the start directory.
You can achieve the same with
Code:

( cd /var/www/html && tar -czf /var/www/html/archive/webroot.tar.gz webroot )

I should have mentioned this needs to fire from exec() in php land. I wasn't sure if or how I could toss multiple commands into a single function call, and if I were to do it in two, I wasn't sure how the system would be aware or keep track of the fact that I am now in a different directory.

Thanks for the explanations all!


All times are GMT -5. The time now is 02:40 AM.