Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

how to mv files by date

Posted on 2007-11-21
5
Medium Priority
?
1,316 Views
Last Modified: 2013-12-27
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

0
Comment
Question by:Baudb0y
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 40

Accepted Solution

by:
omarfarid earned 375 total points
ID: 20329540
Hi,

try

for file in `ls -la |grep "2005" | awk '{print $9}'`
do
mv $file /to/new/dir
done
0
 

Author Comment

by:Baudb0y
ID: 20330141
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
0
 
LVL 2

Assisted Solution

by:SwassLikeMe
SwassLikeMe earned 375 total points
ID: 20331249
You could do that, but really I would use a find command.  If you specifically wanted only 2005 and not before or after, it might not work well, but I would do this:

find ./ -name 'ifOutOctets.*' -mtime +365 -print -exec mv {} /newdir \;

the +365 means modify date older and x days.  If you want two years ago, it would be +730, for example.

Also, the ./ would mean this directory, you may need to modify that based on where the log files are actually located.  Keep in mind, by default, find will also search subdirectories.
0
 
LVL 2

Expert Comment

by:SwassLikeMe
ID: 20331274
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.
0
 
LVL 40

Expert Comment

by:omarfarid
ID: 20332316
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.

0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hello fellow BSD lovers, I've created a patch process for patching openjdk6 for BSD (FreeBSD specifically), although I tried to keep all BSD versions in mind when creating my patch. Welcome to OpenJDK6 on BSD First let me start with a little …
Let's say you need to move the data of a file system from one partition to another. This generally involves dismounting the file system, backing it up to tapes, and restoring it to a new partition. You may also copy the file system from one place to…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.
Suggested Courses

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question