LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Want to make a bash script that removes a png file if a jpg has the same name. (https://www.linuxquestions.org/questions/linux-newbie-8/want-to-make-a-bash-script-that-removes-a-png-file-if-a-jpg-has-the-same-name-4175412405/)

kebertx 06-20-2012 04:08 AM

Want to make a bash script that removes a png file if a jpg has the same name.
 
And I don't know quite what to do :confused:

I want to find all .png images in a directory, and remove them if there exists in the same directory a .jpg which has the same filename (not including extension).

It seemed that this was the right place to learn the tricks I need. Any tips?

jschiwal 06-20-2012 04:46 AM

Code:

for file in *.jpg; do
    [[ -f "${file%.jpg}.png" ]] && rm "${file%.jpg}.png"
done

This is something you may be able to do interactively in the shell when your BASH skills improve a bit.

The expression "${file%.jpg}.png" changes the extension to .png using name expansion.

kebertx 06-22-2012 12:37 AM

Yay thanks! My skills do need to improve, but the learning is the fun part anyways, so I don't mind that at all.


All times are GMT -5. The time now is 04:13 PM.