Share your knowledge at the LQ Wiki.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 06-20-2012, 04:08 AM   #1
LQ Newbie
Registered: Jun 2012
Posts: 3

Rep: Reputation: Disabled
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

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?
Old 06-20-2012, 04:46 AM   #2
LQ Guru
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
for file in *.jpg; do
    [[ -f "${file%.jpg}.png" ]] && rm "${file%.jpg}.png"
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.

Last edited by jschiwal; 06-20-2012 at 04:47 AM.
Old 06-22-2012, 12:37 AM   #3
LQ Newbie
Registered: Jun 2012
Posts: 3

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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Problem-Simple bash script to copy the latest .jpg file RCCollins Linux - Software 6 11-24-2011 03:24 PM
Program to determine if a file is a valid image (jpg, png, gif, etc.) KDHofAvalon Linux - Software 7 06-09-2009 05:54 PM
BASH script that removes sub-directories (not just files) but keeps the two newest. kennc Linux - Newbie 2 04-23-2009 02:09 PM
jpg files has no preview in file tips, png does. Acidx Linux - Desktop 1 10-15-2006 02:52 PM
pnp to png or jpg illtbagu Linux - Newbie 1 01-26-2003 04:08 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:14 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration