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 tagged output from sorted by
Terminal - Commands tagged output - 14 results
sed -n '/jan\|Jan\|JAN\|JAn\|jAn\|jAN\|jaN/p' data.txt > jan-only-data.txt
echo 'foo' | tee >(wc -c) >(grep o) >(grep f)
2013-01-31 09:54:18
User: totti
Functions: echo grep tee wc
Tags: tee output input
5

Output of a command as input to many

git config --local --get remote.origin.url
2011-10-03 09:29:34
User: hced
1

Great way to quickly grasp if a locally cloned repository originates from e.g. github or elsewhere.

strace -e write=1,2 -p $PID 2>&1 | sed -un "/^ |/p" | sed -ue "s/^.\{9\}\(.\{50\}\).\+/\1/g" -e 's/ //g' | xxd -r -p
2010-10-06 19:37:39
User: glaudiston
Functions: sed strace
3

Useful to recover a output(stdout and stderr) "disown"ed or "nohup"ep process of other instance of ssh.

With the others options the stdout / stderr is intercepted, but only the first n chars.

This way we can recover ALL text of stdout or stderr

( x=`tput op` y=`printf %$((${COLUMNS}-6))s`;for i in {0..256};do o=00$i;echo -e ${o:${#o}-3:3} `tput setaf $i;tput setab $i`${y// /=}$x;done; )
6

This is super fast and an easy way to test your terminal for 256 color support. Unlike alot of info about changing colors in the terminal, this uses the ncurses termcap/terminfo database to determine the escape codes used to generate the colors for a specific TERM. That means you can switch your terminal and then run this to check the real output.

tset xterm-256color

at any rate that is some super lean code!

Here it is in function form to stick in your .bash_profile

aa_256 ()

{

( x=`tput op` y=`printf %$((${COLUMNS}-6))s`;

for i in {0..256};

do

o=00$i;

echo -e ${o:${#o}-3:3} `tput setaf $i;tput setab $i`${y// /=}$x;

done )

}

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

bind '"\C-h": "\`fc\ \-s\`"'
2010-08-16 17:58:16
User: rthemocap
0

This is similar to using `!!` or

In bash 4.1 it seems you can bind directly to a shell command, but I'm not running that version.

strace -ff -e write=1,2 -s 1024 -p PID 2>&1 | grep "^ |" | cut -c11-60 | sed -e 's/ //g' | xxd -r -p
2010-04-23 16:22:17
User: systemj
Functions: cut grep sed strace
10

similar to the previous command, but with more friendly output (tested on linux)

G=$(stty -g);stty rows $((${LINES:-50}/2));top -n1; stty $G;unset G
3

One of my favorite ways to impress newbies (and old hats) to the power of the shell, is to give them an incredibly colorful and amazing version of the top command that runs once upon login, just like running fortune on login. It's pretty sweet believe me, just add this one-liner to your ~/.bash_profile -- and of course you can set the height to be anything, from 1 line to 1000!

G=$(stty -g);stty rows $((${LINES:-50}/2));top -n1; stty $G;unset G

Doesn't take more than the below toprc file I've added below, and you get all 4 top windows showing output at the same time.. each with a different color scheme, and each showing different info. Each window would normally take up 1/4th of your screen when run like that - TOP is designed as a full screen program. But here's where you might learn something new today on this great site.. By using the stty command to change the terminals internal understanding of the size of your terminal window, you force top to also think that way as well.

# save the correct settings to G var.

G=$(stty -g)

# change the number of rows to half the actual amount, or 50 otherwise

stty rows $((${LINES:-50}/2))

# run top non-interactively for 1 second, the output stays on the screen (half at least)

top -n1

# reset the terminal back to the correct values, and clean up after yourself

stty $G;unset G

This trick from my [ http://www.askapache.com/linux-unix/bash_profile-functions-advanced-shell.html bash_profile ], though the online version will be updated soon. Just think what else you could run like this!

Note 1: I had to edit the toprc file out due to this site can't handle that (uploads/including code). So you can grab it from [ http://www.askapache.com/linux-unix/bash-power-prompt.html my site ]

Note 2: I had to come back and edit again because the links weren't being correctly parsed

strace -ff -e trace=write -e write=1,2 -p SOME_PID
alias head='head -n $((${LINES:-`tput lines 2>/dev/null||echo -n 12`} - 2))'
26

Run the alias command, then issue

ps aux | head

and resize your terminal window (putty/console/hyperterm/xterm/etc) then issue the same command and you'll understand.

${LINES:-`tput lines 2>/dev/null||echo -n 12`}

Insructs the shell that if LINES is not set or null to use the output from `tput lines` ( ncurses based terminal access ) to get the number of lines in your terminal. But furthermore, in case that doesn't work either, it will default to using the deafault of 12 (-2 = 10).

The default for HEAD is to output the first 10 lines, this alias changes the default to output the first x lines instead, where x is the number of lines currently displayed on your terminal - 2. The -2 is there so that the top line displayed is the command you ran that used HEAD, ie the prompt.

Depending on whether your PS1 and/or PROMPT_COMMAND output more than 1 line (mine is 3) you will want to increase from -2. So with my prompt being the following, I need -7, or - 5 if I only want to display the commandline at the top. ( http://www.askapache.com/linux-unix/bash-power-prompt.html )

275MB/748MB

[7995:7993 - 0:186] 06:26:49 Thu Apr 08 [askapache@n1-backbone5:/dev/pts/0 +1] ~

In most shells the LINES variable is created automatically at login and updated when the terminal is resized (28 linux, 23/20 others for SIGWINCH) to contain the number of vertical lines that can fit in your terminal window. Because the alias doesn't hard-code the current LINES but relys on the $LINES variable, this is a dynamic alias that will always work on a tty device.

/sbin/dumpe2fs /dev/hda2 | grep 'Block size'
2009-05-15 22:23:21
User: rez0r
Functions: grep
Tags: size output block
0

Useful to know, especially if you are dealing with output configurations in block size.

Tested on 'Red Hat'.

ls -S -lhr
2009-04-28 01:28:57
User: rez0r
Functions: ls
3

This command list and sort files by size and in reverse order, the reverse order is very helpful when you have a very long list and wish to have the biggest files at the bottom so you don't have scrool up.

The file size info is in human readable output, so ex. 1K..234M...3G

Tested with Linux (Red Hat Enterprise Edition)

ssh user@server | tee logfilename
2009-04-17 19:17:02
User: bassu
Functions: ssh tee
6

Optionally, you can create a new function to do this with a custom command. Edit $HOME/.bashrc and add:

myssh () { ssh $1 | tee sshlog ; }

Save it.

At command prompt:

myssh user@server
command > tmp && cat logfile.txt >> tmp && tmp > logfile.txt && rm tmp
2009-04-05 22:00:32
User: akoumjian
Functions: cat command rm
-2

Adds the stdout (standard output) to the beginning of logfile.txt. Change "command" to whatever command you like, such as 'ls' or 'date', etc. It does this by adding the output to a temporary file, then adding the previous contents of logfile.txt to the temp file, then copying the new contents back to the logfile.txt and removing the temp file.