Archive for the ‘BASH’ Category

Over the weekend I got into some bash functions.

Paste these into your .bashrc file

1. Command line wiki query
function wiki () {

COLUMNS=`tput cols`

dig +short txt “`echo $@`”.wp.dg.cx | sed -e ‘s/” “//g’ -e ‘s/^”//g’ -e ‘s/”$//g’ -e ‘s/ http:/\n\nhttp:/’ | fmt -w $COLUMNS


2. Disk Usage with human readable formatting
#disk usage formatted
function duf {
du -sk "$@" | sort -n | while read size fname; do for unit in k M G T P E Z Y; do if [ $size -lt 1024 ]; then echo -e "${size}${unit}\t${fname}"; break; fi; size=$((size/1024)); done; done

3. Directory sizes
#dirsize - finds directory sizes and lists them for the current directory
dirsize () {
du -shx * .[a-zA-Z0-9_]* 2> /dev/null | \
egrep '^ *[0-9.]*[MG]' | sort -n > /tmp/list
egrep '^ *[0-9.]*M' /tmp/list
egrep '^ *[0-9.]*G' /tmp/list
rm -rf /tmp/list

4. Power extract function
extract () {
if [ -f $1 ] ; then
case $1 in
*.tar.bz2)   tar xjf $1        ;;
*.tar.gz)    tar xzf $1     ;;
*.bz2)       bunzip2 $1       ;;
*.rar)       rar x $1     ;;
*.gz)        gunzip $1     ;;
*.tar)       tar xf $1        ;;
*.tbz2)      tar xjf $1      ;;
*.tgz)       tar xzf $1       ;;
*.zip)       unzip $1     ;;
*.Z)         uncompress $1  ;;
*.7z)        7z x $1    ;;
*)           echo "'$1' cannot be extracted via extract()" ;;
echo "'$1' is not a valid file"

6. Network Information
#netinfo - shows network information for your system
netinfo ()
echo "--------------- Network Information ---------------"
/sbin/ifconfig | awk /'inet addr/ {print $2}'
/sbin/ifconfig | awk /'Bcast/ {print $3}'
/sbin/ifconfig | awk /'inet addr/ {print $4}'
/sbin/ifconfig | awk /'HWaddr/ {print $4,$5}'
myip=`lynx -dump -hiddenlinks=ignore -nolist http://checkip.dyndns.org:8245/ | sed '/^$/d; s/^[ ]*//g; s/[ ]*$//g' `
echo "${myip}"
echo "---------------------------------------------------"

