How to tar while excluding a combination of files and directories

An auction script has the following directory structure

/var/www/html/
              file1
              file2

/var/www/html/subdir1/
/var/www/html/subdir2/
/var/www/html/subdir3/
/var/www/html/subdirN/

/var/www/html/uploaded/
                       .htaccess            
                       file3
                       file4

                      /banners/
                               file5
                               file6

                      /cache/
                             file7         <<< exclude
                             file8         <<< exclude

                      /hledmd7phk4u5glq5rks8kb621/        <<< exclude
                                                  file9   <<< exclude
                                                  file10  <<< exclude
                      /ad0hmd7phk4u5glq5rks8kb258/        <<< exclude
                                                  file11  <<< exclude
                                                  file12  <<< exclude
                      /zfddfd7phk4u5glq5rks8kb584/        <<< exclude
                                                  file13  <<< exclude
                                                  file14  <<< exclude

A Ubuntu 14.04 tar is being run in crontab -e nightly at present that backs up the entire directory from the /var/www/html/ level using absolute paths. A new tar backup command is needed that will reduce the size of the nightly backup by excluding the combination of files and directories shown above while:

1) including the directory/var/www/html/uploaded/cache/ while excluding the contents under /var/www/html/uploaded/cache/

2) excluding the long list of ever changing MD5 directory names and their contents that are created under /var/www/html/uploaded/

Is there a way to do this in one tar command?


Not a final solution for you, but here is a directive way of thinking:

For the first requirement, we can use wildcard like this:

tar -cvf test.tgz --exclude=/var/www/html/uploaded/cache/*

For the second requirement, MD5 as a filename do not have a string pattern, so more other information should be added to filter those directories.

@ml48603, for how to strcat tar command, here is an example:

tar_all_cmd=tar -cvf test.tgz

dir_name="/var/www/html/uploaded/"

ls $dir_name | while read filename; do if [ ${#filename} -ge 8 ]; then $option_head="--exclude=$dir_name"; tar_all_cmd="${tar_all_cmd}${option_head}${filename}"; fi;  done;
链接地址: http://www.djcxy.com/p/47088.html

上一篇: Javascript MIME类型

下一篇: 如何排除文件和目录的组合