I have been using a simple shell script placed in my user/.bin folder; Its job is very simple, copy specified files $1 and rename to $2; at least I had thought it to be that simple ...
The situation of use is this, I have a file nomenclature for my programming notes. The system is simple, an augmenting number followed by a letter, the rest of the file name contains only letters and underscores. Notes for a certain topic are called: 16115P-foobar.md whilst the code files for that topic are: 16115P-foobar.c 16115P-barfoo.h etc.
As the program progresses with each lesson, I am copying all program files before modifying them; So as to have files of each step of the course. As it is required of me to modify the program using the lessons topic; the first thing I do is copy and rename the files. The script is supposed to duplicate each file and rename them, changing only the sequential number, which is of my standard nomenclature:
e.g. 16115P-foobar.c is to be changed to 17121P-foobar.c ... etc.
The new number is specified by the user (my self only) by way of the second command line parameter; not automated by the script.
#!/bin/bash
for filename in $1*
do
#
# Copy and rename files
#
newname=`echo "$filename" | tr "$1" "$2"`
echo $filename $newname
cp $filename $newname
done
When I parse my file names like so:
:~/blah/blah/code$ copy_and_rename 16115P 17121P
16115P-file.c 27221P-file.c
16115P-input.c 27221P-input.c
16115P-main.c 27221P-main.c
16115P-output.c 27221P-output.c
16115P-recordshop.h 27221P-recordshop.h
16115P-utils.c 27221P-utils.c
I see now that tr
is not the correct command to use here; I would appreciate some help.
Kind regards.
tr
transliterates individual characters from a set: if you're trying to do string replacement it's not the right tool for the job – steeldriver Jan 6 '16 at 14:55rename 's/old-name/new-name/' files
Should do the trick, at least I think so. I wonder can I use shell parameters in there? – iain Jan 6 '16 at 15:06