Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Get basename of files in folder

I want to compress image files and wrote the following bash script:

for i in *.png; do convert $i -quality 100% $i-comp.jpeg; done;

When I run it I get filenames like 48.png-comp.jpeg. I only want to have like 48-comp.jpeg, so basically removing the .png part of the filename.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I tried using this ${$i/.png/}, which gives me an error message.

Any suggestions how to do this?

>Solution :

${$i/.png/} is almost correct, but the second $ is not needed inside a Parameter Expansion.

#!/bin/bash

for i in *.png; do
  convert "$i" -quality 88% "${i/.png/}-comp.jpeg"
done

Note: ${i%.png} is commonly more used to remove a file extension than ${i/.png/}, but both should produce the same output.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading