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

All commands from sorted by
Terminal - All commands - 11,587 results
xclip -o > /tmp/spell.tmp; aspell check /tmp/spell.tmp ; cat /tmp/spell.tmp | xclip
2009-03-26 00:49:59
User: alperyilmaz
Functions: cat
2

xclip -o > /tmp/spell.tmp # Copy clipboard contents to a temp file

aspell check /tmp/spell.tmp # Run aspell on that file

cat /tmp/spell.tmp | xclip # Copy the results back to the clipboard, so that you can paste the corrected text

I'm not sure xclip is installed in most distributions. If not, you can install x11-apps package

% screen -r someuser/
2009-03-25 23:59:38
User: totoro
Functions: screen
38

If you enable multiuser, then you can permit others to share your screen session. The following conditions apply:

1. screen must be suid root;

2. "multiuser on" must be configured in ~/.screenrc;

3. control the others user(s) access with "aclchg":

# ----- from ~/.screenrc-users -----

aclchg someuser +rx "#?" #enable r/o access to "someuser"

aclchg someuser -x "#,at,aclchg,acladd,acldel,quit" # don't allow these

aclchg otheruser +rwx "#?" # enable r/w access to "otheruser"

aclchg otheruser -x "#,at,aclchg,acladd,acldel,quit" # don't allow them to use these commands

# -----

After doing this (once), you start your session with:

$ screen

Then, the other user can join your terminal session(s) with youruserid:

$ screen -r youruserid/

Note: the trailing "/" is required.

Multiple users can share the same screen simultaneously, each with independent access controlled precisely with "aclchg" in the ~/.screenrc file.

I use the following setup:

# ~/.screenrc-base

# default screenrc on any host

source $HOME/.screenrc-base

source $HOME/.screenrc-$HOST

source $HOME/.screenrc-users

# -----

Then, the base configurations are in ~/.screenrc-base; the host-specific configurations are in ~/.screenrc-$HOST, and the user configurations are in ~/.screenrc-users.

The host-specific .screenrc file might contain some host-specific screen commands; e.g.:

# ~/.screen-myhost

# -----

screen -t 'anywhere' /bin/tcsh

screen -t 'anywhere1' /bin/tcsh

# ----

The .screenrc-base contains:

# ~/.screenrc-base

## I find typing ^a (Control-a) awkward. So I set the escape key to CTRL-j instead of a.

escape ^Jj

termcapinfo xterm* ti@:te@:

autodetach on

zombie kr

verbose on

multiuser on

function fork () { tf=$(tempfile -d /tmp -p $1.);echo -n "$tf "; $@ &>$tf& }
2009-03-25 23:33:54
1

This function runs a program in the background, and logs all output to an automatically created logfile. That way, you can still get at the output without it clogging up your terminal.

Tip:

Throw fork() and this:

for prog in firefox kate konqueror ;do alias $prog="fork $prog";done

into your bashrc, so that they'll automatically run out of the way.

function nuke() { if [ $(whoami) != "root" ] ; then for x in $@; do sudo apt-get autoremove --purge $x; done; else for x in $@; do apt-get autoremove --purge $x; done; fi }
2009-03-25 23:21:21
User: ruinbox
Functions: sudo
Tags: sudo apt-get
1

You can't stand programs x, y, and z. Remove all trace of their existence by adding this function to your config. It will remove the cruft, the settings, and such and such. This function doesn't even give a damn about you trying to remove programs that don't exist: it'll just for loop to the next one on your hit list.

nc -l 8000
2009-03-25 23:09:38
User: penpen
Tags: Linux unix WWW
2

Have netcat listen on port 8000, point browser to http://localhost:8000/ and you see the information sent. netcat terminates as soon as your browser disconnects.

I tested this command on my Fedora box but linuxrawkstar pointed out that he needs to use

nc -l -p 8000

instead. This depends on the netcat version you use. The additional '-p' is required by GNU netcat that for example is used by Debian but not by the OpenBSD netcat port used by my Fedora system.

watch -n 1 :
2009-03-25 23:00:28
User: penpen
Functions: watch
Tags: Linux unix
-2

'watch' repeatedly (default every 2 seconds, -n 1 => every second) runs a command (here ':', a shorthand for 'true'), displays the output (here nothing) and the date and time of the last run.

I thought it to be obvious but it seemingly is not: to exit use Ctrl-C.

ls \\someserver\c$\inetpub\wwwroot -r -i web.config | Select-String "SomeMachineName"
2009-03-25 22:36:58
User: cbilson
Functions: ls
-2

Finds all files of a certain name and reports all line with the string. Very simple.

