简体   繁体   中英

Get folder name and rename file name tr Linux bash

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

以下内容适用于子目录:

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

您可能希望在bash脚本上使用find (调用该脚本your_script.sh ):

find $ROOT_DIR -type d -exec your_script.sh \;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM