Hide

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again.

Delete that bloated snippets file you've been using and share your personal repository with the world. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.


If you have a new feature suggestion or find a bug, please get in touch via http://commandlinefu.uservoice.com/

Get involved!

You can sign-in using OpenID credentials, or register a traditional username and password.

First-time OpenID users will be automatically assigned a username which can be changed after signing in.

Hide

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for:

Hide

News

2011-03-12 - Confoo 2011 presentation
Slides are available from the commandlinefu presentation at Confoo 2011: http://presentations.codeinthehole.com/confoo2011/
2011-01-04 - Moderation now required for new commands
To try and put and end to the spamming, new commands require moderation before they will appear on the site.
2010-12-27 - Apologies for not banning the trolls sooner
Have been away from the interwebs over Christmas. Will be more vigilant henceforth.
2010-09-24 - OAuth and pagination problems fixed
Apologies for the delay in getting Twitter's OAuth supported. Annoying pagination gremlin also fixed.
Hide

Tags

Hide

Functions

Commands using alias from sorted by
Terminal - Commands using alias - 206 results
alias QUERY='psql -h $MYDBHOST -p 5432 -d $MYDB -U $MYLOGIN --no-align'
2011-04-08 12:44:33
User: cp
Functions: alias
1

alias for psql command line; works similar for Oracles sqlplus commandline interface.

if you do not provide stdin you will end up in the db shell.

alias screensaver='for ((;;)); do echo -ne "\033[$((1+RANDOM%LINES));$((1+RANDOM%COLUMNS))H\033[$((RANDOM%2));3$((RANDOM%8))m$((RANDOM%10))"; sleep 0.1 ; done'
alias rot13="tr a-zA-Z n-za-mN-ZA-M"
alias va='vi ~/.aliases; source ~/.aliases && echo "aliases sourced"'
2011-03-10 06:41:37
User: greggster
Functions: alias echo
7

When setting up a new aliases file, or having creating a new file.. About every time after editing an aliases file, I source it. This alias makes editing alias a bit easier and they are useful right away. Note if the source failed, it will not echo "aliases sourced".

Sub in vi for your favorite editor, or alter for ksh, sh, etc.

alias big='BIG () { find . -size +${1}M -ls; }; BIG $1'
2011-03-10 06:33:00
User: greggster
Functions: alias find
0

This is for bash - make an alias - also a good blueprint for making aliases that take arguments to functions. If for Solaris use "-size +${1}000000c" to replace "-size +${1}M"

alias sagi="yes | sudo apt-get install"
2011-03-06 03:45:47
User: theepicsnail
Functions: alias
Tags: alias apt-get
0

This alias is quicker to type than 'sudo apt-get install', and it automatically says yes to the prompt that shows up sometimes.

alias rdp='rdesktop -u <user> -g 1600x1200 -D -r disk:home=/home -r clipboard:PRIMARYCLIPBOARD'
2011-02-04 16:22:49
User: bbbco
Functions: alias
0

Sets an alias to remote desktop to the specified console, along with options to ensure the RDP session takes up the whole screen, includes a home directory mapping, and clipboard mappings.

alias ping='ping -n'
alias vim="vim -p"
2010-12-30 22:53:33
User: tommed
Functions: alias
Tags: vim vi tab
10

I always add this to my .profile rc so I can do things like: "vim *.c" and the files are opened in tabs.

alias restoremod='chgrp users -R .;chmod u=rwX,g=rX,o=rX -R .;chown $(pwd |cut -d / -f 3) -R .'
2010-12-28 11:42:43
User: Juluan
Functions: alias chmod chown cut pwd users
2

I often use it at my work, on an ovh server with root ssh access and often have to change mod after having finished an operation.

This command, replace the user, group and mod by the one required by apache to work.

alias connectAllMachines='Terminal --maximize -e "ssh server1" --tab -e "ssh server2" --tab -e "ssh server3"'
2010-12-27 08:47:08
User: nordri
Functions: alias
-2

Terminal is part of XFCE Desktop. This will open a tab for every node that we pass in the command line. In a single line we'll connect to nodes of our server farm.

alias kfire='for i in `ps aux | grep [F]irefox `; do echo $i; kill $(($i)); done; '
2010-12-27 01:47:58
User: swerve
Functions: alias echo grep kill
-2

This is an attempt to get a command which I can alias. It's ugly but it works. I'm hoping someone can suggest a cleaner version.

I have tried....

# alias kfire="for i in $( ps aux | grep [F]irefox | awk \'{print $2}\' ); do kill $; done"

# alias kfire=`kill $(ps aux | grep [F]irefox | awk '{print $2}' | tr '\n' ' ')`

# alias kfire='ps au | grep -i [F]irefox | awk \'{ print $2 \'} '

and they all fail in a .bashrc I've tried escaping the quotes and can't find a way to make the single quotes ' that awk wants work. Maybe I'm just stubborn but I don't want to put in a little #!/bin/bash file just so I can kill a firefox process all in one stroke. This script works (it kills the process before it errors out)... it's just ugly and there may be a pretty way to do this.

