Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

how to mv files by date

Posted on 2007-11-21
5
Medium Priority
?
1,318 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 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Introduction Regular patching is part of a system administrator's tasks. However, many patches require that the system be in single-user mode before they can be installed. A cluster patch in particular can take quite a while to apply if the machine…
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
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.
Suggested Courses

916 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