Update: Change to your Favorite directories on the bash shell by scripting ‘cd’

I’m sick and tired of having to remember and ‘cd’ long paths.
I decided to create my own console based Favorites-script which makes use of a definition file which is easy to change.
This post updates http://www.petur.eu/blog/?p=175
“cd /home/petur/Documents/school/2010/fall/chemistry” becomes “cdf chemistry”
and “cd /var/log” becomes “cdf log”
Demonstration video:

The setup process is pretty straight forward, here we go (single user setup):
Append the following to:
~/.bashrc file (single user setup)
or
/etc/bash.bashrc (for all users)

function cdf() {
#
# Pétur Ingi Egilsson ( petur <@> petur.eu )
# http://www.petur.eu/blog/?p=175
#
# Stuart Rackham ( srackham <@> gmail.com )
#
# cdf (Change to favorites)
local aliasfile fullpath
aliasfile=~/.cdfrc
if [ $# -eq 0 ]
then
echo Usage: cdf FAVORITE
return 1
fi
fullpath=$(grep $1, $aliasfile|cut -d, -f2)
fullpath=$(eval "echo -n $fullpath")
if [ ${#fullpath} -ne 0 ]
then
cd $fullpath
else
echo "Error: '$1' has not been defined in $aliasfile"
echo -n "Do you want to edit the file? (y/n): "
read editFile
case $editFile in
[yY])
if [ ! -n "$EDITOR" ]
then
# Use the nano editor because
# the EDITOR env has not been set.
nano $aliasfile
else
$EDITOR $aliasfile
fi
;;
[nN])
;;
*)
echo "Please use y,Y,n or N."
exit 1
esac
fi
}

Save the following file as ~/.cdfrc

# Definition file for cdf
#
# format: name,/path/to/directoryetc,/etc
logs,/var/logs
mnt,/mnt
root,/root
var,/var

Do you have any comments?

[ad#Banner]

Execute commands on a firewalled remote machine using Dropbox

I use dropbox to backup data offsite and sync it between my other computers.
THIS LINK points to a video explaining how dropbox it works.
One of those machines is my personal computer at campus.
It’s behind NAT/firewall so remote access is out of the question.
I figured I could use dropbox to execute commands on it remotely, so I made a bash script for the job.

#!/bin/bash
#
# Pétur Ingi Egilsson #
INPUTDIR=/home/petur/Dropbox/petur-laptop/input/
OUTPUTFILE=/home/petur/Dropbox/petur-laptop/output
for i in `ls $INPUTDIR`;
do
chmod +x $INPUTDIR$i;
echo $i - `date` >> $OUTPUTFILE;
/bin/bash $INPUTDIR$i >> $OUTPUTFILE;
rm $INPUTDIR$i
done

It executes all the bash scripts uploaded to the /home/petur/Dropbox/petur-laptop/input/ directory, be it a single line or a complex script and redirects the output to the /home/petur/Dropbox/petur-laptop/output file and then deletes the script so it won’t get executed again.
Add the script to crontab and make it run every minute or so.

Sending emails with Dropbox using mutt

Move any file into the ~/Dropbox/muttjobs/ folder and name it recipient@server.xyz{filename and the file will be emailed to the recipient.
On the ‘server’ side have this script running as a cronjob every 5 minutes.
Ps. whitespaces are not allowed in the filename and mutt must ofcourse be properly configured.

#!/bin/bash
MUTTJOBS="/home/petur/Dropbox/muttjobs/"
MUTTTMP="/tmp/"
for i in `ls $MUTTJOBS`;
do
recipient=`echo $i|cut -d{ -f1`;
data=`echo $i|cut -d{ -f2`;
mv $MUTTJOBS$i $MUTTTMP$data;
echo "sent via dropbox"|/usr/bin/mutt $recipient -s "filename: $data" -a $MUTTTMP$data;
rm $MUTTTMP$data;
done;

Download YouTube Music Videos to MP3 files on Linux

youtube2mp3 downloads YouTube videos and saves them as .mp3 files.
Recommended The .deb will automatically solve dependencies and place a launcher under Sounds&Video
Download for Ubuntu (.deb)

Advanced The script can also be downloaded and launched from the command line, make sure you manually solve the dependencies.
Download for Linux (bash)

After installing, the program can be found under “Applications –> Sounds & Video –> Youtube 2 MP3”
After the application has been launched the following window will appear.
Simply copy the complete URL of the video you want and paste it into the textbox.



In this step you must choose where to save the mp3 file.
I’ll call mine song-name.mp3



Now the program will launch the download process in the background.
This can take a few minutes depending on the speed of your connection.



After a while this window should appear and your new .mp3 is in place.





If for some reasons the program fails, launch it from the command line and paste the output here as a comment or email it to me:  petur \at\ petur.eu.
Applications –> Terminal.
Type in “youtube2mp3” without the quotation marks and hit the enter key.