alias Z=base64&&Z=dG91Y2ggUExFQVNFX1NUT1BfQU5OT1lJTkdfQ09NTUFORExJTkVGVV9VU0VSUwo=&&$(echo $Z|Z -d)
2010-12-24 14:29:19
User: rodolfoap
Functions: alias echo
Tags: touch base64
7

Would create a file with a meaningful title. Dedicated to John Cons, who is annoying us users. Merry Christmas!!!

alias irssi="screen -wipe; screen -A -U -x -R -S irssi irssi"
2010-12-15 09:10:53
User: djsmiley2k
Functions: alias
3

Bash alias for easy irssi within screen, attempts to attach to existing irssi session, if one exists, otherwise creates one - Including wipe for when system reboots and leaves "dead" session.

alias -g R=' &; jobs | tail -1 | read A0 A1 A2 cmd; echo "running $cmd"; fg "$cmd"; zenity --info --text "$cmd done"; unset A0 A1 A2 cmd'
2010-12-13 17:44:36
User: pipeliner
Functions: alias echo fg jobs read tail unset
1

make, find and a lot of other programs can take a lot of time. And can do not. Supppose you write a long, complicated command and wonder if it will be done in 3 seconds or 20 minutes. Just add "R" (without quotes) suffix to it and you can do other things: zsh will inform you when you can see the results.

You can replace zenity with other X Window dialogs program.

alias duh='dulist=$(du -sh */); for i in T G M K; do printf "$dulist"|egrep "^[0-9\.]+$i" | sort -rn; done'
2010-12-07 11:11:26
User: forcefsck
Functions: alias du egrep printf sort
Tags: disk usage
-1

Alias to produce a list of all subdir sizes in current dir, in reverse order and human readable units. du is executed only once. Remove the slash after the asterisk to include files.

alias cd='pushd'; alias cd-='popd'
2010-11-30 16:44:46
User: conan
Functions: alias
-2

You know there 'cd -' to go to the previous directory you were standing before, but it will no record more than one. With these alias you can now record all your directory changes and go back whenever you need it. However you will have to get accustomed to use 'cd ~' from now on to go to your home directory.

alias capture='IMAGE="/home/user/Pictures/capture-`date +%Y%m%d%H%M%S`.png"; import -frame $IMAGE; echo "Image saved as $IMAGE"'
alias tarred='( ( D=`builtin pwd`; F=$(date +$HOME/`sed "s,[/ ],#,g" <<< ${D/${HOME}/}`#-%F.tgz); tar --ignore-failed-read --transform "s,^${D%/*},`date +${D%/*}.%F`,S" -czPf "$F" "$D" &>/dev/null ) & )'
2010-11-18 06:24:34
User: AskApache
Functions: alias date tar
7

This is freaking sweet!!! Here is the full alias, (I didn't want to cause display problems on commandlinefu.com's homepage):

alias tarred='( ( D=`builtin pwd`; F=$(date +$HOME/`sed "s,[/ ],#,g" <<< ${D/${HOME}/}`#-%F.tgz); S=$SECONDS; tar --ignore-failed-read --transform "s,^${D%/*},`date +${D%/*}.%F`,S" -czPf "$"F "$D" && logger -s "Tarred $D to $F in $(($SECONDS-$S)) seconds" ) & )'

Creates a .tgz archive of whatever directory it is run from, in the background, detached from current shell so if you logout it will still complete. Also, you can run this as many times as you want, if the archive .tgz already exists, it just moves it to a numbered backup '--backup=numbered'. The coolest part of this is the transformation performed by tar and sed so that the archive file names are automatically created, and when you extract the archive file it is completely safe thanks to the transform command.

If you archive lets say /home/tombdigger/new-stuff-to-backup/ it will create the archive /home/#home#tombdigger#new-stuff-to-backup#-2010-11-18.tgz Then when you extract it, like tar -xvzf #home#tombdigger#new-stuff-to-backup#-2010-11-18.tgz instead of overwriting an existing /home/tombdigger/new-stuff-to-backup/ directory, it will extract to /home/tombdigger/new-stuff-to-backup.2010-11-18/

Basically, the tar archive filename is the PWD with all '/' replaced with '#', and the date is appended to the name so that multiple archives are easily managed. This example saves all archives to your $HOME/archive-name.tgz, but I have a $BKDIR variable with my backup location for each shell user, so I just replaced HOME with BKDIR in the alias.

So when I ran this in /opt/askapache/SOURCE/lockfile-progs-0.1.11/ the archive was created at /askapache-bk/#opt#askapache#SOURCE#lockfile-progs-0.1.11#-2010-11-18.tgz

Upon completion, uses the universal logger tool to output its completion to syslog and stderr (printed to your terminal), just remove that part if you don't want it, or just remove the '-s ' option from logger to keep the logs only in syslog and not on your terminal.

