organise
Ten Handy Bash Aliases for Linux Users
Posted by Kevin Purdy at 10:38 PM on July 10, 2008
TechRepublic's 10 Things blog posts 10 shortcut ideas for Linux users (and Terminal-friendly OS X hackers) to make the terminal a friendlier, faster place to work. To use them, open up the .bashrc file found in your home directory and add lines in the following manner:
Alias NICKNAME='full command here'Replace "NICKNAME" with a quick-type command, and put the full command in quotes. One example offered up by TechRepublic is a command to open up a file you regularly edit, like your /etc/apt/sources.list repository list, with a single command:
For instance, when I used Enlightenment E16 (I now use E17), I was frequently editing the menu file ~/e16/menus/user_apps. Instead of constantly opening up a terminal and entering nano ~/.e16/menus/user_apps, I used an alias that allowed me to type emenu and start editing. I used this alias:Users of GNOME-based distributions like Ubuntu might want to switch
alias emenu='aterm nano -e ~/.e16/menus/user_apps'Now, I just enter the command emenu (or I can enter that in the run command dialog) to open up this file in an editor.
gedit in place of nano -e for an easier-to-grasp graphical editor. Got your own bash shortcuts you put in every new Linux install? Share them in the comments.Tags: command line | linux | organise

Comments (AU Comments · US Comments)
There are currently no AU comments for this post.
friendslikeJimRome
Posted 11:23 PM 10/7/08
Here's my favorite:
alias disku "du -sh * | sort -rn | head"
it pops out the top 10 biggest files or directories in your current working directory.
friendslikeJimRome
The How-To Geek
Posted 11:13 PM 10/7/08
Just noticed my typo... I use aliases like cdh and cdt for switching directories, not lsh and lst.
The How-To Geek
The How-To Geek
Posted 11:11 PM 10/7/08
I have a whole set of aliases like this for common tasks... especially for switching directories. It drives me crazy to have to switch between 5-10 different deep paths when working on my server, so I have aliases like this:
alias lsh='cd /var/www/vhosts/howtogeek/...etc/'
alias lst='cd /var/www/vhosts/howtogeek/...etc/themes/howtogeek/'
That way I can quickly switch between the various locations through my ssh client. Much simpler.
The How-To Geek
fAlCoNNiAn
Posted 11:00 PM 10/7/08
Great tip for Linux newbies.
fAlCoNNiAn
JadeEmperor
Posted 11:50 PM 10/7/08
and for those like me who are "pwd" conscious, try this:
alias lcd = 'cd $1; set PS1='pwd''
so instead of using cd followed by pwd to know where you are just use "lcd" and automagically set your prompt to your current working directory.
well something like that. i'm using Vista now and don't have me *nix console handy. anyway you get the idea. :-)
JadeEmperor
andrewheiss
Posted 11:43 PM 10/7/08
Kind of related question... What does nano -e do? In the manual it says it's just ignored...
andrewheiss
neergrm
Posted 11:41 PM 10/7/08
disku just found 100Mb in my documents folder :-) thanks for sharing
neergrm
jkrell
Posted 11:30 PM 10/7/08
This would be great for the VirtualBox kernel module rebuild you have to do every time you update the kernel. That is, if I could get the rebuild to work since the last couple kernel updates. Oh, well...
jkrell
mrosedal
Posted 11:29 PM 10/7/08
I have an alias for psg and lsg
psg='ps aux | grep' So a common one for me is psg ssh so I can see all of the ssh terminals.
lsg='ls -l | grep' If you are in /etc and you do an ls it returns to many values for you to filter through, but if you know that the directory or file you want starts with a or ap than you can grep that way. They work great.
mrosedal
FiveAcres
Posted 12:15 AM 11/7/08
alias grt="ls -grt" # my favorite options for ls - I have a hard time using the shell without this command
alias ghere="nautilus --no-desktop ." # start nautilus in current directory
alias go="gnome-open" # use gnome default to open file
alias ko="kmclient exec" # use kde default to open file
FiveAcres
ostracize
Posted 12:13 AM 11/7/08
lists hidden files and folders only:
alias l.='ls -d .*'
Stupid typos:
alias dc='cd'
alias l='ls'
alias s='ls'
alias sl='ls'
ostracize
fatalbush
Posted 12:07 AM 11/7/08
very nice. i'll have to try this at home tonight.
of course, while testing this out, the first alias will be something to the effect of
alias alias="sudo gedit /.bashrc"
which nearly creates a head-imploding loop...
fatalbush
BRYtar
Posted 12:00 AM 11/7/08
I use sh and I guess this doesn't work.
Any suggestions?
BRYtar
syko21
Posted 11:57 PM 10/7/08
alias search="apt-cache search"
#search package_name
so I don't have to open up synaptic to find one package
alias install="sudo apt-get install"
alias remove="sudo apt-get remove"
A bit debian/ubuntu -centric but they are useful.
syko21
Eris
Posted 12:49 AM 11/7/08
@JadeEmperor: This is not necessary. With bash you can set PS1 to something containing \w (full path) or \W (just the last path element) in your .bash_profile and it will do this automatically. In fact, here is an article about all of the special things that can go in PS1: [www.davidpashley.com]
Eris
valadil
Posted 12:36 AM 11/7/08
Here are some favorites that I haven't seen a lot of other people already using:
alias bt='btdownloadcurses.bittornado --minport 6779 --maxport 6789 --max_upload_rate 25 --responsefile'
alias tf='tail -f'
and most importantly...
alias sudo='A=`alias` sudo '
This one ensures that your aliases persist so you can use them in sudo.
valadil
sharfah
Posted 12:29 AM 11/7/08
alias ..=cd ..
alias ...=cd ../..
alias ....=cd ../../..
alias .....=cd ../../../..
alias cl=clear
alias cla=clear;la
alias cll=clear;ll
alias cls=clear;ls
alias clal=clear;lal
alias rmdir=rm -rf
alias d=date
alias ff=find . -name $1
alias h=history
alias l=ls
alias la=ls -a
alias ll=ls -ltr
alias lal=ls -al
alias ls=ls -F
alias sl=ls
alias more=less
alias mroe=more
alias m=more
alias r=fc -s
alias igrep=grep -i
My complete bash profile, including some nifty functions:
[fahdshariff.blogspot.com]
sharfah
Eris
Posted 12:56 AM 11/7/08
Additionally, I suggest that everybody have a look at zsh. It's insanely customisable and includes options like auto-cd (typing a path that points to a directory with no command takes you there) and custom keybindings. I found a binding online such that typing . becomes /.. if it is immediately after .., which combined with auto-cd is convenient. :)
Eris
HeartBurnKid
Posted 1:40 AM 11/7/08
Very nice stuff. I haven't really messed with aliases yet, but I think I will. Considering my vast collection of single-line batch files on Windows, it might come in handy.
HeartBurnKid
tbranham
Posted 1:39 AM 11/7/08
@Eris You beat me to it. Zsh is a truly fantastic shell -- I can't sing its praises highly enough.
tbranham
shankar
Posted 2:15 AM 11/7/08
@andrewheiss: I think the -e makes more sense if it is an option for aterm, not for nano
aterm -e nano whatever.ext
should open a new terminal window (aterm), then execute nano
shankar
falcolas
Posted 2:37 AM 11/7/08
The only one from my file worth repeating.
cl()
{
if [ $# -eq 1 ]
then
cd $1
else
cd $HOME
fi
ls
}
Essentially the same as typing cd ; ls
@valadil:
I like the sudo one - I'll have to try that myself.
falcolas
SEMW
Posted 3:18 AM 11/7/08
Something that does work for aliasing things with sudo, without needing to edit the root .bashrc, is:
alias sudo="sudo "
With a space before the second quote marks. I have no idea why this works, but it does (but only if the alias is the first thing after the sudo).
SEMW
arod
Posted 3:00 AM 11/7/08
@valadil: the sudo alias didn't work for me. I'm using mandriva so I changed sudo to su but no luck. Instead, I moved all of my aliases to a .aliases file. Then in the .bashrc of root I put:
. .aliases
Obviously, I can put this in the .bashrc of any user so that they will get the aliases too and not break the DRY principle.
arod
valadil
Posted 4:05 AM 11/7/08
Sorry for the double post. Mine wasn't showing up and I thought no-script ate it so I retyped it.
valadil
valadil
Posted 4:04 AM 11/7/08
@arod:
you need to use sudo, not su. sudo lets you execute a command as root. su logs you in as another user, but defaults to root if there's no argument. Mandriva should have a sudo package you can install if it didn't come with your system. If you want to preserve variables and aliases through su, use su -m.
@SEMW:
No need to edit root's anything. This is all done in user config files. Your method is certainly a lot cleaner though.
valadil
valadil
Posted 4:00 AM 11/7/08
@arod: Did you make sure to use backticks (`) instead of singlequotes (')? Commands inside backticks execute first and their output is used in the next line. In this case they end up running all the alias commands (since alias by itself prints all your aliases, and then it runs again) before sudo runs.
Also, why change it to su? sudo runs a single command as root. su logs you in as another user (root by default). If Mandriva doesn't come with sudo, you should install it.
@SEMW: no need to edit root's .bashrc for this. I'll have to look at aliasing "sudo " though, since it's obviously cleaner (I still have no idea why I need to include "A=" in my version and even less clue of what made me think to include it).
For some more sudo su fun, you can alias su to 'su -m' to preserve your environment. Then you can set your bash prompt to reflect your rootiness, without having to edit any of root's files. I like to do this with color rather a username because I'm much more likely to notice a root terminal that way. The only downside is that your env variables will persist so if you cd, you'll cd into your $HOME, not into /root. This doesn't come up often and I don't mind typing /bin/su instead of su when I need the problem to go away.
valadil
Riistetyt
Posted 4:49 AM 11/7/08
A few I have in my .bashrc are;
alias ins="sudo apt-get install"
alias delete="sudo rm -rf ~/.local/share/Trash/files/*" (when I have stubborn files that won't delete).
alias remove="sudo apt-get autoremove"
alias update="sudo apt-get update && sudo apt-get upgrade && sudo apt-get dist-upgrade"
Riistetyt
friendslikeJimRome
Posted 4:46 AM 11/7/08
@BRYtar: sh doesn't use aliases. On most machines you can go into another shell by just typing it in:
/bin/bash
If your machine doesn't have Bash, it might have the C-Shell:
/bin/csh
friendslikeJimRome
takeiteasy
Posted 4:37 AM 11/7/08
@falcolas:
The same can be achieved with
cl()
{
cd $1
ls
}
takeiteasy
DHT
Posted 4:50 AM 11/7/08
@Eris:
I found a binding online such that typing . becomes /.. if it is immediately after ..
Care to share this keybinding?
DHT
gkelly
Posted 5:27 AM 11/7/08
@gkelly: whoops, tags were stripped.
The alias is:
alias hostname='ssh username@hostname'
gkelly
gkelly
Posted 5:26 AM 11/7/08
If you work with a lot of different machines like I do, this is very useful:
alias ='ssh @'
This saves me from typing "ssh " a bunch and also allows different usernames to be used without having to remember which username goes with which hostname.
gkelly
element103
Posted 1:35 AM 11/7/08
@friendslikeJimRome: Correct me if I'm wrong, but I think the sort command only looks at the numerical value and doesnt account for the human-readable suffix. I think in order for it to work properly you have to use
alias disku "du -s * | sort -rn | head"
and then just deal with the ridiculous numbers, or write a small script to do it. great idea though! (i never knew about du so i was using the stupid gui version)
element103
SoonerGrad
Posted 12:45 AM 11/7/08
gedit is good, but i prefer nedit as well.
SoonerGrad
forgotthename
Posted 7:25 AM 11/7/08
u know it took me 5 min to fig out why --> (alias disku "du -sh * | sort -rn | head") --- ha its bc you forgot the equal sign ( alias disku="du -sh * | sort -rn | head" )... good command thou..thanx
forgotthename
Jungle Pop
Posted 11:31 AM 11/7/08
@jkrell: Oh good, it's not just me!
Jungle Pop
rlee
Posted 12:27 PM 11/7/08
@Eris: Yep, I used to do such cumbersome alias'ing, until I learned this would put the full path in the prompt:
declare PS1='$(echo $PWD): '
rlee
lokalhost
Posted 2:23 PM 11/7/08
Some of these are for the cli, some I use via alt-f2
#Kick firefox
alias killfirefox="for f in $(ps auxfwww | grep firefox | awk {'print$2'}); do kill -9 $f; done"
#listen to country radio
alias ckgy='vlc rtsp://newcap-RedDeer-CKGY.wm.llnwd.net/newcap_RedDeer-CKGY'
#edited my host/user out to connect to a winblows box
alias windowsname='rdesktop -u username -g 1600x1000 hostname:3389 -T "WINDOWSNAME:RDP" -x b -z -r clipboard:PRIMARYCLIPBOARD -5'
#lots of these:
alias hostname='ssh -X -C hostname' #some with -l user
#sql tunnel
alias mysqlTunnel="ssh -f -L 3306:sqlserverip:3306 sqlserverip -N -l root"
lokalhost
asmus.vierck
Posted 4:01 PM 11/7/08
@element103:
Simply use du -ks instead of du -hs; this will give you file sizes in kB. So e.g. 14250 will stand for a file size of 14MB and 250 kB.
For getting the process id´s of (stalled) applications i like to use
pid () { ps -Ax | grep $@ | awk '{print $2="", $3="", $0}'; }
asmus.vierck
jorsch
Posted 9:47 PM 11/7/08
alias lsdir="ls -p | sed -n '/\//p'"
Lists only directories in your current directory
jorsch
DHT
Posted 4:29 AM 12/7/08
@jorsch:
Not quite. That only lists directories (and symbolic links) that aren't hidden. If you want to see the hidden dirs (those that start with a dot) then you have to add a -a to the ls command. And of course if you want to see the long version (size, date, time, etc) then add a -l.
This is my directory listing alias:
alias lsd='ls -ld `find . -maxdepth 1 -type d -o -type l`'
That will show me all directories and symbolic links in the current directory, with all of the file attributes etc.
Actually, I use a more complex function instead of an alias, so I can pass it a directory instead of the current one if I want to. But since this is for aliases, that's my old alias approach.
DHT
weapey
Posted 7:46 PM 11/7/08
Here's one for people who use screen, and connect to a shell from different connections (i.e. connecting to a session at home from work and vice versa).
alias reattach='screen -raAd'
This detaches any screen session you may have from it's current shell and connects you to it. I use it to connect to my irssi session I constantly have open.
weapey
saudade
Posted 4:47 PM 11/7/08
@lokalhost:
Remove the alias hostnames by using .ssh/config for that stuff. Also pkill on solaris/linux and killall on darwin/osx are a quicker replacement.
Quick example:
Host hg.opensolaris.org
User anon
Cipher blowfish
Compression no
Host work
Hostname 127.0.0.1
Port 443
Etc......
Also zsh is better. :)
saudade
jonny290
Posted 8:50 AM 11/7/08
If you are a screen junkie,
alias sr='screen -raAd'
Detaches your screen from any other terminals, resizes them, and does some other magic before attaching to your current terminal. I use it constantly.
jonny290
efbee
Posted 8:36 AM 11/7/08
My favorite alias + linux command line tip... I can generally get around from the command line faster, but sometimes you need the gnome file manager, or just need to open a file without knowing what program handles it. The program for this is (for Gnome users anyway) gnome-open.
My alias for this is like so:
alias go="gnome-open"
So now I can do things like this:
Open current directory in file manager:
$ go .
Play some MP3 file in whatever my default player is:
$ go SomeSong.mp3
Open a PDF file I just downloaded:
$ go Downloads/New_Lifehacker_eBook.pdf
You get the idea. :) Windows command line junkies (*shudder*) have a similar command called "start". Enjoy.
efbee
nuhaedra
Posted 11:32 AM 11/7/08
You can create a separate file called .bash_aliases. It's easier to migrate your aliases to a distro upgrade or to use them on a live cd or whatever. Just add this to your .bashrc to make them read.
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
nuhaedra
CallMeNewt
Posted 7:49 AM 11/7/08
The script below works as like "cd" but allows you to toggle between two directories. By far the most handy and useful script I have and I use it many times each day.
For example:
/tmp> pwd
/tmp
/tmp> to ~
/home/CallMeNewt> pwd
/home/CallMeNewt
/home/CallMeNewt> to
/tmp> pwd
/tmp
to() {
CUR_DIR=$PWD
if [ -z "${1}" ]
then
if [ ! -z "${MY_PREV_DIR}" ]
then
cd $MY_PREV_DIR
MY_PREV_DIR=$CUR_DIR
else
echo "No where to go!"
fi
else
cd ${1}> /dev/null 2>&1
retVal=$?
if [ ${retVal} = 0 ]
then
MY_PREV_DIR=$CUR_DIR
else
cd $reportJobsRoot/${1}> /dev/null 2>&1
retVal=$?
if [ ${retVal} = 0 ]
then
MY_PREV_DIR=$CUR_DIR
else
echo to: ${1}: not found
echo to: $reportJobsRoot/${1} not found
fi
fi
fi
}
CallMeNewt