Using bash                
Here is a small bash script. It changes file suffixes:
                
#! /bin/bash
old_suffix=$1
new_suffix=$2
for f in *.$old_suffix
do
new_name=${f%.*}.$new_suffix
echo Rename $f to $new_name
done
You might use it like this, to rename all of your C files
(ending in .c
) to be C++ files (ending in .cpp
):
                
./rn c cpp
For this lab, you should:
                
- Understand how the script works
- Understand why we said
./rn
as opposed to just rn
- Copy & paste this into your own file
rn
, and try it out
- Make it actually rename the file, rather than just saying so
- Add a usage message if too many or too few arguments are given
- Send the usage message to stderr
- Complain if a file can’t be renamed
- Not clobber existing files
For extra fame & glory, you could:
                
- Add a
-n
option that doesn’t rename,
but just says what it would do
- Add a
-v
option that renames verbosely
- Complain if no files have the given suffix
- Make it work for filenames and suffixes that contain spaces
- Make it work for suffixes that are wildcards (whatever that means)