Play Music In The Terminal With A Bash Script

Whether you do a lot of work in the Terminal or you just don't feel like opening iTunes and searching through your library, reader CamJN's fplay script will get you listening to music in seconds.

The point of the fplay function is that you can play music without having to open iTunes, know where a song is or even the whole name.

To install the function, cut and paste the following code into a text editor and save the file to your desktop as fplay.txt. Make sure it is plain text and not rich text; Mac users will have to go to Format > Make Plain Text in Text Edit to do so. Also, if your music is stored somewhere other than the default location, you'll need to edit the path next to the MUSICROOT variable below.

MUSICROOT=~/Music

function fplay {

if [ $US1 = '-v' ] ; then

shift 1

find -E $MUSICROOT -type f -iname "*$**" -iregex '.*.(3g[2|p] |a(ac|c3|dts|if[c|f] ?|mr|nd|u)|caf|m4[a|r] |mp([1-4|a] |eg[0,9] ?)|sd2|wav)' -print -exec afplay "{}" ; &

elif [ $US1 = '-g' ] ; then

shift 1

find -E $MUSICROOT -type f -iname "*$**" -iregex '.*.(3g[2|p] |a(ac|c3|dts|if[c|f] ?|mr|nd|u)|caf|m4[a|r] |mp([1-4|a] |eg[0,9] ?)|sd2|wav)' -exec open -a QuickTime Player "{}" ; -exec osascript -e 'tell application "QuickTime Player" to play document 1' ; & else

find -E $MUSICROOT -type f -iname "*$**" -iregex '.*.(3g[2|p] |a(ac|c3|dts|if[c|f] ?|mr|nd|u)|caf|m4[a|r] |mp([1-4|a] |eg[0,9] ?)|sd2|wav)' -exec afplay "{}" ; &

fi

}

Then, open up a new Terminal window and run this command to feed this to your bash profile:

cat ~/Desktop/fplay.txt ~/.bash_profile

After restarting Terminal, you should be able to type fplay and some search terms to play music. You can make it play in QuickTime X instead of the Terminal by using the -g flag, i.e. fplay -g car crash to play the song "Car Crash". Check out the video for a demonstration.


Comments

Be the first to comment on this story!

Trending Stories Right Now