10

I found this article discussing a quick way to sort your bash history and see which commands you use the most:

cut -f1 -d" " .bash_history | sort | uniq -c | sort -nr | head -n 30

It really opens your eyes as to which tasks you should create aliases for and which ones you should cronjob away completely.

I'd love to see a ServerFault list of recommended command line aliases that save you time on the job. What have you got?

Between us, I'll bet we can save every user who reads this post at least 10 minutes a day. (Minus the first day wherein they waste all of their time reading ServerFault instead of doing their job of course!)

Jax
  • 428
  • 3
  • 10
  • 17
  • For the record, on Super User there's http://superuser.com/questions/7083/useful-command-line-aliases with loads of useful Bash aliases – Jonik Jul 07 '10 at 08:06
  • See also: http://serverfault.com/questions/15365/favorite-unix-command-line-aliases – Aaron Copley Dec 07 '10 at 18:28

13 Answers13

7

I deal with a lot of different machines so one of my favorites is aliases for each machine that I need to frequently SSH to:

alias claudius="ssh dinomite@claudius"

It is also useful to setup a good .ssh/config and ssh keys to make hopping amongst machines even easier.

Another one of my favorite aliases is for moving up directories:

alias ..="cd .."
alias ...="cd ../.."
alias ....="cd ../../.."
alias .....="cd ../../../.."

And some for commonly used variations of ls (and typos):

alias ll="ls -l"
alias lo="ls -o"alias lh="ls -lh"
alias la="ls -la"
alias sl="ls"
alias l="ls"
alias s="ls"

I almost always want egrep:

alias grep="egrep"

Get man pages from all sections

alias man="man -a"

I often found myself piping output through awk in order to get a certain column of the output, as in df -h | awk '{print $2}' to find the size of each of my disks. To make this easier, I created a function fawk in my .bashrc:

function fawk {
    first="awk '{print "
    last="}'"
    cmd="${first}\$${1}${last}"
    eval $cmd
}

I can now run df -h|fawk 2 which saves a good bit of typing.

I work with a lot of Perl and need to know the versions of modules installed on a system:

function perlmodver {
    perl -M$1 -e 'print "Version " .
        $ARGV[0]->VERSION . " of " . $ARGV[0] .
        " is installed.\n"' $1
}
Drew Stephens
  • 662
  • 7
  • 12
3

I usually like to type everything, because I'm afraid I'll forget all about the commands I'm using in no time. I know I may be wrong, but I don't think I'd know as much about the system if I had just pasted every command in Commandlinefu into my .bashrc.

Note that I'm not saying that I'm the greatest and I know everything there is to know about Linux, far from that, I'm just saying I like to learn, and remember what I've learned by using.

That's not to say I don't appreciate how time saving aliases are (specially for larger functions, but then you have to see if a script isn't better), but, personally, I don't like to use them (at least no always).

That being said, Commandlinefu has a special tag for aliases. It's worth looking.

Also I think this is a good one:

   alias less='less -FSRX'

This will only use less if the output is bigger than the screen.

Flávio Amieiro
  • 753
  • 2
  • 9
  • 18
2

My favorite of all times:

alias server_name='ssh -v -l username ip_address'

It doesn't need explanation, does it? :-)

2

alias lt='ls -lhart'

  • l=long : h=human readable sizes : a=all : r=reverse sort : t=time sort
  • Puts the newest file at the bottom, right above the prompt

alias active='grep -v -e "^$" -e"^ *#"'

  • shows only lines that are not blank or commented out
  • example: active /etc/httpd/conf/httpd.conf

alias svi='sudo vim'

alias scr='screen -Rd'

CanyonR
  • 371
  • 2
  • 3
1

I find annoying to put the whole apt-get and apt-cache things in debian / *buntu, especially with sudo prefix, so I have:

alias apti='sudo apt-get install'
alias aptr='sudo apt-get remove'
alias apts='apt-cache search'
astropanic
  • 297
  • 2
  • 5
  • 18
1

I have to admit I have a few like this..

alias sl=ls

To try and combat my typos :-)

I also have

alias aliases='xterm -e vim ~/.bash_aliases;reload'
alias reload='. ~/.bash_aliases'

So its nice and simple to add new ones.

Vagnerr
  • 1,265
  • 1
  • 15
  • 20
1
alias cd..='cd ..'
vartec
  • 6,137
  • 2
  • 32
  • 49
1

create .tar.gz

alias tarc="tar czvf"

extract .tar.gz

alias tarx="tar xzvf"

list .tar.gz

alias tart="tar tzvf"
aeonflux
  • 1
  • 1
1
for host in `awk '{ print $1 }' ~/.hostsILogIntoAllTheTime`; do
  alias $host='ssx '$host
done 

Type the hostname to logon to the host. Note: ssx is an alias itself:

ssx='ssh -Y'

Combined with shared keys, it's pretty slick.

Swoogan
  • 2,007
  • 1
  • 13
  • 21
0

+1 for Drew Stephens answer. The only one he didn't cover that I also use is to alias the various dot files I need to access on a regular basis like:

alias dotz='vi ~/.zshrc'

Take it a step further with an on-the-fly add-alias function:

function add-alias {
echo "alias $1=\047$2\047" >> ~/.zshrc
}
unhappyCrackers1
  • 977
  • 1
  • 6
  • 18
0

Also see

Hamish Downer
  • 9,142
  • 6
  • 36
  • 49
0

I found this page looking for a way to make an alias to show me the nth column of a csv (tsv actually) file using awk. The fawk function above was almost exactly what I wanted. I modified slightly to add an optional second argument, the filename, so I can use it like awk normally rather than only through a pipe.

function fawk {
    first="awk '{print "
    last="}' $2"
    cmd="${first}\$${1}${last}"
    echo $cmd
    eval $cmd
}

Thanks!

0

This is not an alias, but seeing people who aliases ssh connections, might be useful.

Openssh package has a binary called ssh-argv0 that works via symlinks.

So, if you do something like this:

sudo ln -s /usr/bin/ssh-argv0 /usr/local/bin/user@ssh.server.com

Having /usr/local/bin in the $PATH, now you can connect as user "user" to ssh.server.com writing this in a shell prompt:

 $ user@ssh.server.com 
sntg
  • 1,424
  • 11
  • 15