I have the following command in the part of a backup shell script:
tar -cjf site1.bz2 /var/www/site1/
When I list the contents of the archi
If you want to archive a subdirectory and trim subdirectory path this command will be useful:
tar -cjf site1.bz2 -C /var/www/ site1
Low reputation (too many years of lurking, sigh) so I can't yet comment inline, but I found the answer from @laktak to be the only one that worked as intended on Ubuntu 18.04 -- using tar -cjf site1.tar.bz2 -C /var/www/site1 .
on my machine resulted in all the files I wanted being under ./
inside the tar.bz2 file, which is probably ok but there is some risk of inconsistent behavior across OSs when un-tarring.
The following command will create a root directory "." and put all the files from the specified directory into it.
tar -cjf site1.tar.bz2 -C /var/www/site1 .
If you want to put all files in root of the tar file, @chinthaka is right. Just cd in to the directory and do:
tar -cjf target_path/file.tar.gz *
This will put all the files in the cwd to the tar file as root files.
tar -cjf site1.tar.bz2 -C /var/www/site1 .
In the above example, tar will change to directory /var/www/site1
before doing its thing because the option -C /var/www/site1
was given.
From man tar
:
OTHER OPTIONS
-C, --directory DIR
change to directory DIR
The option -C
works; just for clarification I'll post 2 examples:
creation of a tarball without the full path:
full path /home/testuser/workspace/project/application.war
and what we want is just project/application.war
so:
tar -cvf output_filename.tar -C /home/testuser/workspace project
Note: there is a space between workspace
and project
; tar will replace full path with just project
.
extraction of tarball with changing the target path (default to .
, i.e current directory)
tar -xvf output_filename.tar -C /home/deploy/
tar
will extract tarball based on given path and preserving the creation path; in our example the file application.war
will be extracted to /home/deploy/project/application.war
.
/home/deploy
: given on extract
project
: given on creation of tarball
Note : if you want to place the created tarball in a target directory, you just add the target path before tarball name. e.g.:
tar -cvf /path/to/place/output_filename.tar -C /home/testuser/workspace project
Found tar -cvf site1-$seqNumber.tar -C /var/www/ site1
as more friendlier solution than tar -cvf site1-$seqNumber.tar -C /var/www/site1 .
(notice the . in the second solution) for the following reasons