Find the answer to your Linux question:
Results 1 to 6 of 6
Say i have files of test1, test2, test3, test4... and so on... and all the test are 10 days old or older. But i want to keep the last test ...
  1. #1
    Just Joined!
    Join Date
    Apr 2008
    Posts
    7

    keep last file from moving

    Say i have files of test1, test2, test3, test4... and so on...
    and all the test are 10 days old or older. But i want to keep the last test from moving into another directory.

    find old_dir/ -type f -name "*test" -mtime +10 -exec mv '{}' new_dir/ \;
    that moves all the test into another directory. but...
    is there a way to keep the last test from moving? basically i want to keep the most recent test from moving.

  2. #2
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    Code:
    find old_dir/ -type f -name "*test" -mtime +10 | head -n -1 | xargs -i mv '{}' new_dir

  3. #3
    Just Joined!
    Join Date
    Apr 2008
    Posts
    7
    Thanks again,helped very much!

  4. #4
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    This works provided the most recent file is the last one that find finds, which may not be the case if files are deleted and the gaps reused (though I'm not sure exactly how Linux filesystems organise their directories). To avoid this, I'd use something like:
    Code:
    ls -rt $(find old_dir/ -type f -name "*test" -mtime +10) | head -n -1 | xargs -i mv '{}' new_dir
    which should guarantee the newest file name is the last output.

  5. #5
    Just Joined!
    Join Date
    Apr 2008
    Posts
    7
    the code above works if it put test from 1-8. but if put it backwards like 8-1, and 1 is the most recent file, 8 stays but 1 moves.

  6. #6
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    That's why my solution uses ls to order the files by reverse time (-rt), not alphabetically.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...