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.
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*
The command-line utility rename
comes in many flavors. Most of them should work for this task. I use this: rename
version 1.601 by Aristotle Pagaltzis (docs). To install rename
, simply download its Perl script and place into $PATH
. Or install rename
using conda
, like so:
conda install rename