Link to home
Start Free TrialLog in
Avatar of Baudb0y
Baudb0y

asked on

how to mv files by date

I have a ton of very old log files that I do not want to delete but would like to move to another file system for archiving. I need a command that will identify all of the files with the date stamp of 2005 and then move them.
This is a small sample of the directory listing ls -la |grep "2005"
 
-rw-rw-rw-   1 root     other        100 Nov 11  2005 ifOutOctets.70!
-rw-rw-rw-   1 root     other        100 Nov 11  2005 ifOutOctets.71!
-rw-rw-rw-   1 root     other        100 Nov 11  2005 ifOutOctets.72!
-rw-rw-rw-   1 root     other        100 Nov 11  2005 ifOutOctets.73!
-rw-rw-rw-   1 root     other        100 Nov 11  2005 ifOutOctets.74!
-rw-rw-rw-   1 root     other        100 Nov 11  2005 ifOutOctets.75!
-rw-rw-rw-   1 root     other        100 Nov 11  2005 ifOutOctets.76!
-rw-rw-rw-   1 root     other        100 Nov 11  2005 ifOutOctets.77!
-rw-rw-rw-   1 root     other        100 Nov 11  2005 ifOutOctets.78!
-rw-rw-rw-   1 root     other        100 Nov 11  2005 ifOutOctets.79!
-rw-rw-rw-   1 root     other         99 Nov 11  2005 ifOutOctets.8!
-rw-rw-rw-   1 root     other        100 Nov 11  2005 ifOutOctets.80!

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of omarfarid
omarfarid
Flag of United Arab Emirates image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Baudb0y
Baudb0y

ASKER

Am I to make that into a script? If so does this look correct?

#!/bin/ksh
`ls -la |grep "2005" | awk '{print $9}'`
do
mv $file /new/dir/name
done
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Also, the for statement he refers to has back-ticks because it is using the ls command output as the argument for the for loop.  Doing back-ticks for a command by itself wouldn't be the way you would want to go.  The other issue could be it any file has 2005 in the file name, it would also get moved.
Hi,

Baudb0y:

Yes, you need to run it as a script and what you posted is correct.

SwassLikeMe:

Your comment about using find instead of ls is valid and appreciated, but the script I posted was based on the initial posting by Baudb0y, where I felt that he is comfortable with ls output and he would have though of filenames that have 2005 part of it.