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 ...
- 04-18-2008 #1Just 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.
- 04-18-2008 #2Linux 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
- 04-19-2008 #3Just Joined!
- Join Date
- Apr 2008
- Posts
- 7
Thanks again,helped very much!
- 04-19-2008 #4Linux 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:
which should guarantee the newest file name is the last output.Code:ls -rt $(find old_dir/ -type f -name "*test" -mtime +10) | head -n -1 | xargs -i mv '{}' new_dir
- 04-20-2008 #5Just 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.
- 04-21-2008 #6Linux 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.


Reply With Quote