Move files by date

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • xavix
    New Member
    • Feb 2009
    • 2

    Move files by date

    Hello,

    How can I move files based on age in the following scenario:

    $SOURCE contains some subfolders with some files inside them.

    Each subfolder contain files of the same date, but the last modofication date of the subfolder can be more recent than the date of the files inside it, e.g.:

    $tree . -D
    .
    |-- [Feb 18 16:44] Folder1
    | |-- [Feb 3 3:58] 1.jpg
    | |-- [Feb 3 3:58] 2.avi
    | `-- [Feb 3 3:58] 3.jpg
    `-- [Feb 18 16:44] Folder2
    |-- [Jan 29 3:33] a.jpg
    |-- [Jan 29 3:33] b.jpg
    |-- [Jan 29 3:33] c.avi
    `-- [Jan 29 3:34] d.avi

    If I try to move only the files newer than 15 days (today is Feb 18) with the following command, I get all the entire tree because it seems is just reading the date on the folders not on the files.

    find $SOURCE -mtime -15 -exec mv {} $DEST \;

    So, I want to move the files and the folders that contains the files to $DEST.
  • numberwhun
    Recognized Expert Moderator Specialist
    • May 2007
    • 3467

    #2
    Originally posted by xavix
    Hello,

    How can I move files based on age in the following scenario:

    $SOURCE contains some subfolders with some files inside them.

    Each subfolder contain files of the same date, but the last modofication date of the subfolder can be more recent than the date of the files inside it, e.g.:

    $tree . -D
    .
    |-- [Feb 18 16:44] Folder1
    | |-- [Feb 3 3:58] 1.jpg
    | |-- [Feb 3 3:58] 2.avi
    | `-- [Feb 3 3:58] 3.jpg
    `-- [Feb 18 16:44] Folder2
    |-- [Jan 29 3:33] a.jpg
    |-- [Jan 29 3:33] b.jpg
    |-- [Jan 29 3:33] c.avi
    `-- [Jan 29 3:34] d.avi

    If I try to move only the files newer than 15 days (today is Feb 18) with the following command, I get all the entire tree because it seems is just reading the date on the folders not on the files.

    find $SOURCE -mtime -15 -exec mv {} $DEST \;

    So, I want to move the files and the folders that contains the files to $DEST.
    You might have to write a bit more code to be a bit more specific with what you want. Since $SOURCE contains the list of directories, it is using the dates on those as the comparison to mtime. What you are not telling it to do is go into the directories by using the --depth option to find. Also, I would have to check and see if find has an option to not act on directories themselves.

    Regards,

    Jeff

    Comment

    • xavix
      New Member
      • Feb 2009
      • 2

      #3
      Hi Jeff,

      $SOURCE is something like /mnt/data/

      I played around -depth and the option that only copy the wanted files is "-mindepth 2". What happens now is that "find $SOURCE -mindepth 2 -mtime -15 -exec mv {} $DEST \;" copies the files but not the folders that contains such files.

      Probably you are right and should write more code than a simply find command

      Thanks!

      Comment

      Working...