7. Translate from commandline
#Translate a Word  - USAGE: translate house spanish  # See dictionary.com for available languages (there are many).
translate ()
TRANSLATED=`lynx -dump "http://dictionary.reference.com/browse/${1}" | grep -i -m 1 -w "${2}:" | sed 's/^[ \t]*//;s/[ \t]*$//'`
if [[ ${#TRANSLATED} != 0 ]] ;then
echo "\"${1}\" in ${TRANSLATED}"
echo "Sorry, I can not translate \"${1}\" to ${2}"

8. Define a word
# Define a word - USAGE: define dog
define ()
lynx -dump "http://www.google.com/search?hl=en&q=define%3A+${1}&btnG=Google+Search" | grep -m 3 -w "*"  | sed 's/;/ -/g' | cut -d- -f1 > /tmp/templookup.txt
if [[ -s  /tmp/templookup.txt ]] ;then
until ! read response
echo "${response}"
done < /tmp/templookup.txt
echo "Sorry $USER, I can't find the term \"${1} \""
\rm -f /tmp/templookup.txt

9. Simple Clock
# clock - A bash clock that can run in your terminal window.
clock ()
while true;do clear;echo "===========";date +"%r";echo "===========";sleep 1;done

MORE functions can be found here

Read Full Post »

The cool cd terminal hack

Method One: Navigate up the directory using “..n”

In the example below, ..4 is used to go up 4 directory level, ..3 to go up 3 directory level, ..2 to go up 2 directory level.

Add the following alias to your ~/.bashrc and re-login.

alias ..=”cd ..”

alias ..2=”cd ../..”

alias ..3=”cd ../../..”

alias ..4=”cd ../../../..”

alias ..5=”cd ../../../../..”

Method Two: Navigate up the directory using only dots

In the example below, ….. (five dots) is used to go up 4 directory level. Typing 5 dots to go up 4 directory structure is really easy to remember, as when you type the first two dots, you are thinking “going up one directory”, after that every additional dot, is to go one level up. So, use …. (four dots) to go up 3 directory level and .. (two dots) to go up 1 directory level.

Add the following alias to your ~/.bashrc and re-login for the ….. (five dots) to work properly.

alias ..=”cd ..”

alias …=”cd ../..”

alias ….=”cd ../../..”

alias …..=”cd ../../../..”

alias ……=”cd ../../../../..”

THANKS: Linux Tips & The Geek Stuff

(For two other methods, check out The Geek Stuff, and while you at it, read the best posting yet on RTFM)

Read Full Post »

If you want a a cow telling your fortune in your bash prompt

sudo apt-get install cowsay fortune
sudo gedit .bashrc

ADD this line:

COWDIR=/usr/share/cowsay/cows/; COWNUM=$(($RANDOM%$(ls $COWDIR | wc -l))); COWFILE=$(ls $COWDIR | sed -n ''$COWNUM'p'); fortune | cowsay -f $COWFILE

save & refresh your bash prompt by typing $bash

THANKS: Tyler Mulligan

Read Full Post »

So much nicer with ionice

Use ionice to give different processes more priority on reading and writing to your file system. A good example is the Transmission torrent client, which I prefer to run in the background most of the time. You have to get the Process ID (PID) by using the System Monitor, which can be ran from the Administration menu or by pressing ALT+F2 gnome-system-monitor. Go to the process list and find the name of the program (transmission in this example), copy the PID, and then press ALT+F2 gksudo ionice -c 3 -p 1234 where 1234 is instead the PID. I used 3 here to make it use Idle scheduling, but you can use 1, 2, and 3 for varying degrees. See man ionice for how that works in detail.

from: http://santiance.com/tag/ubuntu/

Read Full Post »

Open ~/.bashrc with your favourite editor.

gedit ~/.bashrc

Place the following to the end:

function wiki () {

COLUMNS=`tput cols`

dig +short txt "`echo $@`".wp.dg.cx | sed -e 's/" "//g' -e 's/^"//g' -e 's/"$//g' -e 's/ http:/\n\nhttp:/' | fmt -w $COLUMNS


Reset your bash terminal: $bash

To invoke type wiki <subject-of-your-query>. It will return some interesting and useful information.

Read Full Post »

I have been looking for a way to favourite my commands. Turning on Bash Smart autocompletion is a start. I found this posting at Ubuntu Blog:

The Bash shell has this sweet feature where you can use the TAB key to auto-complete certain things. For example, when I am in my home directory, the following command:

$cd Do[TAB-key]

will automatically yield:
$cd Documents

If you are an absolute novice, like I was, not so long ago, discovering tab completion in the terminal can make you go “Wow!”. Wait till you hear the rest now 🙂

Though you can use the TAB key to complete the names of files and directories, by default the completion is pretty “dumb”. If you have already typed $cd D you would expect that the tab key would cause only the directory names to be completed, but if I try it on my machine, the tab completion tool uses filenames too.

Now, don’t despair! There is now a smart bash tab completion trick you can use. Smart completion even complete the arguments to commands!!

To enable smart completion, edit your /etc/bash.bashrc file. Uncomment the following lines, by removing the # in the beginning of the lines:

#if [ -f /etc/bash_completion ]; then
# . /etc/bash_completion

Now you can use tab completion to power your way through commands.

You can even extend bash smart completion to your own favourite commands by using /etc/bash_completion, the “complete” utility and /etc/bash_completion.d . Explaining the nitty-gritty is beyond me. I refer you to the Debian Administration gurus for more information regarding smarter bash completion.

Thanks Carthik, I do believe this is the solution. First turn on smart autocompletion, check to see if your favourite commands autocomplete, and if not, then create a file containing these commands using the link provided.

UPDATE: Working with history in Bash

Read Full Post »

After struggling with a number of consoles, all of which claimed to provide the drop-down “Quake” experience, I found Yeah Console, which does exactly what it promises, nothing more, nothing less. I was pleasantly surprised, even though the installation details are not what one could call smooth sailing. If you want to grab the console, you’ll have to pop over to this page

While waiting for the release of Guake for Hardy Heron, I came across Tilda, which is another nifty console, that is less of a killer app, than a good excuse to play around with an alternative to the default. Good work people, keep up the pressure on Canonical, to develop even better terminals.

Instructions for setting up YeahConsole

First, it needs to run every time you log in. So, go to System > Preferences > Session, select Startup Programs, click Add, and add a new command, name “YeahConsole”, command “yeahconsole”. Next, it looks a bit rubbish when you start it up. Here’s how bare-bones yeahconsole is: you configure it with X resources. Party like it’s 1989! I had to go look up how to do this; for the more tender in years among us, X resources were a sort of central configuration for all your apps, round about the same time that humanity was fighting off sabre-toothed tigers and wondering whether that hot flamey thing in the corner could actually be useful. On Ubuntu, you need to edit (actually, you probably need to create) a file called .Xresources in your home folder*. In that file, you put the configuration for yeahconsole, like this:

yeahconsole*toggleKey: None+F2
yeahconsole*consoleHeight: 20
yeahconsole*aniDelay: 0
yeahconsole*stepSize: 10
yeahconsole*faceName: ProFontWindows:style=Regular
yeahconsole*faceSize: 9

The toggleKey one is the important one: it sets which key you use to summon the terminal. I like F2, myself, but pick whatever. What all this stuff means is documented in the man page (man yeahconsole), apart from which font to use. This is the faceName and faceSize options above, and here you have to delve a bit (I told you this was old-fashioned; I started writing a yeahconsole-properties configuration utility that did all this for you, but couldn’t be bothered). In a terminal, run fc-list. This lists all the TrueType fonts that you can use in an xterm. Choose one, and put it in faceName above. Now, simply start yeahconsole for the first time (press Alt+F2, type yeahconsole), and then press F2 (or your key of choice). Pow, a dropdown terminal, like Tilda, but one that won’t keep crashing and make you cry. One other thing: if you hit Ctrl-D to log out by mistake, it’ll close yeahconsole (tilda did this too), and that’s really irritating. To fix this, put the following in a file called bashloop in your home folder:

while true; do bash; done

and change your Startup Programs command above to be yeahconsole -e /home/username/bashloop. Now Ctrl-D won’t close yeahconsole.

Read Full Post »

Older Posts »