The following should work in subdirectories:
for i in $(find . -type f); do dir="$(dirname ${i#./})"; mv "$i" "${i//DSC/$(basename $dir)}"; done
Question
I have a script that changes the file name. It takes a directory name and added to the file name. By the way, using tr replaces a string:
0004 the name of the directory (this directory is a script) - DSC_1234.jpg
result 0004_1234.jpg
The script works if I am in a particular directory. I wanted to change a name yet in subdirectories
#!/bin/bash
CURRENT=`pwd`
BASENAME=`basename $CURRENT`
echo $BASENAME
for i in ./*DSC*;do mv -- "$i" "${i//DSC/$BASENAME}";done
La solution
The following should work in subdirectories:
for i in $(find . -type f); do dir="$(dirname ${i#./})"; mv "$i" "${i//DSC/$(basename $dir)}"; done
Autres conseils
You probably want to use find
on your bash script (call that script your_script.sh
):
find $ROOT_DIR -type d -exec your_script.sh \;