Solved

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

Posted on 2009-05-13
11
607 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
  • 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

PRTG Network Monitor lets you monitor your bandwidth usage, so you know who is using up your bandwidth, and what they're using it for.
ADCs have gained traction within the last decade, largely due to increased demand for legacy load balancing appliances to handle more advanced application delivery requirements and improve application performance.
Viewers will learn how to connect to a wireless network using the network security key. They will also learn how to access the IP address and DNS server for connections that must be done manually. After setting up a router, find the network security…
After creating this article (http://www.experts-exchange.com/articles/23699/Setup-Mikrotik-routers-with-OSPF.html), I decided to make a video (no audio) to show you how to configure the routers and run some trace routes and pings between the 7 sites…

810 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