10
votes

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!)

2

13 Answers 13

7
votes

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
}
3
votes

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.

2
votes

My favorite of all times:

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

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

2
  • 5
    Isn't that what's .ssh/config is for :-P Host server_name Hostname 1.2.3.4 User username etc.
    – vartec
    May 6, 2009 at 12:23
  • Using bash_completion and tab,tab seems to work just fine for me, instead of setting up a bunch of ssh aliases Jun 4, 2012 at 4:08
2
votes

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'

1
  • Nice, especially sudo vim
    – astropanic
    Dec 7, 2010 at 13:49
1
vote

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.

1
vote
alias cd..='cd ..'
1
vote
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.

1
vote

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'
1
vote

create .tar.gz

alias tarc="tar czvf"

extract .tar.gz

alias tarx="tar xzvf"

list .tar.gz

alias tart="tar tzvf"
0
votes

Also see

0
votes

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
votes

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/[email protected]

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

 $ [email protected] 
0
votes

+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
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .