39

What are your favorite command line aliases (bash/sh/tcsh) aliases? Here are a few of mine.

alias lsr='ls -lrt'
alias gon='cd $HOME/Notes'
alias devdb='mysql -h dev --user=x --password=secret dbname'
alias ec='rm *~'; # emacs cleanup
alias h='history'
alias eb='exec bash'; # Solaris sometimes defaults to sh
alias mr='more'
alias mroe='more'
alias qd='echo export DISPLAY=$DISPLAY'
alias ralias='. $HOME/.alias'; # reread aliases
alias ,,='cd ../..'
alias ..='cd ..'
alias c='clear'
Stefan Lasiewski
  • 23,667
  • 41
  • 132
  • 186
  • 18
    Rather than passing your password in on the commandline to `mysql` (anyone else on the server could see it!), put the username and password in a ~/.my.cnf file, and simply specify `-up`. MySQL tools will pick those credentials up automatically, read mysql(1) for more info. – Alex J May 30 '09 at 04:54
  • +1 For *alias ..='cd ..'* – mosg May 13 '10 at 10:42
  • +1 for 'mroe'. I need to take care of my common misspellings... – gWaldo Nov 15 '10 at 15:06

52 Answers52

12
function s()
{
    screen -t "$@" /usr/bin/ssh "$@"
}

Connect to a host in a new screen tab, with the device name as the tab title.

Murali Suriar
  • 10,296
  • 8
  • 41
  • 62
11

My favourites that haven't been mentioned so far:

alias l='ls'
alias u='cd ..'
alias uu='cd ../..'
alias uuu='cd ../../..'
alias uuuu='cd ../../../..'

I'm not normally a fan of aliases that just shorten things, but I type ls so very much, and l only needs one hand.

C Pirate
  • 226
  • 1
  • 4
  • 1
    ls (and cd) only needs one hand...if the keymap is Dvorak! Same hand as 'Enter', for that matter. – gbarry May 31 '09 at 01:56
8

none since I can never guarantee they'll be configured on EVERY system I'll log into (as myself, root, or whoever).

TCampbell
  • 2,024
  • 14
  • 14
  • 1
    Exactly! Harmless things like alias ls="ls --color=auto are fine, but changing the rm command etc.? Never. – user9474 Jun 14 '09 at 17:50
7

None, I change between systems so much every day that I basically gave up on it.

  • You don't sync your home directories between the various machines? http://xoa.petdance.com/How_to:_Keep_your_home_directory_in_Subversion – Andy Lester May 31 '09 at 04:20
  • I maintain with a group of other administrators around 5k machines, most of these machines during their entire lifespan never had an remote interactive user session (all installation and configuration happens automatically), sometimes there is a wierd problem and you do have to log on. We have considered to have the user admins account to auto mount from a shared NFS partition, but for the use of it isn't worth it. – Martin P. Hellwig Jun 01 '09 at 09:16
  • 1
    ...don't have admin/root accounts include nfs directories in its path -- when NFS is bork, so is admin/root accounts. Some of my clients insist on learning this the hard way. – David Mackintosh Oct 22 '09 at 13:03
7
alias ..="cd .."
alias ...="cd ../.."

# mkdir and enter it immediately thereafter
mcd()           { mkdir $1 && cd $1; }

# when entering a directory, list the contents.
cd()            { builtin cd "$@" && ls; }
z8000
  • 792
  • 1
  • 7
  • 15
6
alias rm 'mv -f \!* $WASTEBASKET'
alias unrm 'mv $WASTEBASKET/\!* .'

I know that many will disagree, but I like safety nets. (And please try to forgive me for using tcsh.)

This somewhat similar one should be outlawed, though:

alias rm 'rm -i'

I've seen people who were trained on systems with that alias, and then they type rm * on some other system, expecting to get questions about which files to delete, and then they sit there and watch it do exactly what it is supposed to do.

EDIT:

