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 sed

Commands using sed from sorted by
Terminal - Commands using sed - 992 results
for i in /var/spool/cron/tabs/*; do echo ${i##*/}; sed 's/^/\t/' $i; echo; done
2012-07-12 08:07:20
User: harpo
Functions: echo sed
1

This is flatcaps tweaked command to make it work on SLES 11.2

for i in /var/spool/cron/*; do echo ${i##*/}; sed 's/^/\t/' $i; echo; done
sudo apt-get remove $(dpkg -l|awk '/^ii linux-image-/{print $2}'|sed 's/linux-image-//'|awk -v v=`uname -r` 'v>$0'|sed 's/-generic-pae//'|awk '{printf("linux-headers-%s\nlinux-headers-%s-generic*\nlinux-image-%s-generic*\n",$0,$0,$0)}')
amixer -c 0 set Master toggle | tail -1 | awk '{print $4}' | sed "s/[^0-9]//g" ; amixer -c 0 set Speaker toggle >/dev/null; amixer -c 0 set Front toggle >/dev/null
amixer -c 0 set Master toggle | sed -n "$ p" | awk '{print $4}' | sed "s/[^0-9]//g"
curl -s 'http://www.google.com/ig/api?weather=santa+monica,ca'| sed -ne "s/.*temp_c data..//;s/....humidity data.*//;p" | figlet
2012-06-25 03:58:15
User: monza241
Functions: sed
0

Change "santa+monica,ca" to your city name and state, or city name and country. In the USA, you can also use your zip code. In other countries, try using your postal code and country code.

curl -s 'http://www.google.com/ig/api?weather=santa+monica,ca'| sed -ne "s/.*temp_f data..//;s/....temp_c data.*//;p" | figlet
2012-06-25 03:52:11
User: monza241
Functions: sed
0

Change "santa+monica,ca" to your specific location, i.e. "london,england". In the USA you can also use a zip code, in other countries, use your postal code and country code.

expr `echo "123671" | sed -e 's/[0-9]/ + &/g' -e 's/^ +//g'` 20
gcc -E code.c | sed '/^\#/d' | indent -st -i2 > code-x.c
top $(pgrep foo | sed 's|^|-p |g')
2012-06-14 15:13:00
User: michelsberg
Functions: sed top
5
pgrep foo

may return several pids for process foobar footy01 etc. like this:

11427

12576

12577

sed puts "-p " in front and we pass a list to top:

top -p 11427 -p 12576 -p 12577
sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g"
2012-06-12 11:20:46
User: rcbarnes
Functions: sed
0

Handles the color codes intended for 256-color terminals (such as xterm-(256)color and urxvt-unicode-256color), in addition to the standard 16-color ANSI forms. Overkill for strict ANSI output, see other options for something simpler.

#!/bin/zsh SHL='\\e[0;31m' EHL='\\e[0m' while read line; do TEXT=$line for SSTR in $*; do TEXT=$(echo $TEXT | sed -e "s:$SSTR:${SHL}${SSTR}${EHL}:g") done echo -e $TEXT done
rpm -qa | sort | sed -n -e "s/\-[0-9].[0-9]*.*//p" | uniq
2012-06-04 12:36:39
User: tuxknowledge
Functions: rpm sed sort
0

This will list all installed packages on a RedHat/CentOS based system, sort them alphabetically, Parse off the version numbers, and delete any duplicate entries.

This is good if you need to build out a mirrored system or rebuild a failing system.

sed "s|\(2[0-9]\{3\}-[01][0-9]-[0-3][0-9]T[01][0-9]:[0-5][0-9]:[0-5][0-9].[0-9]\{3\}Z\)|$(date -d \1 +%s)000|g"
2012-06-03 14:50:12
User: anthonyf
Functions: sed
0

The match pattern only matches ISO 8601 dates of the form YYYY-MM-DDTHH:mm:SS.sssZ

git ls-files | xargs -n1 git blame --line-porcelain | sed -n 's/^author //p' | sort -f | uniq -ic | sort -nr
2012-06-02 22:04:36
User: hugopeixoto
Functions: sed sort uniq xargs
Tags: statistics git
0

Uses line-porcelain in git blame, which makes it easier to parse the output.

tune2fs -l $(df -P / | awk 'NR==2 {print $1}') | sed -n 's/^.*created: *//p'
2012-05-31 12:12:35
User: forcefsck
Functions: awk df sed tune2fs
0

Find out the earliest installation time of a linux system by getting the / filesystem creation time. This example is only valid the os is installed on an ext2/3/4 filesystem.

ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/ /' -e 's/-/|/'
wki () { dig +short txt "${*// /_}".wp.dg.cx | sed -e 's/^"\(.*\)"$/\1/' -e 's/\([^\]\)"[^\]*"/\1/g' -e 's/\\\(.\)/\1/g' }
2012-05-30 11:36:25
User: kovirobi
Functions: sed
0

This removes the enclosing quotation marks ("), and sticthes the different packets together, e.g. '

mysqlbinlog <logfiles> | grep exec | grep end_log_pos | grep -v exec_time=0 | sed 's/^\(.*exec_time=\([0-9]\+\).*\)/\2 - \1 /' | sort -n
2012-05-30 09:38:02
User: theist
Functions: exec grep sed sort
0

Shows sorted by query time, the headers of mysqlbinlog entries. Then is easy to locate the heavier events on the raw log dump

fdisk -l /dev/sda | grep -E sda[0-9]+ | sed s/*// | awk '{printf ("%s %f ",$1,$2/512); if($2%512){ print "BAD" }else {print "Good"} }' | column -t
2012-05-18 08:34:36
Functions: awk column fdisk grep sed
0

To get most of you HDD/SSD driver you need to make sure you partition are aligned, if not the speed penalty can be up to 50% slower!

this simple one liner will check to see if each partition start sector is divided by 512

you need to change sda with your driver

if you find the one of your partitions is not aligned use gparted to move the start sector of the partition to be divided of 512

iotop -a -p $(sed 's, , -p ,g' <<<`pgrep "_raid|_resync|jbd2"`)
2012-05-17 19:45:02
User: AskApache
Functions: sed
1

Shows the IO of the raid sync

sed -i -e '1i \# -*- coding: utf-8 -*-' yourfile.py
2012-05-15 14:54:06
User: igniteflow
Functions: sed
0

Useful when specifying char encoding for Python and/or your editor

echo `disklabel mfid1s4 | sed -n '$p' | awk '{print $2}'` / 1024 / 1024 | bc -l
lynx -dump http://www.ip2location.com/ | sed -n '/^ *Field Name *Value *$/,/^ *\[_\] *Mobile .*Carrier.*name/p'
for i in *.jpg; do dst=$(exif -t 0x9003 -m $i ) && dst_esc=$(echo $dst | sed 's/ /-/g' ) && echo mv $i $dst_esc.jpg ; done
2012-05-02 07:23:38
User: klisanor
Functions: echo mv sed
Tags: exif date rename
0

The command renames all files in a certain directory. Renaming them to their date of creation using EXIF. If you're working with JPG that contains EXIF data (ie. from digital camera), then you can use following to get the creation date instead of stat.

* Since not every file has exif data, we want to check that dst is valid before doing the rest of commands.

* The output from exif has a space, which is a PITA for filenames. Use sed to replace with '-'.

* Note that I use 'echo' before the mv to test out my scripts. When you're confident that it's doing the right thing, then you can remove the 'echo'... you don't want to end up like the guy that got all the files blown away.

Credits: http://stackoverflow.com/questions/4710753/rename-files-according-to-date-created