Tar a directory, but don't store full absolute paths in the archive

前端 未结 9 894
鱼传尺愫
鱼传尺愫 2020-12-04 05:14

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

相关标签:
9条回答
  • 2020-12-04 05:22

    If you want to archive a subdirectory and trim subdirectory path this command will be useful:

    tar -cjf site1.bz2 -C /var/www/ site1
    
    0 讨论(0)
  • 2020-12-04 05:23

    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.

    0 讨论(0)
  • 2020-12-04 05:24

    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.

    0 讨论(0)
  • 2020-12-04 05:28
    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
    
    0 讨论(0)
  • 2020-12-04 05:28

    The option -C works; just for clarification I'll post 2 examples:

    1. 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 .

    2. 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
    
    0 讨论(0)
  • 2020-12-04 05:36

    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

    • Tar file name can be insignificant as the original folder is now an archive entry
    • Tar file name being insignificant to the content can now be used for other purposes like sequence numbers, periodical backup etc.
    0 讨论(0)
提交回复
热议问题