Some of the comments compared the move-to-wastebasket alias with the "-i" flag, saying that they are similar. But to me, there is an important difference. With "-i", you get the confirmation prompt every time you use the command, and it becomes something you expect and rely on. The wastebasket solution, on the other hand, works exactly like the standard rm, until you actually make a mistake and need to un-remove a file. It's a bit like the difference between training wheels and a spare tire in the trunk.

  • the bash equivalent requires a function: del() { path=`readlink -f "$1"` mkdir -p $WASTE$path mv $path $WASTE$path } Its not perfect (as it creates a new dir with the filename) but it works ok. (putting in newlines is left as exercise for reader!) – gbjbaanb Jun 01 '09 at 16:35
  • +1: I use Tcsh too :-) –  Jun 02 '09 at 01:01
  • +1: you're forgiven for using tcsh ;) – bedwyr Jun 06 '09 at 01:20
  • 6
    Well in fairness, expecting a confirmation prompt and not getting one is just as bad as expecting it to go into a wastebasket and that not happening. There's no difference, really. – Dan Udey Jun 10 '09 at 01:49
  • @Dan: I thought the same. The alias really should not be called rm, but wb for wastebasket, or something similar. – user9474 Jun 14 '09 at 17:48
  • On of my company's "shared" machines we have this: % which rm rm: aliased to echo Remove !* \?\?\? If you are really sure about deleting type /bin/rm I'm not sure how good it is for preventing real accidents though. – Muxecoid Dec 16 '09 at 12:01
6

The total contents of my "alias list" is:


I've spent enough time fixing unix machines I don't "normally" work with (one of the downsides of having been in-house unix admin for a software house, you end up on customer sites, A Lot) that the first thing I do is to "unalias -a", just so that any alias the normal production admin have don't happen to interact with a mis-spelling, after that it's too much hassle to customise.

This has carried over into my normal usage, too.

Vatine
  • 5,440
  • 25
  • 24
4

These are for zsh, but I imagine you could port them to another shell reasonably easily:

sudo() { [[ $1 == (vi|vim) ]] && shift && sudoedit "$@" || command sudo "$@"; } # sudo vi/vim => sudoedit
wst() { TZ=Australia/Perth date } # get local time no matter what server I'm on

