Home > Net >  How to output to the original directory when renaming recursively?
How to output to the original directory when renaming recursively?

Time:02-02

dirs example:


./lv1/lvN/*.json

./lv1/*.json

demo.sh


Here's my script:


# ...

# Get all paths recursively

# exclude dir

find . -name *.json > json_list

# Read line by line, rename and output to the original directory

while read -r line_content; do

    mv "${line_content}" ${RANDOM}.json # How do I export to the original directory?

done < json_list

# ...


How do I export to the original directory?

Tried IFS or AWK to split the path into two fields, but is there a better way?

CodePudding user response:

I think you may be looking for

mv "${line_content}" "${line_content%/*}/${RANDOM}.json"

See Removing part of a string (BashFAQ/100 (How do I do string manipulation in bash?)) for an explanation of ${line_content%/*}.

  • Related