Solved

how to mv files by date

Posted on 2007-11-21
5
1,289 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

In tuning file systems on the Solaris Operating System, changing some parameters of a file system usually destroys the data on it. For instance, changing the cache segment block size in the volume of a T3 requires that you delete the existing volu…
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.:
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

708 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

12 Experts available now in Live!

Get 1:1 Help Now