-1

My goal is to automate the following process: search within all second-level sub-directories, and for all files called "Test.pptx" in said sub-directories, rename to "Test - Appended.pptx". Based on responses I have seen to other questions on StackOverflow I have attempted the following code:

for D in *; do
        if [ -d "${D}" ]; then
                echo "${D}"
                for E in "${D}"; do
                        if [ -d "${E} ]; then
                                echo "${E}"
                                for f in "Test.pptx"; do mv "$f" "Test - Appended.pptx"; done
                        fi
                done
        fi
done

I set the script executable (using chmod +x) and run it, but get the following errors:

line 7: unexpected EOF while looking for matching `"'
line 12: syntax error: unexpected end of file

I am a relative newcomer to Bash scripts, so I would appreciate any help in diagnosing the errors and achieving the initial goal. Thank you!

Cyrus
  • 84,225
  • 14
  • 89
  • 153
  • 2
    Please add a shebang and then paste your script at http://www.shellcheck.net/ and try to implement the recommendations made there. – Cyrus Aug 28 '20 at 15:35
  • Does this answer your question? [Find multiple files and rename them in Linux](https://stackoverflow.com/questions/16541582/find-multiple-files-and-rename-them-in-linux) – Inder Aug 28 '20 at 15:51

2 Answers2

0

use find:

while read -r pptx
do
    mv -n "${pptx}" "${pptx%.pptx} - Appended.pptx"
done < <( find . -mindepth 3 -maxdepth 3 -type f -name "*.pptx" )

Be aware, that I did not test it and it might need adaptions on your special case. As long as the -n option is set in mv it will not overwrite anything.

Marco
  • 824
  • 9
  • 13
0

sub-loops not really needed.

for f in */*/Test.pptx; do mv "$f" "${f%/*}/Test - Appended.pptx"; done

${f%/*} is the current file's full path with everything from the last slash (/) forward stripped off, so if the file is a/b/Test.pptx then ${f%/*} is a/b.

Paul Hodges
  • 13,382
  • 1
  • 17
  • 36