I have files in subdirectories A, B, C Each directory has files of various names, lets say 1.txt 2.txt 3.txt in each although the names are not relevant as such. I want to run a bash shell script to recurse into each so that the files in each are prefixed with the names of the holding directory so in this example they are renamed A_1.txt, A_2.txt A_3.txt B_1.txt etc.
I currently have
#!/bin/bash
for d in ./*/ ;
do cd "$d" ;
for f in "$arg" ;
do echo mv "$f" "${PWD##*/}""_""${f/_*_/_}";
done
done
but this is failing and I can't see why. Grateful for insights.
This version works for single named files with the argument passed to the shell script but of course has no recursion into the subdirectories although I can use it with the apply command for bulk file processing.
#!/bin/bash
for f in "$1" ;
do mv "$f" "${PWD##*/}""_""${f/_*_/_}";
done