I've been wondering what command-line aliases (such as in bash, or just even .bat files under Windows) you can't live without?
I'll start off with my favourite:
alias q=exit
i love the shorthands in my bash:
ll = ls -l
la = ls -la
l = ls -ltrA
and lsd = ls -AtlLr | grep ^d
- reinierpost
I rarely cd to a location without wanting to cd back, so I use pushd and popd a lot.
alias d='dirs -v'
alias pu=pushd
alias po=popd
Because cutting and pasting is good when you do similar tasks:
alias hg='history | grep $1'
Or see what's running:
alias psg='ps -ef | grep $1'
Because I frequently miss a space:
alias ls-al='ls -al'
alias cd..='cd ..'
And transpose letters:
alias csv=cvs
alias snv=svn
And, before svn had a no-whitespace diff, I started using this, and still do:
alias svndiff='svn diff --diff-cmd=`which diff` -x -w $1'
#define flase false
. I've seen that around a bit. - CamJackson
alias ls='ls --color=auto'
...to get colour-coded directory listings.
My two favorites are meta-aliases:
alias realias='vim ~/.bash_aliases; source ~/.bash_aliases'
alias refunction='vim ~/.bash_functions; source ~/.bash_functions'
This allows me to type realias
or refunction
, write a new alias or function in vim, and as soon as I close vim, the alias or function file is sourced so that the new item is instantly available.
cdd = cd /wherever/i/am/most/likely/to/be/working/on/this/machine
Saves me the most keystrokes:
alias ..='cd ..'
alias .='echo $PWD'
.
does the work of source
e.g .
filename == source filename. - nagul
Not strictly an alias, but a bash function in my .bashrc file:
ff () { find . -name "*$1*" -print; }
Show listening ports on this machine
alias openports='sudo lsof -i -P | grep -i "listen"'
Everybody loves screen
alias sre="screen -xr"
My own take on these [1]
alias l='ls -lA'
alias la='ls -laA'
Change to and list directory
function cdl { cd $1; l; }
Create and change to directory
function mkcd {
if [ $# -ne 1 ]; then
echo "usage: mkcd directory_name"
elif [ -d "${1}" ]; then
echo "(directory already existed)"
cd "$1"
elif [ -e "${1}" ]; then
echo "file exists"
else
mkdir "${1}" && cd "${1}"
fi
}
Remove working directory, if it's empty
function rmwd {
if (shopt -s nullglob dotglob; f=(*); ((! ${#f[@]}))); then
# directory is empty
rmdir `"pwd"`
cd ..
else
echo "not empty"
fi
}
[1] http://superuser.com/questions/7083/useful-command-line-aliases/7103#7103alias sair='sudo aptitude install --with-recommends'
alias srch='apt-cache search'
Lovely for any Ubuntu or Debian system.
A few picked from my .bash_aliases
:
alias l="ls -l"
alias la="ls -la"
alias m="less"
alias p="cd .."
alias as="aptitude search"
alias ai="sudo aptitude install"
alias df="df -hT | grep -v tmpfs"
alias mk="make"
alias mki="sudo make install"
alias rmbak="rm ./*~"
mkdir
is shortened to md
, copy
is shortened to cp
, and move
is shortened to mv
. And don't get me started about usr
... - Hello71
On windows I always have to make a ls.bat file with dir
inside. I just can't get used to typing dir
everytime I want to see directory contents.
Cywgin [1] has also saved me much headache in going between windows and my preferred unix variants.
[1] http://www.cygwin.com/dir %*
- Joey
Another "must-have" for me:
alias df='df -h'
alias du='du -h'
To automatically get human-readable disk usage information, e.g. "3.9G" instead of "4040808" (bytes).
Another example: which of these listings do you find easier to take in with a quick glance?
/dev/md1 61519828 28402280 29992508 49% /
tmpfs 4099328 8 4099320 1% /lib/init/rw
/dev/mapper/vg0-opt 123854820 74758216 42805404 64% /opt
/dev/md1 59G 28G 29G 49% /
tmpfs 4,0G 8,0K 4,0G 1% /lib/init/rw
/dev/mapper/vg0-opt 119G 72G 41G 64% /opt
On linux
alias x ='startx'
On windows
alias ls ='dir'
I've made a 33% optimization!
dir
under Unix, or ls
under DOS. Of course there where some that replaced common commands to work uniformely, but this "abstraction" leaks too much for my taste. Getting a Command not found
when typing ls is a good way to remind yourself that you are not the system you are used too, and that you should be little more careful (permissions, executable order, and all those lovely inconcistencies). - voyager
I always liked:
alias h='history'
alias g='grep'
alias hg='h|g'
hg foo now gets your foo history, quick smart.
s'pose you could do:
alias hg='history|grep'
but you lose the (sometimes useful) independent h and g.
-r
These are just a few that I use.
alias canhaz='sudo apt-get install'
alias webshare='python -c "import SimpleHTTPServer;SimpleHTTPServer.test()"'
alias fail='kill'
alias sls='screen -ls'
alias rtfm='man'
python -m SimpleHTTPServer
for webshare
. - Mark Szymanski
For safety, given how little time I tend to spend in the Terminal:
alias rm='rm -i'
Because it makes me smile:
alias please=''
For convenience:
alias launch="osascript /Users/Shared/Scripts/launch.applescript"
do shell functions count?
mdcd() {
mkdir -p "$@" && cd "$@"
}
rmalias() {
unalias $1 && sed -i "/alias $1\=/d" ${HOME}/path/to/aliases
}
mkalias() {
echo -e "\n# added `date "+%F %T"`" >> ${HOME}/path/to/aliases
echo "alias $@" >> ${HOME}/path/to/aliases
alias $@
}
alias open="emacsclient -n"
If you'd rather edit everything in Emacs :) Customize server-mode
in Emacs to make this work.
If you use the cal
command, and hail from elsewhere than
Australia, Canada, New Zealand, UK or USA
[1] (or other places with the funny idea that a week begins on Sunday... :-), you might like this:
alias cal='cal -m'
Where -m means:
-m Print a calendar where Monday is the first day of the week, as opposed to Sunday.
Update: As Daniel pointed out in comments, you're probably better off with this:
alias cal='ncal -b'
[1] http://en.wikipedia.org/wiki/Seven-day_week#Week_numberalias cal='ncal -b'
since I got tired of cal
's buggy behaviour related to locales, among other things. - Daniel Andersson
cal -m
no longer works (on recent Ubuntu versions), but ncal -b
does the job. - Jonik
alias lt='ls -lrt'
(Btw: For exit, I simply type ctrl-d)
alias latest='ls -Strr --group-directories-first --color=auto | tail'
// start, stop, restart, reload daemons in Arch Linux
usage: start <daemon-name>
start()
{
for arg in $*; do
sudo /etc/rc.d/$arg start
done
}
stop()
{
for arg in $*; do
sudo /etc/rc.d/$arg stop
done
}
restart()
{
for arg in $*; do
sudo /etc/rc.d/$arg restart
done
}
reload()
{
for arg in $*; do
sudo /etc/rc.d/$arg reload
done
}
alias reboot='/usr/local/bin/sobriety-check reboot'
alias poweroff='/usr/local/bin/sobriety-check poweroff'
The above aliases reference scripts I've written that do a uname -a
, sleep 10
, and then ask me to enter a sentence to confirm the reboot or poweroff.
For navigating deep directory trees:
alias ..='cd ..'
alias ..2='cd ../..'
alias ..3='cd ../../..'
alias ..4='cd ../../../..'
alias ..5='cd ../../../../..'
alias ..6='cd ../../../../../..'
alias ..7='cd ../../../../../../..'
alias ..8='cd ../../../../../../../..'
alias ..9='cd ../../../../../../../../..'
alias ld='ls -l | grep ^d'
List just directories.
A couple of my favourites:
Getting the size of the current directory:
alias du-dir='du -h -d 1' (on OS X)
alias du-dir='du -h --max-depth=1' (on Linux)
Quickly logging into servers I use a lot:
alias server-name='ssh remote-id@remote.com'
On the Mac another really great one is automating the open
command for items you need a lot. The open
command asks the system to open the file indicated as if you double clicked on it from the GUI. For example, to get the Activity Monitor from the command line you can do:
alias activity='open /Applications/Utilities/Activity\ Monitor.app/'
A similar idea could be used for other apps, or regularly needed files. Very handy!
I like those:
alias push='pushd .'
alias pus=push
alias pu=push
alias p=push
alias pop=popd
alias po=popd
alias sp='source ~/.profile'
alias sb='source ~/.bashrc'
alias gp='gvim ~/.profile'
alias gb='gvim ~/.bashrc'
alias tbz='tar --use-compress-program=bzip2'
alias nn='netstat -r ; netstat -e ; netstat -a'
alias no='yes | sed "y/y/n/"'
alias h=history
To make sure you're in the canonical path for whatever directory you're in. Useful whenever you're using symlinks.
alias "cwd"='cd `pwd -P`; pwd
This is not an alias, but works the same way:
function cd() { builtin cd "${@:-$HOME}" && ls; }
This function redefined the cd command to 1) cd, then 2) list files. Normally, I issue an ls command immediately after the cd command, so this function naturally combine both.
I like to browse through bash like I would with any GUI file-manager, so I use terms that seem more natural to me.
alias go='pushd'
alias back='popd'
alias up='cd ..'
spawn() {
( "$@" & ) &> /dev/null
}
Very useful for launching graphical apps. (The braces work kind of like nohup
or disown
.)
Because it annoys me that (AFAIK) it's the one command with a longer name in *nix than in Windows:
alias tracert='traceroute'
clear
command - Pascal Thivent
I never (anymore) overload standard names with aliases which have different default behavior - like "rm" to "rm -i", because I've trained myself to rely on it in the past, and boy did I make some stuff ups.
No more, train those fingers to use standard commands. But when the standard version of the command is a lightweight, then I can replace it, like my very favorite which replacement.
Not an alias, but bash functions are very similar in power to csh aliases. Bash aliases don't cut the mustard for lots of stuff.
(Edit, rwhich is a "real which, which is part of which infrastructure I left out before.)
if alias which 2>/dev/null 1>&2 ; then unalias which; fi
unset which
alias rwhich="`which which`"
function which ()
{
if [ x"$1" == x ] ; then
echo "Usage: which command" 1>&2
return 1
fi
# @todo Fix this so it doesn't require executing the command twice.
# Using /dev/tty as the redirection for stdout causes problems for
# redirecting the function. :-(
(alias ; declare -f) | \
rwhich --tty-only --read-function --read-alias --show-dot \
--show-tilde "$@" 2>/dev/null
local fn_errors="`(alias ; declare -f) | \
rwhich --tty-only --read-function --read-alias --show-dot \
--show-tilde \"$@\" 2>&1 1>/dev/null`"
local help_result="`help ${!#} 2>&1 | head -1 | cut -d\: -f1`"
if [ "x%" == "x${!#}" ] ; then
#Special case. Bah.
if [ -n "$fn_errors" ] ; then
echo $fn_errors 1>&2;
return 1
fi
elif [ "x$help_result" == "x${!#}" ] ; then
echo ${!#} is a bash builtin
elif [ "x[[" == "x${!#}" ] ; then
#Special case. Bah.
echo ${!#} is a bash builtin
elif [ "x((" == "x${!#}" ] ; then
#Special case. Bah.
echo ${!#} is a bash builtin
elif [ "x:" == "x${!#}" ] ; then
#Special case. Bah.
echo ${!#} is a bash builtin
elif [ -n "$fn_errors" ] ; then
echo $fn_errors 1>&2;
return 1
fi
}
I usually have:
+ grep='grep -irn --color'
=> coloring, case-insensitive, recursive and shows line number
+ ls='ls -lGh --color --time-style="+%T %d/%m/%Y"'
=> long list, doesn't show groups, show human-readable sizes, coloring, and changes time style for brazilian way
I also uses an alias for each database connection I have.
At work i have three: development, testing and production; so I have those aliases: pg-d
, pg-t
, and pg-p
that calls psql
in an specific host, with user, asking for password.
At home i make freelances, so I have aliases like sabia
that does mysql -u sabia -p
, for example.
Mine, on Windows:
Also, I use Take Command [2] as a replacement for CMD.
[1] http://www.flos-freeware.ch/notepad2.htmlHere are a few I've found useful depending on the environment:
alias ssh='ssh -X' # export DISPLAY to this Xserver
alias rm='rm -i' # make sure I want to delete
alias pgr='pgrep'
alias pk='pkill'
alias more='less' # a MUST because I type 'more' without thinking
alias vi='vim' # only works in certain instances
~/.ssh/config
, so you can still disable X tunneling for really slow (e.g. inter-planetary, or via 1+ satellite(s)) connections. - mctylr
s1 = screen -dRRS chaos1
s2 = screen -dRRS chaos2
s3 = screen -dRRS chaos3
s4 = screen -dRRS chaos4
s5 = screen -dRRS chaos5
s6 = screen -dRRS chaos6
screen
's window management. - chaos
alias wsplit='xrandr --output LVDS --pos 0x880'
Used when my laptop is connected to another screen - splits the virtual screen and gives me another workspace for free.
I use the following alias for quickly checking out the changed files in a directory with CVS server:
alias changes='cvs stat | grep Status | grep Up-to-date'
If you are using zsh, I recommend these:
alias -g L="| less"
alias -g S="| sort"
They allow you to do quickly add "less" or "sort" to the end of everything without cumbersome typing, e.g.:
diff -ur foo/ bar/ L
If you are not using zsh, you shouldn't be asking about shell customization. :-)
If you are using zsh, you shouldn't be asking about aliases, but about shell functions. :-)
Sometimes Ctrl+D comes in handy to close a session, but it can't be used directly in Powershell. So I created a function that solves this:
iex "function $([char]4) { exit }"
Then Ctrl+D, Enter closes the shell. Not quite right, but close.
shortcut for sudo plus setting variables in the context of the new process
+() { sudo env "$@" }
Not an alias, but lives in my bashrc:
export CDPATH='/path/on/this/machine/'
The CDPATH environment variable defines additional locations to be searched when you type the cd command.
alias up='cd ..'
alias ..='cd ..'
- Mark Szymanski
alias ls="ls --color"
alias ll="ls -l --color --human-readable"
alias la="ls -l --color --all"
alias df="df -h"
alias du="du -h"
alias locate="locate -i"
alias info="info --vi-keys"
alias ssr="ssh -l root"
#alias less="/usr/share/vim/vim71/macros/less.sh"
alias diff="diff -u"
alias vi="vim -Xp"
alias vim="vim -Xp"
I had posted this [1] on stackoverflow in response to the question: Hidden features of bash, but it's relevant here too.
I have an alias r='fc-s'
, and I find it very useful in some limited cases. To run the last command, just type r
and hit enter, and that's it. Of course, that itself is not very useful because up arrow does the same thing. But you can use r
to run the previous command with substitutions. Let's say your last command was a long command compiling some file:
$ gcc -c <file_name>.c <lots of options> -o <file_name>.o
Now you want to compile another file with the same options and have a corresponding .o
file:
$ r <file_name>=<new_file>
will do it. You don't have to use up arrow, navigate to the right places and then replace them each manually. This can be repeated multiple times, so you can do this next:
$ r <new_file>=<other_file>
Of course, for such a thing you have makefiles, but I hope I have shown that the alias is useful.
I haven't needed the use of this alias a lot, but there have been times that I have been glad that I have this alias!
[1] http://stackoverflow.com/questions/211378/hidden-features-of-bash/1881903#1881903less='less -r'
Displays raw characters, which, among other things, retains color of the text.
LESS
environment to r
and that would be used automatically every time less
is called, not just when you type it on the command line (e.g. when you're viewing man pages). I have export LESS='icM'
in my .bashrc. - Doug Harris
Mine are:
# too lazy :)
alias ..='cd ..'
# remove those pesky .DS_Store OSX files recursively
alias cleandsstore='find . -name ".DS_Store" -type f -print0 | xargs -0 rm -f'
# remove all .svn directories recursively (careful!)
alias cleansvn='find . -type d -name .svn -depth -exec rm -rf {} \;'
# chmod files to 0644 and directories to 0755 recursively
alias fixperm='find $PWD -type f -print0 | xargs -0 chmod 0644 && find $PWD -type d -print0 | xargs -0 chmod 0755'
# remove empty lines and lines beginning with # (usually comments) from stdin, useful to slim down config files
alias stripcomment='sed -e '\''/^\#/d'\'' -e '\''/^$/d'\'''
Cheers ;)
on linux (debian-like) :
huge time savers
alias search="apt-cache search"
alias install="sudo apt-get install"
alias show="apt-cache show"
digging installed package list
alias dgrep="dpkg -l |grep"
ssh to home server (with public key authentication)
alias sshome="ssh -l MY_HOME_USER MY_HOME_IP -p MY_HOME_SSH_EXTERNAL_PORT $*"
character encoding converters
alias utf2iso="iconv -c --from-code=UTF-8 --to-code=ISO-8859-15"
alias iso2utf="iconv -c --to-code=UTF-8 --from-code=ISO-8859-15"
usage : utf2iso utf_file > iso_file
c = 'clear
l = 'ls -Filah'
ggg = 'newgrp <GROUP_NAME>'
xxx = 'xterm -sb -sl 10000 -fg green1 -bg DarkSlateGrey -geometry 65x40 -cr green2'
I've posted this in other "favorite aliases" threads, and it's always controversial, but I stand by it:
sudo apt-get install trash-cli
alias rm=trash
If you're not using Debian, it might actually be alias rm=trash-put
instead.
Now when you "delete" things on the command line, they show up in your trash can.
To edit a file (assuming you have set your $EDITOR environment variable):
alias e="$EDITOR"
My days working under MS-DOS spoiled me. I can't live without this one.
cls=tput\ reset
I prefer tree
over ls
alias t="tree -LFC 1 --dirsfirst"
function te(){
tree -LFC $1 --dirsfirst $2
}
alias hrr='history -c; history -r'
Useful when I experiment a lot with something on the command line, but don't want all that garbage to make it to the .bash_history
file.
Maybe it's silly, but this was useful for a Linux programmer working in a Windows-based company:
pp () { getclip |eval "$@" |putclip } #sed or do sth with clipboard's content.
g { grep \`getclip` $1 $2 $3 * -R }
export IGNOREEOF="2"
in order to avoid a similar uh-oh. - Dennis Williamson