Solved

how to mv files by date

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Shell script errors 10 133
Bad Block Relocation for Synchronous GLVM AIX 7.1 2 71
UNiX Script filesystem space usage Part2 54 87
Unix Script: Loop over all days of a month 17 98
When you do backups in the Solaris Operating System, the file system must be inactive. Otherwise, the output may be inconsistent. A file system is inactive when it's unmounted or it's write-locked by the operating system. Although the fssnap utility…
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
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…

773 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