On my Linux (Ubuntu) I have a very simple backup script, essentially:

rsync <params> dir_src_a dir_dest_b
rsync <params> dir_src_aa dir_dest_bb
rsync <params> dir_src_aaa dir_dest_bbb

What I'd like to do, is before each rsync runs, store the current date/time in a variable (obviously a different var for each rsync) then when the last rsync has finished, print up on screen something like:

rsync 1 started at startdate
rsync 2 started at startdate
rsync 3 started at startdate

I have zero scripting skills so any guidance would be appreciated. The script currently is just in standard bash and on Ubuntu 18.04, doubt that matters though. Thanks!

  • Zero scripting skills, learn scripting: tldp.org/guides.html -> "Bash Guide for Beginners" and then "Advanced Bash-Scripting Guide" - as you grasp the very basics you will be scripting in no time. – Hannu Sep 21 '18 at 17:52
  • I don't have a need to write very many scripts. This is simply a time-saver so I don't have to run several different-but-similar commands whenever I do backups. Telling me to "go learn how to script" doesn't really help. – Phil Rigby Sep 21 '18 at 18:16
  • That is a resource pointer, nothing else. ;-) With just a tiny bit of effort you'll be creating your own special scripts. – Hannu Sep 24 '18 at 21:24

See the date command. You can put the current date/time in a string like this:


Or restrict that to just the time:

now=$(date +%H:%M:%S)

In a script you put these values in an array:


# declare array
set -a times

# Loop over the directories
for d in dir1 dir2 dir3
  # Add the current time at the end of the array
  times[${#times[*]}]=$(date +%H:%M:%S)
  # Perform rsync (replace next two by actual code)
  echo rsync $d
  sleep 2

# Loop over array of start times, "${!times[@]}" produces a 0-based sequence of indices over the array
for i in "${!times[@]}"; 
  # Print the time ( "$((i+1))" increments the number because unlike computers, humans counts from 1)
  printf "Rsync of dir #%s started at %s\n" "$((i+1))" "${times[$i]}"

There are many options to format the date. If you go further in your scripting, the most useful one is %s that gives "seconds since the Epoch", which is very useful (and really the only safe way) to compute durations.

  • Thanks, that seems the way to go... not bothered about computing the durations, I can do that mentally by looking at the times. As @KamilMaciorowski mentioned, the steps needed to store/print this would be awesome! – Phil Rigby Sep 21 '18 at 16:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.