Shell command to tar directory excluding certain files/folders

后端 未结 28 2189
春和景丽
春和景丽 2020-11-27 08:31

Is there a simple shell command/script that supports excluding certain files/folders from being archived?

I have a directory that need to be archived with a sub dire

28条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-27 09:25

    old question with many answers, but I found that none were quite clear enough for me, so I would like to add my try.

    if you have the following structure

    /home/ftp/mysite/
    

    with following file/folders

    /home/ftp/mysite/file1
    /home/ftp/mysite/file2
    /home/ftp/mysite/file3
    /home/ftp/mysite/folder1
    /home/ftp/mysite/folder2
    /home/ftp/mysite/folder3
    

    so, you want to make a tar file that contain everyting inside /home/ftp/mysite (to move the site to a new server), but file3 is just junk, and everything in folder3 is also not needed, so we will skip those two.

    we use the format

    tar -czvf   
    

    where the c = create, z = zip, and v = verbose (you can see the files as they are entered, usefull to make sure none of the files you exclude are being added). and f= file.

    so, my command would look like this

    cd /home/ftp/
    tar -czvf mysite.tar.gz mysite --exclude='file3' --exclude='folder3'
    

    note the files/folders excluded are relatively to the root of your tar (I have tried full path here relative to / but I can not make that work).

    hope this will help someone (and me next time I google it)

提交回复
热议问题