I am writing a script to rename my files, The current syntax for filename is program 1.c
and I want to change this to cprogram-program1.c
.
I am unable to get this format. Few conditions for simplicity are:
- assume that there are only 10 files i.e: 0,1,2,3..9
My procedure to achieve this:
First we need to filter the files to find out files that follow the given pattern
Then we need to filter the filename into substrings ignorer to verify whether they are in a proper format
If they are in a proper format, then we can edit the filename in the way we need it.
I am stuck on step 1 and 2, this is sort of what I am doing:
for fn in *; do --> this loops over the files program=${fn:0:7} number=${..} --> number between 0 and 9 extension=${..} --> gets file extension like .c if [ "$program" == "program" ] then if [ "$number" -ge 0 ] && [ "$number" -le 9 ] then if [ "$extension" == ".c" ] then --> edit code fi fi fi done
My problem: I have tried many permutations of the above example, but I am unable to get what I want. Any help, simple method of doing this would be highly appreciated.
Thanks
program 1.c
?for file in *.c; do [[( -f $file && $file =~ program )]] && echo cprogram-"${file/ /}"; done