Here's how my syslog server recorded this..

2010-11-18T00:44:13-05:00 gravedigger.askapache.com (127.0.0.5) [user] [notice] (logger:) Tarred /opt/askapache/SOURCE/lockfile-progs-0.1.11 to /askapache-bk/tarred/#opt#SOURCE#lockfile-progs-0.1.11#-2010-11-18.tgz in 4 seconds

Caveats

Really this is very robust and foolproof, the only issues I ever have with it (I've been using this for years on my web servers) is if you run it in a directory and then a file changes in that directory, you get a warning message and your archive might have a problem for the changed file. This happens when running this in a logs directory, a temp dir, etc.. That's the only issue I've ever had, really nothing more than a heads up.

Advanced:

This is a simple alias, and very useful as it works on basically every linux box with semi-current tar and GNU coreutils, bash, and sed.. But if you want to customize it or pass parameters (like a dir to backup instead of pwd), check out this function I use.. this is what I created the alias from BTW, replacing my aa_status function with logger, and adding $SECONDS runtime instead of using tar's --totals

function tarred ()

{

local GZIP='--fast' PWD=${1:-`pwd`} F=$(date +${BKDIR}/%m-%d-%g-%H%M-`sed -u 's/[\/\ ]/#/g'

[[ ! -r "$PWD" ]] && echo "Bad permissions for $PWD" 1>&2 && return 2;

( ( tar --totals --ignore-failed-read --transform "s@^${PWD%/*}@`date +${PWD%/*}.%m-%d-%g`@S" -czPf $F $PWD && aa_status "Completed Tarp of $PWD to $F" ) & )

}

#From my .bash_profile http://www.askapache.com/linux-unix/bash_profile-functions-advanced-shell.html

alias whichall='{ command alias; command declare -f; } | command which --read-functions --read-alias -a'
2010-11-18 03:32:04
User: AskApache
Functions: alias command which
2

5 helpful aliases for using the which utility, specifically for the GNU which (2.16 tested) that is included in coreutils.

Which is run first for a command. Same as type builtin minus verbosity

alias which='{ command alias; command declare -f; } | command which --read-functions --read-alias'

Which (a)lias

alias whicha='command alias | command which --read-alias'

Which (f)unction

alias whichf='command declare -f | command which --read-functions'

Which e(x)ecutable file in PATH

alias whichx='command which'

Which (all) alias, function, builtin, and files in PATH

alias whichall='{ command alias; command declare -f; } | command which --read-functions --read-alias -a'

# From my .bash_profile http://www.askapache.com/linux-unix/bash_profile-functions-advanced-shell.html

alias sete='set|sed -n "/^`declare -F|sed -n "s/^declare -f \(.*\)/\1 ()/p;q"`/q;p"'
2010-11-17 23:58:01
User: AskApache
Functions: alias sed
0

Normally the bash builtin command 'set' displays all vars and functions. This just shows the vars. Useful if you want to see different output then env or declare or export.

Alias 'sete' shows sets variables

alias sete='set|sed -n "/^`declare -F|sed -n "s/^declare -f \(.*\)/\1 ()/p;q"`/q;p"'

Alias setf shows the functions.

alias setf='set|sed -n "/^`declare -F|sed -n "s/^declare -f \(.*\)/\1 ()/p;q"`/,\$p"'

Also see: http://www.commandlinefu.com/commands/view/6899/print-all-environment-variables-including-hidden-ones

At the very least, some cool sed commands!

From my .bash_profile http://www.askapache.com/linux-unix/bash_profile-functions-advanced-shell.html

alias sshv='ssh -vvv -o LogLevel=DEBUG3'
2010-10-30 11:23:52
User: AskApache
Functions: alias
3

When debugging an ssh connection either to optimize your settings ie compression, ciphers, or more commonly for debugging an issue connecting, this alias comes in real handy as it's not easy to remember the '-o LogLevel=DEBUG3' argument, which adds a boost of debugging info not available with -vvv alone.

Especially useful are the FD info, and the setup negotiation to create a cleaner, faster connection.

alias sshdo='ssh -q -t root@localhost -- cd $PWD \&\& sudo'
2010-10-20 22:33:22
User: darkfader
Functions: alias cd
Tags: sudo su ssh-agent
-2

Run program as root by SSH key forwarding instead of sudoers.

Put this alias line in .bashrc or wherever you like. Alias arguments might need extra escaping.

You might wonder about security. But you'd block out root login as much as possible of course. In sshd_config you put this:

PermitRootLogin no

Match Address 127.0.0.1

  PermitRootLogin without-password

alias differ='sdiff --suppress-common-lines'
alias differ='sdiff --suppress-common-lines $1 $2'
2010-10-12 16:38:34
User: rthemocap
Functions: alias
0

Often I need to diff two files and want the output side by side for ease of reading and I don't want to see common lines.

With this alias I just:

differ file1 file2