문제

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
도움이 되었습니까?

해결책

The following should work in subdirectories:

for i in $(find . -type f); do dir="$(dirname ${i#./})"; mv "$i" "${i//DSC/$(basename $dir)}"; done

다른 팁

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 \;
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top