rsync -avz -e 'ssh -A sshproxy ssh' srcdir remhost:dest/path/
2009-03-25 21:29:07
User: totoro
Functions: rsync
5

If you have lots of remote hosts sitting "behind" an ssh proxy host, then there is a special-case use of "rsynch" that allows one to easily copy directories and files across the ssh proxy host, without having to do two explicit copies: the '-e' option allows for a replacement "rsh" command. We use this option to specify an "ssh" tunnel command, with the '-A' option that causes authentication agent requests to be forwarded back to the local host. If you have ssh set up correctly, the above command can be done without any passwords being entered.

/scripts/rickrollyes
2009-03-25 21:24:12
User: sliggins
-37

Mass suspends all User accounts on a Cpanel server and inputs a RedirectMatch to the Rick Roll video. Learn more at rainbowblast com

rsync --rsync-path 'sudo rsync' username@source:/folder/ /local/
2009-03-25 21:18:55
User: Alioth
Functions: rsync
Tags: rsync
18

If your user has sudo on the remote box, you can rsync data as root without needing to login as root. This is very helpful if the remote box does not allow root to login over SSH (which is a common security restriction).

function crtonl { perl -i -ape 's/\r/\n/g;' $* ; }
2009-03-25 20:28:32
User: totoro
Functions: perl
Tags: files
-2

Many Mac OS X programs, especially those in Microsoft:Office, create ASCII files with lines terminated by CRs (carriage returns). Most Unix programs expect lines separated by NLs (newlines). This little command makes it trivial to convert them.

function headers { head -1 $* | tr ',' '\12' | pr -t -n ; }
2009-03-25 20:07:47
User: totoro
Functions: head pr tr
Tags: CSV headers
0

This little command (function) shows the CSV header fields (which are field names separated by commas) as an ordered list, clearly showing the fields and their order.

function t { ls -ltch $* | head -20 ; }
2009-03-25 20:05:52
User: totoro
Functions: head ls
0

Coming back to a project directory after sometime elsewhere?

Need to know what the most recently modified files are?

This little function "t" is one of my most frequent commands.

I have a tcsh alias for it also:

alias t 'ls -ltch \!* | head -20'

find . -type f -depth -3 -mtime -5
2009-03-25 19:54:06
User: totoro
Functions: find
-2

Ever wanted to find the most recently modified files, but couldn't remember exactly where they were in a project directory with many subdirectories? The "find" command, using a combination of "-mtime -N" and "-depth -D" can be used to find those files. If your directory structure isn't very deep, just omit the "-depth -D", but if your directory structure is very deep, then you can limit the depth of the traversal using "-depth -D", where "D" is the maximum number of directory levels to descend.

perl -e 'print scalar localtime $ARGV[0],"\n" ' epoch
du -hS / | perl -ne '(m/\d{3,}M\s+\S/ || m/G\s+\S/) && print'
2009-03-25 18:06:53
User: Alioth
Functions: du perl
6

Finds all directories containing more than 99MB of files, and prints them in human readable format. The directories sizes do not include their subdirectories, so it is very useful for finding any single directory with a lot of large files.

curl -i -X HEAD http://localhost/
rpm -q --qf "%{VERSION}\n" redhat-release
2009-03-25 16:46:14
User: atoponce
Functions: rpm
2

In this case, I'm getting the package version for 'redhat-release', but of course, this can be applied to any package installed on the filesystem. This is very handy in scripts that need to determine just the version of the package, without the package name and all the sed and grep hackery to get to the data you want. To find out all the support format strings that 'rpm --qf' supports:

rpm --querytags
find /proc -regex '/proc/[0-9]+/smaps' -exec grep -l "$PATH_REGEX" {} \; | cut -d'/' -f2
2009-03-25 16:33:44
User: juddmaltin
Functions: cut find grep
1

faster than lsof by at least x2 on my box.

rm -rf `find . -type d -name .svn`
wc -l `find . -name *.php`
find . -wholename './.snapshot' -prune -o -print
2009-03-25 13:52:01
User: jeffaz
Functions: find
3

This can be useful for those who have mounted NetApp file-systems with snapshot activated.

netstat -antuwp | egrep "(^[^t])|(^tcp.*LISTEN)"
test.bat parm1 parm2 parm3
2009-03-25 12:27:18
User: lansa
Functions: test
-34

I am using .bat commands to execute Curl commands for Twitter API

wget -q http://xyz.gpg -O- | sudo apt-key add -
2009-03-25 12:18:36
User: gnuyoga
Functions: sudo wget
Tags: wget apt-key
4

when we add a new package to a aptitude (the debian package manager) we need to add the gpg, otherwise it will show warning / error for missing key