Shell command to tar directory excluding certain files/folders

后端 未结 28 2186
春和景丽
春和景丽 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:38

    The following bash script should do the trick. It uses the answer given here by Marcus Sundman.

    #!/bin/bash
    
    echo -n "Please enter the name of the tar file you wish to create with out extension "
    read nam
    
    echo -n "Please enter the path to the directories to tar "
    read pathin
    
    echo tar -czvf $nam.tar.gz
    excludes=`find $pathin -iname "*.CC" -exec echo "--exclude \'{}\'" \;|xargs`
    echo $pathin
    
    echo tar -czvf $nam.tar.gz $excludes $pathin
    

    This will print out the command you need and you can just copy and paste it back in. There is probably a more elegant way to provide it directly to the command line.

    Just change *.CC for any other common extension, file name or regex you want to exclude and this should still work.

    EDIT

    Just to add a little explanation; find generates a list of files matching the chosen regex (in this case *.CC). This list is passed via xargs to the echo command. This prints --exclude 'one entry from the list'. The slashes () are escape characters for the ' marks.

提交回复
热议问题