Renaming multiple directories while keeping a part in the middle with varying suffix

Viewed 36

I'm trying to change the name of multiple directories using bash, the names being structures like the following:

DRMAD_CA-12__MRBK01_237a8430 DRMAD_CA-17__MRBK10_766c3396
DRMAD_CA-103__MRBK100_c27a6c1c

The goal is the to keep the MRBK as well as the number following directly after it (MRBK###), but to get rid of of the rest. The pattern of the prefix is always the same (DRMAD_CA-###__), while the suffix is '_' followed by a combination of exactly 8 letters and digits. Tried sed, but can't seem to figure out the right pattern.

Seeing other posts on Stackoverflow, I've tired variations of

ls | while read file; do new=$( echo $file | sed 's/[^0-9]*\([^ ]*\)[^.]*\(\..*\)*MRBK\1\2/' ) mv "$file" "$new" done

But since I don't really understand the syntax of sed, it doesn't produce a usable result.

2 Answers

Use rename utility.
First, print the old and new names, but do not rename anything:

rename --dry-run 's/.*(MRBK\d+).*/$1/' *MRBK*

If OK, actually rename:

rename 's/.*(MRBK\d+).*/$1/' *MRBK*

Install rename, for example, using conda.

Using find:

find . -type d -regextype posix-extended -regex "^.*MRBK[[:digit:]]+.*$" | while read line
do 
     dir=$(dirname $line)
     newfil=$(grep -Eo 'MRBK[[:digit:]]+' <<< $line)
     mv "$line" "$dir/$newfil"
done
Related