Global renaming of files

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi folks

I have some 1600 files where the first 7 characters of each file name is in
the format "CN NNN ". I would like to be able to change these via a program
to the format "CN-NNN-". The files are actually hymns and these 7 characters
are followed by the first line of the hymn.

Can anyone out there help, or do I have to go through manually making the
changes? I would appreciate any valid comments.

Thanks everybody
 
Jaymac said:
I have some 1600 files where the first 7 characters of each file name is in
the format "CN NNN ". I would like to be able to change these via a program
to the format "CN-NNN-". The files are actually hymns and these 7 characters
are followed by the first line of the hymn.

Can anyone out there help, or do I have to go through manually making the
changes? I would appreciate any valid comments.

I use a sed & mv script I call 'rename' for things like this, but it
works on Cygwin or other Unix-like systems. With it, I'd do this:

rename 's:\(..\) \(...\) :\1-\2-:' C*

.... but this may be impractical for you.

---8<--
#!/bin/bash
#
# Written by Barry Kelly, 19 January 2004
#

function syntax {
echo "Syntax:"
echo " $(basename $0) <sed-script> <file>+"
echo "Files are renamed to new names tranformed by the sed script."
echo "For example, the command:"
echo " $(basename $0) s/foo/bar/g"
echo "renames all files which contain 'foo' to contain 'bar'
instead."
}

if [ $# -lt 2 ]; then
syntax
exit 1
fi

case "$1" in -h | -H | --help)
syntax
exit
esac

sed_script="$1"
shift

function process {
local old_name="$1"
local new_name="$(echo "$1" | sed "$sed_script")"
if [ "$old_name" = "$new_name" ]; then
echo "Skipping $new_name, still the same."
return
fi
if test -f "$new_name"; then
mv -i -- "$old_name" "$new_name.tmp"
mv -i -- "$new_name.tmp" "$new_name"
else
mv -i -- "$old_name" "$new_name"
fi
}

while [ "$1" ];
do
process "$1"
shift
done
--->8---

-- Barry
 
Back
Top