38

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'

closed as off topic by sysadmin1138 Jul 10 '11 at 23:39

Questions on Server Fault are expected to relate to server, networking, or related infrastructure administration within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 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 4: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 Answers 52

1
back='cd $OLDPWD'

I really hate typing that '$' ...

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.

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"
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.

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 $@
}
0
alias uab='unison -rsync -auto -batch'

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

0
alias memusage='ps -o rss,command -waxc | sort -n'
alias ssq='svn status -q'
alias up='cd ..'
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 '

0
alias svndiff='svn diff --diff-cmd=colordiff'
alias df='df -h'

The first line uses colordiff to colorize svn diff output

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'
0

to display squid log file in real time

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

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
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.

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
}
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/"
0

I have also:

alias dmeasg='dmesg'

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

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

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'