[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

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

Posted on 2009-05-13
11
Medium Priority
?
615 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
Independent Software Vendors: 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 2000 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

Plesk WordPress Toolkit

Plesk's WordPress Toolkit allows server administrators, resellers and customers to manage their WordPress instances, enabling a variety of development workflows for WordPress admins of all skill levels, from beginners to pros.

See why 2/3 of Plesk servers use it.

Question has a verified solution.

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

I had an issue with InstallShield not being able to use Computer Browser service on Windows Server 2012. Here is the solution I found.
This program is used to assist in finding and resolving common problems with wireless connections.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Suggested Courses

656 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