Search code examples
linuxfilterfindrsync

Filter and copy all the subdirectories and its content


I have a list of directories and files that I need to migrate. The directory tree looks something like:

-var
   -data
      -archive
           -111111
               -logs
                  datetime.log 
               meter.txt
           -222222
               -logs
                  datetime.log 
               meter.txt
           -configurations
               -rules
               config.json
           -recycle
               

When copying, it should satisfy the below conditions:

  • Copy only files/ directories that are less than or equal to 30 days. (This condition is not applicable to the configurations directory. Get all contents under configuration irrespective of its creation/modification date time.)
  • Exclude the directory recycle.
  • Copy only the content under archive. That means do not copy directories var, data & archive.

After copying the directory should look something like this:

-target
     -111111
          -logs
              datetime.log 
           meter.txt
     -222222
          -logs
              datetime.log 
          meter.txt
     -configurations
          -rules
          config.json

I came up with this:

find var/data/archive ! -path "*/recycle*" \( -path "*/configurations*" -o -name "*"  -mtime -30 \) | rsync -av --inplace --files-from=- . target

I am able to achieve the first 2 conditions, i.e. copying files that are less than 30 days and excluding the directory recycle. But not able to exclude var/data/archive. How do I fix this?


Solution

  • This worked for me.

    cd ${HOME}/var/data/archive;\ 
    find . ! -path "*/recycle*" \( -path "*/configurations*" -o -name "*"  -mtime -10 \) | rsync -av --inplace --files-from=- . ${HOME}/target