How can i move folders and its content if folder is older than 1,5 days and keep subdirs in bash?

Hello all,
do you know any way i can i move folders and its content if folder is older than 1,5 days in bash?

I tried:

find /home/xyz/DATA/* -type d -ctime +1.5  -exec mv "{}" /home/xyz/move_data_here/ \;

All i got was that Files from DATA / home/xyz/DATA/* ended messed up in /home/xyz/move_data_here/ , i need to keep folders

example hot it should works, so like i move

/home/xyz/DATA/subdir/subsubdir/files.rar

into

/home/xyz/move_data_here/subdir/subsubdir/files.rar

THX!

Most find implementations I know of only support integer values for -ctime days creation time.

If you have GNU find you can use -cmin instead and specify 1.5 days in minutes using -cmin 720

When you move a folder all sub-folders will automatically be moved so I used a awk program to skip subfolders already moved.

DEST=/home/xyz/move_data_here
cd /home/xyz/DATA/
find . -type d -cmin +720 -print | awk '
  {
     l=split($0,D,"/")
     m=D[1]
     for(i=2;i<=l;i++) {
        if(m in done) next
        m=m"/"D
     }
     done[m]
     print m
}' | while read folder
do
   PARENT=${folder%/*}
   [ -d "$DEST/$PARENT" ] || echo mkdir -p "$DEST/$PARENT"
   echo mv "$folder" "$DEST/$folder"
done

Remove echos above (in red) once you are sure it is doing what you require. Backup you files before running for real.

1 Like