Want to make txt files for every png in the folder

You can remove the existing extension using the shell's parameter expansion features

${parameter%pattern} The 'pattern' is matched against the end of 'parameter'. The result is the expanded value of 'parameter' with the shortest match deleted.

So in your case, replace $filePng.txt with "${filePng%.png}.txt"


You can use the basename command here:

touch "$folder/$(basename "$filePng" .png).txt"

Note the additional $folder/. This is neccessary since the basename command removes the path from.


With variation on what steeldriver already mentioned - parameter expansion - we can use string replacement to do the job. Additionally, you should quote variables. Below is your edited script.

#!/bin/bash

folder='/home/data/mnist/training'

for filePng in "$folder"/*
do
    touch "${filePng/.png/.txt}"
done

Tags:

Bash

Scripts