Skip to content

Instantly share code, notes, and snippets.

@ccampo133
Last active August 8, 2019 03:24
Show Gist options
  • Save ccampo133/01e5d4f1251b8a4f8389ec6f8e759c74 to your computer and use it in GitHub Desktop.
Save ccampo133/01e5d4f1251b8a4f8389ec6f8e759c74 to your computer and use it in GitHub Desktop.
Rename the prefix of a bunch of files
#!/usr/bin/env sh
# Usage:
# prefmv frompref topref files...
#
# Changes the prefx from `frompref` to `topref`. Adds prefix `topref` if the file
# didn't have the prefix `frompref`
#
# Examples:
# prefmv main oldmain main.c main.java main.cpp
# prefmv main oldmain main*
#
# It's best to use wildcards involving fromext for the file list, to be
# sure not to rename files lacking the extension, if you don't want that.
frompref=$1
shift
topref=$1
shift
files=$*
frompref=`echo $frompref | sed 's/\./\\\./'`
for i in $files; do
j=`echo $i | sed s/\^${frompref}//`
mv $i $topref$j
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment