?
Solved

Get files between two dates, Linux RedHat

Posted on 2014-11-26
7
Medium Priority
?
341 Views
Last Modified: 2014-11-27
Hi Experts,
I would write a script to make a copy (scp) of all Transfer_data files. * From a path, which were generated from the current time (DATE) and 1 minute backward.
Example:
hostname: / path /> date
Wed November 26 2014 16:53:00 ART
hostname: / path /> ls -t1 Transfer_data *.
Transfer_data.20141126165232
Transfer_data.20141126165203
Transfer_data.20141119165103
Transfer_data.20141119165003
Transfer_data.20141119164909
Transfer_data.20141119164801

Open in new window

I wish to copy only:
Transfer_data.20141126165232
Transfer_data.20141126165203

Open in new window

and after a minute, rerun the script, taking only the files generated during that minute
Example2:
hostname: / path /> date
Wed November 26 2014 16:54:00 ART
hostname: / path /> ls -t1 Transfer_data *.
Transfer_data.20141126165332
Transfer_data.20141126165303
Transfer_data.20141126165232
Transfer_data.20141126165203

Open in new window

I wish to copy only:
Transfer_data.20141126165332
Transfer_data.20141126165303

Open in new window

I know how to use the cron tool, but I have trouble generating the ranges in a script.
I tried to start with:
#!/bin/ksh
touch -t date --date "1 minute ago"  start
touch -t date end
find . \( -newer inicio -a \! -newer fin \) -exec ls -l  {} \;

Open in new window

You would have any examples that I can use?
Would appreciate any idea how.
Thank you very much
Regards
0
Comment
Question by:carlino70
[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
  • 3
  • 3
7 Comments
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 40467681
Wouldn't it be easier to remove the already scp'ed files from the relevant directory so you can transfer all new files at each run?
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 40467695
To list the files whose names correspond to the previous minute you can use this:

ls -t1 Transfer_data.$(date -d "1 minute ago" "+%Y%m%d%H%M")*

The above works only with GNU date (standard on Linux).

Please be aware that percent signs (%) used in crontab (not in scripts called by cron!) must be escaped by means of a backslash (\)
0
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 1000 total points
ID: 40467733
Again, your own script is not at all wrong but just needs corrections:

#!/bin/ksh
touch -t $(date --date "1 minute ago" "+%Y%m%d%H%M.00") start
# 1 minute backward only:
touch -t $(date --date "1 minute ago" "+%Y%m%d%H%M.59") end
# Current minute and 1 minute backward:
# touch -t $(date "+%Y%m%d%H%M.59") end
find . -name "Transfer_data*" -type f \( -newer start -a \! -newer end \) -print |sort -r

"ls -l" will give way too much info, so I used "-print" and "sort -r" to get the (by name) newest file on top.
0
Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

 
LVL 40

Assisted Solution

by:omarfarid
omarfarid earned 1000 total points
ID: 40468313
I would run something like:

mystart=start$$
myend=end$$
touch $mystart
sleep 60
touch $myend
find . -name "Transfer_data*" -type f \( -newer $mystart -a \! -newer $myend \) -print |sort -r

This should give files that were created in last minute
0
 

Author Comment

by:carlino70
ID: 40468712
If it should be easier, but I can not delete files from the original directory. Move them to a temporary folder and then transfer would be an option, but would require more time in the process.
0
 

Author Comment

by:carlino70
ID: 40468852
omarfarid, woolmilkporc:

I'll test both ideas.

Thanksyou
0
 

Author Closing Comment

by:carlino70
ID: 40469260
Thanks, both solutions work
0

Featured Post

Stressed Out?

Watch some penguins on the livecam!

Question has a verified solution.

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

SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
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 …
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses
Course of the Month7 days, 19 hours left to enroll

765 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