Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Let's say there's a specific time and date I have in mind. All files last edited before this date I want to keep in the directory but all files that have been edited since this date I want to mv somewhere else. The man page of mv doesn't show this being possible with mv directly. I thought some form of the the following should work:

ls -t | head -n $number 

Where $number specifies the number of files that have been edited since the time and date I had in mind. I could then somehow feed this to mv to mv those files (haven't thought up exactly how to do that). The disadvantage of this is that I would have to count up how many files have been edited since the date and time I had in mind. Is there a way where I can just specify a date and time and let my computer figure out which files need to be mved and mv them for me? If not, then how would I complete the command I have written above to feed those file names to mv to have them all mved to the same location?

share|improve this question

1 Answer 1

up vote 3 down vote accepted
find /path/to/dir -mtime +5 -exec mv {} /target/path/ ';'

will move all files in /path/to/dir that are older than five days to /target/path.

You can try this to see what will actually be executed:

find /path/to/dir -mtime +5 -exec echo mv {} /target/path/ ';'

Note that the -mtime parameter checks the file's modification time. Have a look at -ctime or -atime in find's manpage for more detail.

If you want to specify your times in minutes, use one of -mmin, -cmin and -amin instead.

To find files younger than a specific amount of time, use - instead of +, e.g. -mmin -30.

Another method would be to use xargs (which will execute a command with each of its input lines; manpage):

find /path/to/dir -mtime +5|xargs -i echo mv {} /target/path 

(remove the 'echo' to actually move stuff)

share|improve this answer
    
What if I want something less than five days? Like, say 30 minute? –  Joshua May 21 at 21:00
    
use find's -mmin parameter –  mreithub May 21 at 21:01
    
My guess from my own experimentation is that changing the argument to -mtime from +5 to -5 will move all files that are younger than five days. Correct? –  Joshua May 21 at 21:06
    
And what does the ';' do at the end of the command? I understand what everything else in the command does. –  Joshua May 21 at 21:06
1  
Yes, but that's 'more than 5*24h before now'; if you want actual days, use -daystart instead. But it's all described in the manpage and not that hard to test without breaking stuff (if you put an echo before the execution commands) –  mreithub May 21 at 21:28

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.