I wish to iterate over all directories where my bash script is stored and cd into each directory to then rename my filenames by substituting a substring for another. Once done renaming the file save the file to the same directory it was originally in.
Here is my script:
#!/bin/bash
# loop over all directories
for dir in `ls -d */`; do
# cd into directory and print directory -- Sanity check
( cd "$dir" && echo "$dir" );
# loop over files within directory
for file in $dir/*; do
echo $file;
# assign var 'fname' to file
fname1 = $file;
# assign variable 'fname2' to new filename, replace 'ops' for 'fcst_ops'
fname2=`echo $file | awk '{sub("ops","fcst_ops"); print $0}'` ;
# rename original filename to new filename(fname2) in same directory
mv $fname1 $fname2;
#echo "$(basename "$file")"; WORKS
echo $fname2;
done
done
Current issues: fname1: command not found && fname2 variable name is not correct.
I've tried assigning my fname1 variable in many ways but bash still throws an error.
I've also tried different methods to get the filename variable correctly but no luck.
This issue is a bit different than the others since I am iterating over directories and renaming the filename one file at a time w/r to each directory.
I wish to save each filename in its respective directory after substitution of 'ops' for 'fcst_ops'.