I have folders inside a directory with names having the pattern DD.MM.YYYY-X
where X
refers to an index from 0-9 to distinguish folders with names having the same date.
How could I use regex on bash to replace this pattern with YYMMDDIX
where
I
is an actual I
to signal that what follows is the index of the folderYY
is the last two numbers in YYYY
DD
and MM
are the same as in the original nameRunning this script in the same directory containing folders with names having the pattern DD.MM.YYYY-X
will copy those folders in the same directory with naming syntax you requested.
script2.sh
file containing the following#!/bin/bash
for dir in $(find . -maxdepth 1 -name "*-*" -type d -exec basename {} \;) ;do
dd=$(cut -d'.' -f1 <<< "${dir}")
mm=$(cut -d'.' -f2 <<< "${dir}")
yyyy=$(cut -d'.' -f3 <<< "${dir}" | cut -d'-' -f1)
yy="${yyyy: -2}"
x="${dir: -1}"
cp -rvi "${dir}" "${yy}${mm}${dd}I${x}"
done
exit 0
'22.12.1983-1' -> '831222I1'
'22.12.1982-1' -> '821222I1'
'22.12.1983-0' -> '831222I0'
'22.12.1982-2' -> '821222I2'
ls
output after running script22.12.1982-1 22.12.1982-2 22.12.1983-0 22.12.1983-1 821222I1 821222I2 831222I0 831222I1 script2.sh
It is recommended to update and use unique variable names. Like with a _
prefix.
Here dd=
variable can be changed to _dd=...
to avoid conflicting/confusing with the dd
command.