Solved

how to mv files by date

Posted on 2007-11-21
5
1,292 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
  • 2
  • 2
5 Comments
 
LVL 40

Accepted Solution

by:
omarfarid earned 125 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 125 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
pauing printer deamon in AIX 10 57
Audit HISTFILE for all users on AIX 9 109
Write an app 10 67
How to Insert a File Using Text Editor 9 80
I promised to write further about my project, and here I am.  First, I needed to setup the Primary Server.  You can read how in this article: Setup FreeBSD Server with full HDD encryption (http://www.experts-exchange.com/OS/Unix/BSD/FreeBSD/A_3660-S…
Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
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.

863 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now