0

Okay so this shell script is might sound complicated but I don't know how to achieve this.

so here is the requirement.

enter image description here

note the above files kindly.

filname are as follows.

Meter_20200120100846.xls
Meter_20200121100846.xls
Meter_20200122100846.xls
Meter_20200123100846.xls
Meter_20200124100846.xls
Meter_20200125100846.xls

So I want to create such a script that copy these files to another server. but here's the twist the number in the end is the date of that file. so 20200120 in the file 1 means it was created on 20/01/2020.

so basically i want to created such a script that takes the input as 20/01/2020 or 20200120 to 25/01/2020 or 20200125 and copy all files to another server with same name.

I can use anything to copy scp, sftp or rsync in the command.

basically creating such a script that take from date to end date and copy all the files to another server.

To be honest I don't know if it's even possible to do that.

but guys if you are able to know such solution then it would really help me out.

Thanks and Regards, Sagar Mandal

2
  • 1
    Please don't post images of text. Instead, paste the text directly into your question and use the formatting tools to format it as code. It would make our life much easier if we could simply copy your file names instead of having to type the names letter by letter looking at your image.
    – terdon
    Apr 8, 2020 at 15:17
  • Hey man, I just added filename.and it's gonna be an automation type of script so yeah.. help me out in this.
    – user404480
    Apr 8, 2020 at 15:22

2 Answers 2

1

If I understand you correctly:

# Here you set the range of dates
from="20200121" 
to="20200125"
for f in *.xls; do 
  [[ $(date -d "${f:6:8}" +%s) -ge $(date -d "$from" +%s) ]] &&
  [[ $(date -d "${f:6:8}" +%s) -le $(date -d "$to" +%s) ]] &&
  echo "$f in range" || # Here your actions
  echo "$f not in range" 
done

Output:

Meter_20200120100846.xls not in range
Meter_20200121100846.xls in range
Meter_20200122100846.xls in range
Meter_20200123100846.xls in range
Meter_20200124100846.xls in range
Meter_20200125100846.xls in range
11
  • Bro the number after 202001120 is dynamic it can be changed
    – user404480
    Apr 8, 2020 at 15:50
  • @SagarMandal Sorry, I don't understand. In my code the names of the files are not changed (copying them from your post). Apr 8, 2020 at 15:53
  • so your saying that it doesn't matter if the first file is named as Meter_20200121100846.xls and second file is Meter_20200124091834.xls, so will your code work with this file too.
    – user404480
    Apr 8, 2020 at 15:56
  • My code takes Meter_<20200125>100846.xls, as I understand the date (20200125), the rest is ignored. Apr 8, 2020 at 16:01
  • Thnks man really, I'll test this and let you know for sure. so what would you suggest, what is the easiest way to send files to a remote server
    – user404480
    Apr 8, 2020 at 16:03
0

date command is what you are looking for

$ date -d 1/25/2020 +%Y%m%d
20200125

-d mm/dd/yyyy - give the input date to the command.

+%Y%m%d - what you want your output to be.

  • %Y - year in four digits
  • %m - month in two digits
  • %d - date of the month in two digits.
1
  • but how will you pick files from "this date" to "this date" and then copy them to another server.
    – user404480
    Apr 8, 2020 at 15:08

You must log in to answer this question.