Solved

Unix scripting - Date cmd - if ne to today then move?

Posted on 2009-05-13
11
612 Views
Last Modified: 2013-12-26
I'm asking for help in writing a script to move some log files.  I'm stuck on the date cmd.  I want to move any file that ends in .log and whose date is not equal to today.  The o/s is Redhat's linux.   I've read the man page on the date cmd, and I'm searching for examples on how this is done, but I haven't found it yet.  Would somebody help me out with a bit of code? Here's what I've got and this works for me, what I need help with is to figure out how to select the files whose date is not equal to today's date. Thanks

#! /bin/sh
cd /opt/log
mv 2009-05-10*.log /var/httplogs/
cd /var/
tar -cvf 2009-05-10.tar /var/httplogs/
gzip 2009-05-10.tar

USER="username"
PASS="userpassword"
FTPSERVER="servername.domain"
ftp -i -n $FTPSERVER << EOF
user $USER $PASS

prompt off
cd /tmp/
put 2009-05-10.tar.gz
bye
 
0
Comment
Question by:Westez
[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
  • 4
  • 3
  • 3
  • +1
11 Comments
 

Author Comment

by:Westez
ID: 24381684
I'm in the Central Time zone.  A lot of the code I've been checking out mentions the timezone.
0
 
LVL 5

Expert Comment

by:0ren
ID: 24381799
you should do it with the find command
i will send you the syntax
0
 
LVL 5

Expert Comment

by:0ren
ID: 24381812
find ./ -ctime -10
will find files that was created in the last 10 days
find ./ -ctime +10
will find files that was created before the last 10 days
find ./ -name "*.log"
will find all *.log files
you can combine and ls them
find ./ -ctime -10 -name "*.log" -exec ls {} \;
0
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
LVL 5

Expert Comment

by:0ren
ID: 24381817
you can also use atime - access time
                           mtime - modification time

and many others

just post what you need to do
0
 
LVL 5

Expert Comment

by:0ren
ID: 24381839
will find files 10 created 10 days ago
find ./ -ctime 10
0
 
LVL 48

Expert Comment

by:Tintin
ID: 24381843
You can use grep to exclude todays files, eg:
#!/bin/bash
today=$(date +%Y-%m-%d)
 
cd /opt/log
ls *.log | grep -v $today | xargs -i mv {} /var/httplogs

Open in new window

0
 

Author Comment

by:Westez
ID: 24390244
Hey guys thanks a lot.  Tintin your solution works great.  

Oren - I like your approach because it allows me to leave a few days worth of logs on the server.  I want to apply this same type of script to a firewall once I'm done with the web server. And I want to leave 5 days worth of logs on the firewall.

Would you modify this line of code so it moves the files to /opt/log/?
find ./ -ctime -10 -name "*.log" -exec ls {} \;  

Tintin - Would you modify your code so it leaves a few days worth of log files on the server?

If I'm over stepping the rules of fair play here let me know and I'll close out this question and open a new one.
0
 
LVL 48

Expert Comment

by:Tintin
ID: 24390456
If you want  to keep a few days worth, then find is easier

find /opt/log -name "*.log" -mtime +5 | xargs -i {} /var/httplogs
0
 
LVL 48

Accepted Solution

by:
Tintin earned 500 total points
ID: 24390462
Whoops, the above should be
find /opt/log -name "*.log" -mtime +5 | xargs -i mv {} /var/httplogs

Open in new window

0
 

Author Comment

by:Westez
ID: 24398391
Tintin - works great just like the first solution.  

Thanks to both of you for your for your suggestions and solutions.
0
 

Expert Comment

by:benak
ID: 24561978
Hi,

Apologies for hijacking the thread, but my problem is very similar to this and requires some tweaking.
Finding the files except for 2 for example works OK for me, but moving part needs changing. Because of the nature of the application I am executing this command from, I have effectively 3 concerned locations:
 - the home folder where I am going to execute the command from
 - the location of the folder I want to find the files in (different to above)
 - the location of the folder I want to move the files to (different to above)
 So, say I have the "test2.csv" and "test3.csv" /apps/home/source among many other files. I want to move all files except for the 2 above to /apps/home/target.
And my login location (from where I can only execute the find and move command) is /apps/dsbin/.

Does anybody have any idea how I can achieve this?

Thanks,

Benak
0

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Suggested Solutions

Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

738 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