Solved

Get files between two dates, Linux RedHat

Posted on 2014-11-26
7
335 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
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 
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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Linux Real Time Memory on Distinct 24 Cycle 7 42
PHP error function not working on AWS 10 117
I NEED A "BARE" LINUX ... 9 84
Check for Linux process in script 7 65
This is the error message I got (CODE) Error caused by incompatible libmp3lame 3.98-2 with ffmpeg I've googled this error message and found out sometimes it attaches this note "can be treated with downgrade libmp3lame to version 3.97 or 3.98" …
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…
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…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

680 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