1

After SSH into remote webserver, I execute the following command on CentOS remote web server:

tar -vcf remote_name.tar /home/remote_name/public_html

On my local sys (Debian based), I execute the following:

user@linux-mx21:/var/www/html
$ wget https://example.com/remote_name.tar
$ mkdir remote_name
$ tar -vxf remote_name.tar --directory ./remote_name

All good, BUT... I don't want the dir structure the way it results as: /var/www/html/remote_name/home/remote_name/public_html/ (public_files,and,dirs_extracted_here).

Instead, I want the result to be as:

/var/www/html/remote_name/

where the resulting files under new location,

/var/www/html/remote_name would be the mirror of original location, excluding the parent directory structure.

Considering my result w/ commands as illustrated above, the files which now reside in /var/www/html/remote_name/home/remote_name/public_html must be moved from to my desired location /var/www/html/remote_name/. I want to skip that step with an improvement to my commands.

I presume this can be set w/ args on the tar command as desired when I'm SSH at remote_location, but I don't know the correct arguments to achieve what I want.

Your assistance is much appreciated!

1 Answer 1

1

tar has the ability to chdir into a directory via the C command:

$ mkdir -p a/b/public_html
$ touch a/b/public_html/index.html
$ tar vCcf a/b/public_html remote_name.tar .
a .
a ./index.html

This makes the tarball relative to the current directory a/b/public_html. However, this is not typical, and may be considered rude, as someone else expanding the tarball will end up by default with files all over their home directory. If you are the only one using the tarball, this is probably not a concern.

More typical would be to make the tarball one level up, or:

$ rm remote_name.tar
$ tar vCcf a/b remote_name.tar public_html
a public_html
a public_html/index.html

Then, on the expansion side you will need to make an appropriate chdir either in the parent command, e.g.

cd some/dir && tar xf remote_name.tar

or within the tar command itself

tar xCf some/dir remote_name.tar

Exactly what some/dir should be will depend on exactly how the tarball was made, either from the less common . directory, or the more typical parent-of directory.

2
  • Hi. Thank you @thrig I'm a bit confused about "are we SSH on remote server, or performing operations on a 'local' server (aka, a server#2)?" In my example, i'm using remote_name where remote server path is /home/remote_name and local path is /var/www/html (regardless, tarball would remain named remote_name.tar at either location) I realize you're abbreviating, above. but, E.g. assuming this is SSH on "remote" server, why $ mkdir -p a/b/public_html as first command? I realize backups are not usually placed in ./public_html, but for the sake of this example... thank you!
    – J S
    Sep 6, 2022 at 17:55
  • the mkdir is so that the subsequent example commands have a directory tree to chdir into when constructing the tarball. a/b would be substituted for some other suitable directory tree on an actual system
    – thrig
    Sep 6, 2022 at 18:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .