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

前端 未结 9 937
鱼传尺愫
鱼传尺愫 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:42

    One minor detail:

    tar -cjf site1.tar.bz2 -C /var/www/site1 .
    

    adds the files as

    tar -tf site1.tar.bz2
    ./style.css
    ./index.html
    ./page2.html
    ./page3.html
    ./images/img1.png
    ./images/img2.png
    ./subdir/index.html
    

    If you really want

    tar -tf site1.tar.bz2
    style.css
    index.html
    page2.html
    page3.html
    images/img1.png
    images/img2.png
    subdir/index.html
    

    You should either cd into the directory first or run

    tar -cjf site1.tar.bz2 -C /var/www/site1 $(ls /var/www/site1)
    

提交回复
热议问题