Skip to content

Bash recipes

Parameter expansion

Convert wav files to mp3

Remove the file extension using parameter expansion.

for file in *.wav; do
> ffmpeg -i "$file" -acodec mp3 "${file%.*}.mp3"
> done

Loop files

Set basic ID3 tags for mp3 files

t=1; for file in *.mp3; do
> eyeD3 -a "Patrice O'Neal" -A 'Mr. P' -N 17 -n $t -G Comedy "$file"
> ((t++))
> done

Unpack multiple zip archives into one dir each based on zip file name

In this example I unpack files in my current workin dir into /media/nas/satashare/Comedy as a dir based on zip file name.

for file in *.zip; do
> mkdir -v "/media/nas/satashare/Comedy/${file%.*}"
> unzip "$file" -d "/media/nas/satashare/Comedy/${file%.*}/"
> done

Random tricks

Transfer many files over ssh

Use tar to pipe multiple files over the network into a sub-directory called my-server.

ssh my-server 'cd /var/log/ \
> sudo tar -cvf - maillog*' | tar -xvf - -C my-server/

Last update: March 14, 2021