Help batch rename folders with dates

Hi there, this is surely a trival request but I’m unable to find a regex or any other pattern to do this simple renaming.

I want to switch from rsync_tmbackup to linux_timemachine for my daily automated backups.

Both are (in my understanding) wrappers around rsync.

rsync_tmbackup.sh (what I currently use) creates folders with this naming convention: yyyy-mm-dd-hhmmss (A)

timemachine instead uses this convention: yyyy-mm-dd__hh-mm-ss (B)

Can anybody suggest a way to batch rename all these folders from convention A to B ?

For example 2020-03-03-001010 should become 2020-03-03__00-10-10

I’m on a Unix machine (Ubuntu 20.10) and I use Bash.

Ciao Alessandro!

How about using awk?
Here is one example:

Have fun!
Claes a Lund, Svezia

@aadm So… in your case it would be something like this…

pentoo@penTux ~ $ cat exp.awk 
echo 2014-08-05-234656 | awk 'BEGIN { FIELDWIDTHS = "10 1 2 2 2" } { printf "%s__%s-%s-%s\n", $1, $3, $4, $5 }'
pentoo@penTux ~ $ ./exp.awk 
2014-08-05__23-46-56

Which, of course, reminds me of this:

3 Likes

You could try something like this:

find . -depth -type d -execdir rename ‘s/-(\d{2})(\d{2})(\d{2})/__$1-$2-$3/’ {} \;

This looks for directories (-type d) from the directory you are standing in (the lone first dot). It gives what it finds to the rename command, which does the regexp magic and the renaming. The -depth and -execdir (instead of -exec) are needed in case it finds subdirectories.

Do give this a test in a safe location first. I did do a mini test just to make sure but better safe then sorry…

EDIT: I need to escape the escape otherwise it won’t show. Fixed.

1 Like

thanks Jacques and Claes!

In the end I’ve used tha last command with find+rename (had to install rename with sudo apt install… because it wasnt available on my system) and only had to change one small thing at the end:

find . -maxdepth 1 -type d -execdir rename -v 's/-(\d{2})(\d{2})(\d{2})/__$1-$2-$3/' {} \;

(I had to add \ before the final ;)

Have you looked at restic or Borg backup? Both are nice and offer some additional features over rsync solutions.

1 Like

I’ve heard of them… or quickly scanned through their readmes/docs… I dunno, to me the rsync-based solution are lightweight and effective; my needs are simple, to make regular backups of my home folder, the darktable folder with all the raws and the videos folder where I keep all family clips over ssh to the other pc I have in my house (a small intel nuc with a raid hd).

I will have a look at them however, thanks Mica, maybe I discover something better

1 Like