Bash - techniq/wiki GitHub Wiki

Bash Handbook

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

Run same command over multiple ssh hosts

for ip in $(seq 10 13); do ssh [email protected].$ip -i ansible/.ssh/id_rsa docker ps; done

Ping until hostname resolves

while ! ping -c 1 some_hostname.local; do echo -; done;

Say "Internet is down" if unable to ping Google DNS

while true; do ping -t1 -c1 8.8.8.8 || say Internet is down; sleep 1; done

Find out what shell you are currently running

    echo $0

Pass output of command to another as argument

  • using backtick (`)
    somecommand `anothercommand`
  • using $()
    somecommand $(anothercommand)
  • using xargs
    anothercommand | xargs somecommand