Solved

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

Posted on 2009-05-13
11
613 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

#Citrix #Citrix Netscaler #HTTP Compression #Load Balance
When it comes to security, there are always trade-offs between security and convenience/ease of administration. This article examines some of the main pros and cons of using key authentication vs password authentication for hosting an SFTP server.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

707 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