Neat things with find, LAME and sed in Bash

Just want to¬†chronicle¬†l some really neat, time saving tricks I’ve picked up today for handling files in bash.

The first, how to use the find command:

 find -type f -iname "*.txt"

Easy enough, right?

Let’s use find and cp to back up a bunch of text files:

 find . -name "*.txt" -execdir cp {} a_copy.txt  \;

NOTE: the above assumes that there is only one text file in each directory, and will create a file in the same directory being a copy of whatever file it is that the find command returns.

Let’s string find to sed, and use sed to replace all instances of “.ogg” to “.mp3” within a collection of text files:

 find . -name "*.txt" -exec sed -i "s/.ogg/.mp3/g" '{}' \;

Let’s also use find and LAME together to convert a batch of wav files to mp3 files (I recommend you run the following from within a screen session):

  find -type f -name "*.wav" -execdir /usr/bin/lame -V3 {} \;

NOTE: The above will create mp3 files in the form of “somefile.wav.mp3” in the same directory that “somefile.wav” is located.

Let’s use find to delete the remaining wave format files:

 find -type f -iname "*.wav" -delete

The above commands can save hours of messing around with a file manager.

Leave a Reply

*

Contact Nixz Kerr