FULLHOST=`hostname -f` 2>/dev/null || FULLHOST=`hostname` # reasonably portable, always gets a DHCP suffix too (if one exists)
SHORTHOST=`echo $FULLHOST | cut -d. -f1-2` # get the first two segments of hostname, which I used in my shell prompt
Alex J
  • 2,844
  • 2
  • 23
  • 24
  • Sorry for my ignorance, what is the difference between sudo vim and sudoedit? Is sudo edit = sudo $EDITOR ? – olle May 30 '09 at 16:22
  • sudoedit runs your editor under your account, rather than as root. That means you get access to your own ~/.vimrc and so on. – Alex J May 30 '09 at 17:00
  • 1
    +1 for changing `sudo vi ` to `sudoedit – Kevin M Aug 04 '09 at 15:45
4

Here are some of my favorites. (A few are ZSH-specific.)

alias ls='ls -F --color=auto'
alias l='ls'
alias ll='ls -ahl'
alias ..='cd ..'
alias ...='cd ../..'
alias mv='mv -i'
alias mmv='noglob zmv -W'
alias mcp='mmv -C'

mkcd() {
        if [ $1 = "" ]; then
                echo "Usage: mkcd <dir>"
        else
                mkdir -p $1
                cd $1
        fi
}

# ZSH global aliases for piping
alias -g H="| head"
alias -g T="| tail"
alias -g C="| wc -l"
alias -g L="| less"
alias -g G="| grep"
alias -g S="| sed -e"
alias -g A="| awk"

# Subversion related
alias ss='svn status'
alias sd='svn diff'
alias sc='svn commit'

# Git related
alias gs='git status'
alias gc='git commit'
alias ga='git add'
alias gd='git diff'
alias gb='git branch'
alias gl='git log'
alias gsb='git show-branch'
alias gco='git checkout'
alias gg='git grep'
alias gk='gitk --all'
alias gr='git rebase'
alias gri='git rebase --interactive'
alias gcp='git cherry-pick'
alias grm='git rm'

fortune -s  # Add to your profile to brighten your day :)
4

there are many aliases here wich are not neccessary:

alias c='clear'

can be replaced by just pressing [Ctrl]+[L]

alias mroe='more'

and similar: zsh provides spell correction by default, bash does it with extensions

alias something="history | grep $@"

pressing [Ctrl]+[R] does the same thing in bash/zsh

back='cd $OLDPWD'

the same can be done in every shell with

cd -
Skaarj
  • 1
  • 1
4

For those troublesome colleagues:

alias ls=rm
Mark Harrison
  • 805
  • 2
  • 12
  • 20
3
jldugger@jldugger:~ $ alias 
alias ls='ls --color=auto'
alias youtube-dl='youtube-dl -t
jldugger
  • 14,342
  • 20
  • 77
  • 129
3
alias perg='grep -rni --exclude=\*.svn\*'
alias df='df -kTh'
alias ll="ls -l --group-directories-first"
Andrei Serdeliuc
  • 905
  • 5
  • 14
  • 26
3
alias cdd='cd /wherever/my/current/project/is'
chaos
  • 7,483
  • 4
  • 34
  • 49
2
alias ls="ls --color=auto -A -h -i -s --group-directories-first -l"
alias screen="screen -U"
alias sscreen="~/Projects/bin/start_screen.sh"
alias gst='git status'
alias gl='git pull'
alias gp='git push'
alias gd='git diff | emacs'
alias gc='git commit -v'
alias gca='git commit -v -a'
alias gb='git branch'
alias gba='git branch -a'
GNUix
  • 490
  • 1
  • 5
  • 13
2

p='ps auxww|grep -v grep|grep '

disserman
  • 1,850
  • 2
  • 17
  • 35
2

For searching old perl scripts for something:

alias searchperl 'find /place1 /place2 /place3 -name "*.pl" | xargs grep

Email myself a file:

alias mailthis 'mail -s mailthis email@email.com < '
mmarchin
  • 1
  • 1
1
if [ "$(uname)"="darwin" ]; then
  EDITOR=mate
  PATH=$PATH:~/.bin
  alias sleep_hdd='sudo pmset -a hibernatemode 1'
  alias sleep_ram='sudo pmset -a hibernatemode 0'
  alias sleep_combined='sudo pmset -a hibernatemode 3'
  alias cdproj='cd ~/Projects/Web'
  alias e='mate'
  alias vboxheadless='VBoxHeadless -startvm '
  alias subash='sudo bash'
fi

if [ "$(uname)" = "SunOS" ]; then
  alias ls='ls -F'
  alias e='vim'
  alias subash='pfexec bash'
fi
daeltar
  • 311
  • 1
  • 4
  • 8
  • vboxheadless should already be in your $PATH: uname; which vboxheadless Darwin /usr/bin/vboxheadless – olle May 30 '09 at 16:27
1
alias l='ls --color=auto -lsah'
alias ..='cd ..'

I miss it very often on other systems

lImbus
  • 497
  • 4
  • 13
1

This one works on most linux desktops:

alias go='xdg-open'

Opens a document or folder with the registered application, similar to the start command on windows.

amarillion
  • 1,409
  • 2
  • 16
  • 25
1
history | awk '{print $2}' | awk 'BEGIN {FS="|"} {print $1}' | sort | uniq -c | sort -nr | head -10

Show the top 10 most used commands in your history.

pete
  • 296
  • 1
  • 2
  • shorter version, no need for awk: history | cut -f 5 -d' ' | sort | uniq -c | sort -n | tail – Marcin Aug 01 '09 at 16:16
1
alias viewpw='aespipe -d < ~/.passwd.aes > ~/.passwd.dec && more ~/.passwd.dec && shred -u ~/.passwd.dec'

How I remember all my passwords...

1

Here is my favorite, to find something in all of the Python code in the current and child directories, excluding those associated with subversion:

alias greppy="find . | grep -v [.]svn | grep [.]py$ | xargs grep "

Joel Bender
  • 175
  • 1
  • 7
1

Two more complicated shell functions. I use them often when searching stuff in source code or config files.

FFind() {
    if [ -n "$1" ] ; then
        if [ -n "$2" ] ; then
            local testVar="$1"
            shift
            find . -type f \
                -and '(' -not -path '*.git*' ')' \
                -and '(' -not -path '*.svn*' ')' \
                -and '(' -not -path '*.hg*' ')' \
                -and '(' "$@" ')' \
                -exec grep --color=always -I -i -F -H -n "${testVar}" {} ';'
        else 
            find . -type f \
                -and '(' -not -path '*.git*' ')' \
                -and '(' -not -path '*.svn*' ')' \
                -and '(' -not -path '*.hg*' ')' \
                -exec grep --color=always -I -i -F -H -n "$1" {} ';'
        fi
    fi
}

EFind() {
    if [ -n "$1" ] ; then
        if [ -n "$2" ] ; then
            local testVar="$1"
            shift
            find . -type f \
                -and '(' -not -path '*.git*' ')' \
                -and '(' -not -path '*.svn*' ')' \
                -and '(' -not -path '*.hg*' ')' \
                -and '(' "$@" ')' \
                -exec grep --color=always -I -i -E -H -n -m 1 "${testVar}" {} ';'
        else 
            find . -type f \
                -and '(' -not -path '*.git*' ')' \
                -and '(' -not -path '*.svn*' ')' \
                -and '(' -not -path '*.hg*' ')' \
                -exec grep --color=always -I -i -E -H -n -m 1 "$1" {} ';'
        fi
    fi
}

Usage:

FFind elephant

Recursively searches all text files in the current directory for the string elehant. Ignores files created by Subverion, git or mercurial. Ignores binary files.

FFind 'ele.*phant' -name '*.c' -or -name '*.h'

Recursively searches all c code files in the current directory for the string 'ele.*hant' (no regular expression matching). Ignores files created by Subverion, git or mercurial.

EFind 'mo*use' -name '*.java' 

Recursively searches all java code files in the current directory for the string muse or mouse or moouse or .... Ignores files created by Subverion, git or mercurial.

Tested with bash and zsh.

Skaarj
  • 1
  • 1
1

Some of my best collection :)

alias cdd="cd .."  
alias lss="ls -ctrl"  
alias nn="nautilus . &"  
alias pp="popd"  
alias p="pushd"  
alias g="gvim --servername `hostname` --remote"
alias findd="find . -name"
Kvisle
  • 4,193
  • 24
  • 25
insidepower
  • 191
  • 1
  • 3
1

A few plucked from my bashrc:

alias grep='grep --color=auto'
alias egrep='grep -E --color=auto'
alias e='$EDITOR'
alias g='git'
alias csort='sort | uniq -c | sort -n' # column sort piped data
alias sl='ls' # fat fingers

Generally, I usually have my bashrc figure out what package manager the system uses and then have it aliased as apt and yum, meaning on any machine on which my bashrc runs, I can just do:

apt search foo
yum install foo
apt update

It's not perfect but most of the common actions are the same between yum and aptitude, by the time you're trying to do something more complicated you can just remember what OS you're on.

JamesHannah
  • 1,731
  • 2
  • 11
  • 24
1

Probably my favorite, since it makes writing new aliases so easy:

alias realias='vim ~/.bash_aliases;source ~/.bash_aliases'
Telemachus
  • 571
  • 2
  • 11
1

I would say this is my favorite alias.

alias resume='screen -D -R'

It proves to be very handy after my windows workstation is automatically rebooted every weekend (Firm's policy).

1

A few more to add to the pile:

# little bit more readable PATH
alias path='echo -e ${PATH//:/\\n}'

# like others, I find these more efficient than
# typing cd ../../ etc
alias up='cd ..'
alias 2up='cd ../../'
alias 3up='cd ../../../'
alias 4up='cd ../../../../'

# found myself always mistyping this, so...
alias findy='find . -name'


alias targz='tar -xzvf'
alias hg='history | grep '
alias cls='clear'

# handy for the xclip tool
alias xclip='xclip -selection c'

# quick directory listing
alias ldir='ls -d */'

alias mys='mysql -uroot -psecret name-of-frequently-used-DB' 

alias trash='mv -t ~/.local/share/Trash/files --backup=t'
alias vb='vim ~/.bashrc'
alias +='pushd .'
alias _='popd'
yalestar
  • 227
  • 1
  • 2
  • 7
  • Storing passwords in scripts like this is potentially dangerous, unless you got it 600. Also upon execution your password goes to history file--also potentially dangerous. – Marcin Aug 01 '09 at 16:21
1
alias dsclean='find . -name .DS_Store -exec rm \{\} \;'
alias l='ls -lh'
alias ls='ls -G'

# Depends on your specific router
alias myip='curl -sn http://192.168.1.1/wancfg.cmd?action=view | grep td | tail -1 | tr -d '\''/<>a-z '\'''

# Start/stop local mysql installation
alias myserver='sudo /usr/local/mysql/support-files/mysql.server'
alias rssh='ssh -l root'
alias sc='./script/console'
alias sr='screen -r'
alias ss='./script/server'
alias sss='screen ./script/server'
alias up='svn up'
alias webshare='python -c "import SimpleHTTPServer;SimpleHTTPServer.test()"'
Jorge Bernal
  • 454
  • 1
  • 3
  • 9
1

Do quick arithmetic from the command line. Use "x" for multiplication to avoid expansion.

function math
{       
    echo "scale=2 ; $*" | sed -e "s:x:*:g" | sed -e "s:,::g" | bc
}


$ math 12,537.2 x 4
50148.8
Gerald Combs
  • 6,441
  • 25
  • 35
1

Unix alias command with examples

Cheers!

1
back='cd $OLDPWD'

I really hate typing that '$' ...

user27388
  • 41
  • 1
  • 4
0
alias t="rlog -L -l -R RCS/*"
David Oneill
  • 123
  • 6
0
#. Darwin:
.  : source
ls : ls -G -F
resize : osascript << EOF
  tell app "Terminal"
    set number of rows of first window to 25
    set number of columns of first window to 80
    set custom title of first window to "${PROFILE}"
  end tell
EOF

#. Linux:
.  : source
ls : ls --color=auto -F -X -h

...in general - I don't like using aliases too much though, you become dependent and learn to use something which only ever exists if you yourself make it so. I'd then feel handicapped when jumping on someone else's machine, even for a minute - so my general advice would be to use aliases very lightly.

For example aliasing ls to ls -F is ok, because it only has cosmetic effects, but aliasing to it ls -l or something - I would never do. I don't like these ll for ls -l aliases either for the same reason.

khosrow
  • 4,163
  • 3
  • 27
  • 33
0

We tend to use rdp a bit, because we can only access certain servcices via windows so I like:

alias rdt='rdesktop -d UOFA -g 1024x768 -u '

usage is

rdt <win_username> <winhost>

A lot of our stuff is in LDAP so search as the directory manager is useful (with an apporpirately configured /etc/openldap/ldap.conf):

alias dmsearch='ldapsearch -x -LLL -D"<directory_admin_bind_dn>" -W '

alias budate='date +%Y%m%d-%H%M' E.g. cp -p some-conf.comf someconf-'budate'.conf NB repalce the single quotes with backticks. Serverfault wont let me put in backtick literals and I cant work out how to escape backticks.

alias psg='ps aux|grep '

Jason Tan
  • 2,752
  • 2
  • 17
  • 24
0

Not an alias but a function, since i hate manually checking of the sshd is up after a reboot I have a waiting-for-ssh-to-answer function in my .zshrc:

function wssh () {
local HOST=$1
shift
local PORT=$1
if [[ -z $PORT ]]; then
        PORT=22
else
        shift
fi
echo -n "Polling host $HOST on port $PORT for ssh connection"
while ! nc -z -w 2 $HOST $PORT &>/dev/null; do
        for i in `jot 2 1`; do
                echo -n "."
                sleep 2
        done
done
echo "\nConnection establihed!"
ssh -p$PORT $HOST $@
}
olle
  • 101
  • 3
0
alias uab='unison -rsync -auto -batch'

I use unison to sync my settings, read/undread newsgroup mesages, etc.

hayalci
  • 3,631
  • 3
  • 27
  • 37
0
alias memusage='ps -o rss,command -waxc | sort -n'
alias ssq='svn status -q'
alias up='cd ..'
Andy Lester
  • 740
  • 5
  • 16
0
alias svndiff='svn diff --diff-cmd=colordiff'
alias df='df -h'

The first line uses colordiff to colorize svn diff output

Epeius
  • 1,031
  • 2
  • 9
  • 6
0

Most of our servers don't listen to SSH on a public VLAN. I can't be bothered to keep a tunnel open on my laptop when away from the office:

zugzug() { ssh -A -p <non_standard_port> -t <proxy_server> ssh $1; }

While it's a function and not an alias, I find it invaluable.

0

a few choice LAMP related snippets:

apache: current activity

alias apache_status='watch -n1 "/etc/init.d/httpd fullstatus | egrep \"GET|POST\""'

apache: check config and gracefully restart if all ok

alias graceful='/etc/init.d/httpd configtest && /etc/init.d/httpd graceful && echo Gracefully Done' 

get process info

alias pinfo='/root/procinfo' 

redhat release info

alias about='cat /etc/redhat-release && cat /proc/version && uname -a' alias 

start vim in insert mode (contraversial!)

alias vi='vim -c startinsert' 

monitor drbd activity and state of bonded NICs

alias cluster='watch -d -n0.2 cat /proc/drbd /proc/net/bonding/bond0'
Andy
  • 5,230
  • 1
  • 24
  • 34
0

to display squid log file in real time

alias proxy='tail -f /usr/local/squid/var/logs/access.log'

Jindrich
  • 4,968
  • 8
  • 30
  • 42
0
    alias pd=pushd
    alias so="echo ^O"
    alias sy='rsync -vaH'
    alias tcem='echo -ne "\033[?25h"'
    alias tt='traceroute -I'
    alias x=type
sendmoreinfo
  • 1,772
  • 13
  • 34
0

List all the files in this directory, and sort by human-readable file size.

alias sizes="du --max-depth=1 -k | sort -nr | cut -f2 | xargs -d '\n' du -sh"

I use it a lot on some servers with very limited disk space.

bryan kennedy
  • 1,721
  • 3
  • 16
  • 31
0

To add up a column of numbers, I have this in my .bashrc. If you give it a filename as a 2nd arugment, it will sum up the passed-in column number of that file instead of std-in.

sumcol() {
   awk "BEGIN { SUM=0 } { SUM+=\$$1 } END { print SUM }" $2
}
jftuga
  • 5,731
  • 4
  • 42
  • 51
0

I haven't got many aliases, I copied this one from somewhere.

alias dirf='find . -type d | sed -e "s/[^-][^\/]*\//  |/g" -e "s/|\([^ ]\)/|-\1/"
chmeee
  • 7,370
  • 3
  • 30
  • 43
0

I have also:

alias dmeasg='dmesg'

I typed it so often wrong, that I made an alias for that.

Mnementh
  • 1,125
  • 2
  • 11
  • 18
0
alias agc='df -h; apt-get autoclean ; apt-get clean ; apt-get autoremove ; df -h'
alias agi='apt-get install '
alias acs='apt-cache search '
alias agdu='apt-get update ; apt-get dist-upgrade'
jet
  • 475
  • 4
  • 8
0

I may rebuild the same system 5+ times in one day. This helps speed up removing the entry from known_hosts.

remove-known-host() {
local HOST=$1

grep -v $HOST ~/.ssh/known_hosts > ~/.ssh/known_hosts.tmp
mv ~/.ssh/known_hosts.tmp ~/.ssh/known_hosts
}

Aaron Copley
  • 12,525
  • 5
  • 47
  • 68
0

RedHat based systems, e.g. RHEL, CentOS, Fedora come with the following aliases:

alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mc='. /usr/libexec/mc/mc-wrapper.sh'
alias vi='vim'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
Cristian Ciupitu
  • 6,396
  • 2
  • 42
  • 56