Skip to content

Instantly share code, notes, and snippets.

@techniq
Last active August 29, 2015 14:22
Show Gist options
  • Save techniq/623ddac81838a4b8a368 to your computer and use it in GitHub Desktop.
Save techniq/623ddac81838a4b8a368 to your computer and use it in GitHub Desktop.
Bash tips

Bash tips

Strip extension (source)

Example: foo.png => foo

for file in *.png; do
    echo $(basename $file .png)
done;

or

for file in *.png; do
    echo ${file%%.*}
done;

example

for file in *.png; do
    convert -flatten $file ${file%%.*}-flatten.png
done;

Extract extension from filenames (source)

Example: foo.png => png

for file in *.png; do
    echo "${file##*.}"
done;

Ignore files with a specific extension

for file in *; do
    [ "${file##*.}" != "txt" ] && echo $file
done

http://stackoverflow.com/questions/965053/extract-filename-and-extension-in-bash

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment