Solved

Get files between two dates, Linux RedHat

Posted on 2014-11-26
7
329 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
  • 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 250 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
Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

 
LVL 40

Assisted Solution

by:omarfarid
omarfarid earned 250 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

Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

Suggested Solutions

Currently, there is not an RPM package available under the RHEL/Fedora/CentOS distributions that gives you a quick and easy way to allow PHP to interface with Oracle. As a result, I have included a set of instructions on how to do this with minimal …
Over the last ten+ years I have seen Linux configuration tools come and go. In the early days there was the tried-and-true, all-powerful linuxconf that many thought would remain the one and only Linux configuration tool until the end of times. Well,…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

747 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now