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,860 results
git push origin --all
awk "/<xsd:annotation>/{h=1};!h;/<\/xsd:annotation>/{h=0}" annotatedSchema.xsd
2011-07-15 07:17:17
User: 2chg
Functions: awk
Tags: xml filter
-1

Removes all lines between the lines containing "" and "", including these lines itself

Backdrop:

Sometimes when working with XML files without an graphical editor, large comment-/annotation-blocks taper the readability to walk through the file. I like to create a copy of such documents without these annotations. As the documentation itself is in documentation tags inside the annotation tags an therefore graphical editors tend to put the annotation tags in their own lines, this command removes all documentations within annotation-tags.

ip route show | awk '$3 ~ /^[1-9]+/ {print $3;}'
2010-08-08 23:57:23
User: kerim
Functions: awk route
-1

Find your default gateway and print it directly output.

http://www.bayner.com/

kerim@bayner.com

ping -c 1 www.google.com | /usr/bin/awk '{print $7}' | /usr/bin/awk 'NR > 1' | /usr/bin/awk 'NR < 2' | /usr/bin/awk -F"=" '{print $2}'
2010-12-15 08:50:52
User: ackers
Functions: ping
-1

Does one ping to a URL or host, and echo out just the response time. I use this on, with MRTG to monitor the connections to various hosts.

for f in ~/.mcabber/histo/*; do a=`egrep "^(MR|MS)" $f | wc -l`; echo $f: $a | awk -F\/ '{print $6}'; done
dd bs=1 if=/dev/zero of=/path/to/imagename.raw seek=50G count=1 conv=notrunc
mysql -BNe 'select table_name from tables where table_schema="DB-NAME" and table_type="BASE TABLE" and table_name not like "PREFIX%";' information_schema | xargs mysqldump DB-NAME > test.sql
2013-06-20 13:26:18
User: sesom42
Functions: xargs
-1

Replace DB-NAME and PREFIX with your settings. MySQL username and password handled by ~/.my.cnf.

update-alternatives --config x-terminal-emulator
ipcalc $(ifconfig eth0 | grep "inet addr:" | cut -d':' -f2,4 | sed 's/.+Bcast:/\//g') | awk '/Network/ { print $2 } '
ldd path_to_executable
sed -n -e 1186,1210p A-small-practice.in
2011-06-04 10:53:46
User: evandrix
Functions: sed
Tags: sed
-1

Using sed to extract lines in a text file

If you write bash scripts a lot, you are bound to run into a situation where you want to extract some lines from a file. Yesterday, I needed to extract the first line of a file, say named somefile.txt.

cat somefile.txt

Line 1

Line 2

Line 3

Line 4

This specific task can be easily done with this:

head -1 somefile.txt

Line 1

For a more complicated task, like extract the second to third lines of a file. head is inadequate.

So, let's try extracting lines using sed: the stream editor.

My first attempt uses the p sed command (for print):

sed 1p somefile.txt

Line 1

Line 1

Line 2

Line 3

Line 4

Note that it prints the whole file, with the first line printed twice. Why? The default output behavior is to print every line of the input file stream.

The explicit 1p command just tells it to print the first line .... again.

To fix it, you need to suppress the default output (using -n), making explicit prints the only way to print to default output.

sed -n 1p somefile.txt

Line 1

Alternatively, you can tell sed to delete all but the first line.

sed '1!d' somefile.txt

Line 1

'1!d' means if a line is not(!) the first line, delete.

Note that the single quotes are necessary. Otherwise, the !d will bring back the last command you executed that starts with the letter d.

To extract a range of lines, say lines 2 to 4, you can execute either of the following:

sed -n 2,4p somefile.txt sed '2,4!d' somefile.txt

Note that the comma specifies a range (from the line before the comma to the line after).

What if the lines you want to extract are not in sequence, say lines 1 to 2, and line 4?

sed -n -e 1,2p -e 4p somefile.txt

Line 1

Line 2

Line 4

echo "John's" | grep -Po '\b\w+(?<!s)\b'
function ldd(){ objdump -p $1 | grep -i need; }
2010-09-07 02:44:59
User: jyf
Functions: grep objdump
Tags: ldd
-1

use this to avoid executing the target app

touch file{1,2,3,4,5}.sh
sudo ifconfig wlan0 | grep inet | awk 'NR==1 {print $2}' | cut -c 6-
2013-02-18 14:10:07
User: mouths
Functions: awk cut grep ifconfig sudo
-1

On wired connections set 'eth0' instead of 'wlan0'

sqlite3 $HOME/.dropbox/config.db "select value from config where key like '%dropbox_path%'"
2011-06-05 08:26:02
User: mobilediesel
-1

Newer versions of Dropbox let you choose the location for your Dropbox folder. If you use script to put things into your Dropbox folder (todo list, screenshots, torrents etc.) but have the Dropbox folder in different locations on your other computers this lets you use the same script on all systems without having to tell it where the Dropbox folder is.

script -qf | tee >(nc -l -p 5000)
ct find -avobs -nxname -element 'brtype(branch_name)' -print 2>/dev/null
for f in *.ogg ; do mplayer -quiet -vo null -vc dummy -ao pcm:waveheader:file="$f.wav" "$f" ; done
CLASSPATH=.; export CLASSPATH=$CLASSPATH$(find "$PWD" -name '*.jar' -type f -printf ':%p\n' | sort -u | tr -d '\n'); echo $CLASSPATH
openssl req -new -x509 -extensions v3_ca -days 1100 -subj "/C=CA/ST=CA/L=SomeCity/O=EXAMPLE Inc./OU=Web Services/CN=example.com/emailAddress=postmaster@example.com" -nodes -keyout web.key -out web.crt
tar cf - . | (cd /new/dir; tar xvf -)
2009-03-09 20:30:34
User: jauderho
Functions: cd tar
-1

Add z to the flags to enable compression.

head -n X | tail -n 1
2010-07-08 22:06:39
User: infinull
Functions: head tail
-1

using tail first won't do it because tail counts from the bottom of the file. You could do it this way but I don't suggest it

export PATH=$PATH$(find "$PWD" -name '.*' -prune -o -type f -a -perm /u+x -printf ':%h\n' | sort -u | tr -d '\n'); echo $PATH
2011-03-11 19:31:17
User: dexterhu
Functions: echo export find sort tr
-1

(1) don't run twice, or the same folder will occur in $PATH.

(2) otherwise you need to start a new terminal

dpkg -l | sed '/^rc/!d;s/^[^ ]* [^ ]* \([^ ]*\).*/\1/' | xargs -r sudo apt-get -y purge