1836779 Members
2428 Online
110109 Solutions
New Discussion

moving files by date

 
SOLVED
Go to solution
Mark Vollmers
Esteemed Contributor

moving files by date

I have a folder with a very large number of files in it. I need to go through that and move all of the files that were created from a certain date on (say 1-19-01) to another folder. What is the easiest way to do this? I can't find anything for mv that talks about moving by date, and I would really hate to list them and move individually (or even in handfuls). There could be several hundred files or more. Thanks in advance!
"We apologize for the inconvience" -God's last message to all creation, from Douglas Adams "So Long and Thanks for all the Fish"
5 REPLIES 5
Curtis Larson
Trusted Contributor

Re: moving files by date

how about trying this:

here=/tmp/test
there=/tmp/test


for month in Jan Feb # etc
do
(( day = 0 ))
while (( $day < 31 ))
do
(( $day = $day + 1 ))
[ ! -d $there/${month}.${day} ] && mkdir $there/${month}.${day}
there=$there/${month}.${day}

ll $here | tr -s '[:space:]' | awk "/$month $day/"'{print $NF;}' |
while read file
do
mv $here/$file $there/
done
done
done
James R. Ferguson
Acclaimed Contributor
Solution

Re: moving files by date

Hi Mark:

Use the 'find' command with the 'newer' option and a "reference file" that you specify with the date and time transition:

# cd
# touch -mt myfile
# find . ! -newer myfile -exec mv {} \;

See the man pages for 'find'.

...JRF...
Curtis Larson
Trusted Contributor

Re: moving files by date

probably want to add:

[ -f $file ] && mv $here/$file $there/

so you don't affect directories and such
Mark Vollmers
Esteemed Contributor

Re: moving files by date

Thanks for the help! I used James suggestion (Curtis, not that your's wouldn't work, but James' was shorter :) ) Thanks. It saved me a lot of time.
"We apologize for the inconvience" -God's last message to all creation, from Douglas Adams "So Long and Thanks for all the Fish"
James R. Ferguson
Acclaimed Contributor

Re: moving files by date

Hi Mark (again):

A postscript (pun intended).

'find' is intended to descend a directory structure. In some cases, you may want to limit this action. For instance, if you had subdirectories and files therein, upon which you did not want 'find' to operate, you could add the and options to the syntax I presented above. Thus:

# find . ! -newer myfile -prune -type f -exec mv {} \;

The "-type f" selects only regular files; not directories, etc. The "-prune" keeps 'find' from examining the contents of the subdirectory in the first place.

Regards!

...JRF...