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

Posted on 2009-05-13
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

ftp -i -n $FTPSERVER << EOF
user $USER $PASS

prompt off
cd /tmp/
put 2009-05-10.tar.gz
Question by:Westez
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

Author Comment

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

Expert Comment

ID: 24381799
you should do it with the find command
i will send you the syntax

Expert Comment

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 {} \;
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.


Expert Comment

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

and many others

just post what you need to do

Expert Comment

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

Expert Comment

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

Open in new window


Author Comment

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.
LVL 48

Expert Comment

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
LVL 48

Accepted Solution

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


Author Comment

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

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

Expert Comment

ID: 24561978

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?



Featured Post

Simple, centralized multimedia control

Watch and learn to see how ATEN provided an easy and effective way for three jointly-owned pubs to control the 60 televisions located across their three venues utilizing the ATEN Control System, Modular Matrix Switch and HDBaseT extenders.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to include conditional log rotate in liunx. 17 68
VPN Server 5 52
Isolated network on ESXi 6.5 8 45
Datacenter Upgrade - Design Question 5 18
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.
For many of us, the  holiday season kindles the natural urge to give back to our friends, family members and communities. While it's easy for friends to notice the impact of such deeds, understanding the contributions of businesses and enterprises i…
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…
Here's a very brief overview of the methods PRTG Network Monitor ( 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…

756 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