Frage

I got help regarding the following question: batch rename files with ids intact

It's a great example of how to rename specific files in a group, but I am wondering if there is a similar script I could use to do the following:

  1. I have a group of nested folders and files within a root directory that contain [myprefix_foldername] and [myprefix_filename.ext]
  2. I would like to rename all of the folders and files to [foldername] and [filename.ext]

Can I use a similar methodology to what is found in the post above?

Thanks! jml

War es hilfreich?

Lösung

Yes, quite easily, with find.

find rootDir -name "myprefix_*"

This will give you a list of all files and folders in rootDir that start with myprefix_. From there, it's a short jump to a batch rename:

find rootDir -name "myprefix_*" | while read f
do
  echo "Moving $f to ${f/myprefix_/}"
  mv "$f" "${f/myprefix_/}"
done

EDIT: IFS added per http://www.cyberciti.biz/tips/handling-filenames-with-spaces-in-bash.html

EDIT 2: IFS removed in favor of while read.

EDIT 3: As bos points out, you may need to change while read f to while read -d $'\n' f if your version of Bash still doesn't like it.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top