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 from sorted by
Terminal - Commands using sed - 1,082 results
find . -name '*.txt' -exec sed -ir 's/this/that/g' {} \;
zcat a_big_file.gz | sed -ne "$(zcat a_big_file.gz | tr -d "[:print:]" | cat -n | grep -vP "^ *\d+\t$" | cut -f 1 | sed -e "s/\([0-9]\+\)/\1=;\1p;/" | xargs)" | tr -c "[:print:]\n" "?"
2009-02-24 02:57:37
User: DEinspanjer
Functions: sed tr zcat
1

Scans the file once to build a list of line numbers that contain non-printable characters

Scans the file again, passing those line numbers to sed as two commands to print the line number and the line itself. Also passes the output through a tr to replace the characters with a ?

find . -type f -name "*.java" -print0 | xargs -0 -n 1 svn blame | sed -n 's/^[^a-z]*\([a-z]*\).*$/\1/p' | sort | uniq -c | sort -n
sed -e "s/| /\n/g" ~/.bash_history | cut -d ' ' -f 1 | sort | uniq -c | sort -nr | head
echo -en "stats\r\n" "quit\r\n" | nc localhost 11211 | tr -s [:cntrl:] " "| cut -f42,48 -d" " | sed "s/\([0-9]*\)\s\([0-9]*\)/ \2\/\1*100/" | bc -l
N="filepath" ; P=/proc/$(lsof +L1 | grep "$N" | awk '{print $2}')/fd ; ls -l $P | sed -rn "/$N/s/.*([0-9]+) ->.*/\1/p" | xargs -I_ cat $P/_ > "$N"
2009-02-21 02:31:24
User: laburu
Functions: awk cat grep ls sed xargs
5

Note that the file at the given path will have the contents of the (still) deleted file, but it is a new file with a new node number; in other words, this restores the data, but it does not actually "undelete" the old file.

I posted a function declaration encapsulating this functionality to http://www.reddit.com/r/programming/comments/7yx6f/how_to_undelete_any_open_deleted_file_in_linux/c07sqwe (please excuse the crap formatting).

sed 's/[ \t]*$//' < emails.txt | tr 'A-Z' 'a-z' | sort | uniq > emails_sorted.txt
find . -type d -execdir du -sh '{}' ';' | grep -E "[0-9]+K" | sed 's/^[0-9\.]\+K[\t ]\+//' | tr "\n" "\0" | xargs -0 rm -rf
sed -n -e '/postfix\/smtp\[.*status=sent/s/^.*to=<\([^>]*\).*$/\1/p' /var/log/mail.log | sort -u
`find . -iname ".svn" -type d | sed -e "s/^/rm -rfv /g"`
find cvsdir -name Root -exec sed -i 's/oldserver/newserver/' {} \;
2009-02-19 04:31:13
User: mulad
Functions: find sed
0

If your CVS server has moved, here's a way to update your CVS Root files throughout your code tree without checking out a new copy of your files.

for f in *.ogg; do vorbiscomment -l "$f" | sed 's/peter gabriel/Peter Gabriel/' | vorbiscomment -w "$f"; done
2009-02-18 23:54:01
User: mulad
Functions: sed
1

The "vorbiscomment" utility lets you update information such as artist names and song and album tags in an Ogg Vorbis file. You can use this command to fix any mistakes that were made when ripping an album.

sed 's/\(..\)/\1:/g;s/:$//' mac_address_list
2009-02-18 14:38:37
User: sil
Functions: sed
6

I sometimes have large files of MAC addresses stored in a file, some databases need the information stored with the semicolon (makes for easier programming a device) others don't. I have a barcode to text file scanner which usually butchers MAC addresses so this was the fix> I initially did this in awk ;)

awk '{for(i=10;i>=2;i-=2)$0=substr($0,1,i)":"substr($0,i+1);print}' mac_address_list

sed '1!G;h;$!d'
sed 's/$//'
sed 's/$/<ctrl+v><ctrl+m>/'
2009-02-16 20:53:05
User: SiegeX
Functions: sed
4

The ctrl+v,ctrl+m portion represents key presses that you should do. If you do it successfully you should see a ^M character appear.

sed '/./,$!d'
sed '1,55d'
2009-02-16 20:36:25
User: grep
Functions: sed
-4

deletes first 55 lines; change the 55 to whatever number

sed 's/$'"/`echo \\\r`/"
echo "SELECT * FROM table; " | mysql -u root -p${MYSQLROOTPW} databasename | sed 's/\t/","/g;s/^/"/;s/$/"/;s/\n//g' > outfile.csv
2009-02-16 18:55:18
Functions: echo sed
8

This command converts a MySQL query directly into a .csv (Comma Seperated Value)-file.

for I in `find . -name "*.php"`; do sed -i "s/old name/new name/g" $I; done
2009-02-16 17:09:09
User: ruedu
Functions: sed
-1

So you have a web site and you've plastered your significant other's name all over it. But you broke up with them and have some new love in your life. How do you find all those instances of their name and replace them?

sed -i '10d' <somefile>
2009-02-16 15:05:29
User: raphink
Functions: sed
4

Very useful when the ssh key of a host has changed and ssh refuses to connect to the machine, while giving you the line number that has changed in ~/.ssh/known_hosts.

curl -s http://checkip.dyndns.org | sed 's/[a-zA-Z<>/ :]//g'
2009-02-16 07:12:01
User: vaporub
Functions: sed
12

Just another curl command to get your public facing IP

MAC=`(date; cat /proc/interrupts) | md5sum | sed -r 's/^(.{10}).*$/\1/; s/([0-9a-f]{2})/\1:/g; s/:$//;'`
2009-02-16 07:09:43
User: vaporub
Functions: cat md5sum sed
15

Original author unknown (I believe off of a wifi hacking forum).

Used in conjuction with ifconfig and cron.. can be handy (especially spoofing AP's)

echo 'example' | sed -e 's/^\(.\)/\U\1/'