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,069 results
xxd -p /dev/urandom |fold -60|head -30|sed 's/\(..\)/\1 /g'
2013-02-19 22:18:52
User: psifertex
Functions: fold head sed
1

Replaces hexdump with the more succint xxd, and the sed was unnecessarily complex.

curl -s 'http://xkcd.com/rss.xml' | xpath '//item[1]/description/text()' 2>&1 | sed -n 's/.*title="\([^"]*\)".*/\1/p' | fold -s
2013-02-16 22:38:26
User: putnamhill
Functions: fold sed
Tags: sed curl fold xpath
0

I look at xkcd in my news reader, but it displays the image's title attribute only for a few seconds which makes reading the longer ones more challenging. So I use this to display it in my console.

hexdump -v -e '"%u"' </dev/urandom|fold -60|head -n 30|sed 's/\(.\{2\}\)/\1 /g'
2013-02-16 12:56:24
User: knoppix5
Functions: fold head hexdump sed
6

or

od /dev/urandom -w60 -An|sed 's/ ..../ /g'|head -n 30

(this one lacks digits 8 and 9)

for i in *RET.zip; do unzip -l "$i"| grep -B 4 XXX | grep RET| sed "s/.\+EPS/EPS/" |xargs -I '{}' cp '{}' out/'{}';done;
sudo apt-get <apt-get command and options> --print-uris -qq | sed -n "s/'\([^ ]\+\)' \([^ ]\+\) \([^ ]\+\) MD5Sum:\([^ ]\+\)/wget -c \1/p" > dowload_deb_list.txt
touch file{1..10}.txt ; ls *txt| sed -e "p;s/\.txt$/\.csv/"|xargs -n2 mv
msgfilter --keep-header --input input.po awk '{}' | sed '/^#$/d; /^#[^\:\~,\.]/d' >empty.po
2013-02-08 08:05:32
User: seanf
Functions: awk sed
0

Also removes translator comments. You can remove the header by omitting --keep-header, but if your msgids contain non-ASCII characters you will need the header to specify a suitable charset.

find /path/ -type f -exec grep -l '<string of text>' {} \; | xargs sed -i -e 's%<string of text>%<new text string>%g'
sed -i 's/http:\/\/old\/new\///g' index.html
sed -i "0,/enabled/{s|enabled=0|enabled=1|}" /etc/yum.repos.d/remi.repo
dpkg-query -Wf '${Package}\n' | xargs dpkg --status | sed '/^Conffiles:/,/^Description:/!d;//d' | awk '{print $2 " " $1}' | md5sum -c 2>/dev/null | grep FAILED$ | cut -f1 -d':'
2013-01-31 16:52:38
User: hallmarc
Functions: awk cut grep md5sum sed xargs
0

This functionality seems to be missing from commands like dpkg. Ideally, I want to duplicate the behavior of rpm --verify, but it seems difficult to do this in one relatively short command pipeline.

color()(set -o pipefail;"$@" 2>&1>&3|sed $'s,.*,\e[31m&\e[m,'>&2)3>&1
cd $(ls -1 | sed -n '<N>p')
git grep -l foo | xargs sed -i 's/foo/bar/g'
2013-01-29 16:02:21
User: Schleis
Functions: grep sed xargs
Tags: git
1

Find all the occurrences in the git repo of 'foo' and replace with 'bar'

find $folder -name "[1-9]*" -type f -print|while read file; do echo $file $(sed -e '/^$/Q;:a;$!N;s/\n //;ta;s/ /_/g;P;D' $file|awk '/^Received:/&&!r{r=$0}/^From:/&&!f{f=$0}r&&f{printf "%s%s",r,f;exit(0)}');done|sort -k 2|uniq -d -f 1
2013-01-21 22:50:51
User: lpb612
Functions: awk echo find read sed sort uniq
1

# find assumes email files start with a number 1-9

# sed joins the lines starting with " " to the previous line

# gawk print the received and from lines

# sort according to the second field (received+from)

# uniq print the duplicated filename

# a message is viewed as duplicate if it is received at the same time as another message, and from the same person.

The command was intended to be run under cron. If run in a terminal, mutt can be used:

mutt -e "push otD~=xq" -f $folder

sed '/^\s*[^@%]/s=\([A-Z][A-Z]*\)\([^}A-Z]\|},$\)={\1}\2=g' literature.bib > output.bib
2013-01-15 22:24:17
User: michelsberg
Functions: sed
Tags: sed latex BibTeX
1

It is often recommended to enclose capital letters in a BibTeX file in braces, so the letters will not be transformed to lower case, when imported from LaTeX. This is an attempt to apply this rule to a BibTeX database file.

DO NOT USE sed '...' input.bib > input.bib as it will empty the file!

How it works:

/^\s*[^@%]/

Apply the search-and-replace rule to lines that start (^) with zero or more white spaces (\s*), followed by any character ([...]) that is *NOT* a "@" or a "%" (^@%).

s=<some stuff>=<other stuff>=g

Search (s) for some stuff and replace by other stuff. Do that globally (g) for all matches in each processed line.

\([A-Z][A-Z]*\)\([^}A-Z]\|},$\)

Matches at least one uppercase letter ([A-Z][A-Z]*) followed by a character that is EITHER not "}" and not a capital letter ([^}A-Z]) OR (|) it actually IS a "}", which is followed by "," at the end of the line ($).

Putting regular expressions in escaped parentheses (\( and \), respectively) allows to dereference the matched string later.

{\1}\2

Replace the matched string by "{", followed by part 1 of the matched string (\1), followed by "}", followed by the second part of the matched string (\2).

I tried this with GNU sed, only, version 4.2.1.

curl -s -L http://nytm.org/made-in-nyc | grep "(hiring)" | sed -re 's/<([^>]+)>//g;s/^([ \t]*)//g'
ls -l /dev/disk/by-id | egrep ata-.*`hdparm -i /dev/sda | grep SerialNo | sed 's/.*SerialNo=//' | tr -d "\n"`.*sda$ | sed -e 's/.*ata-/ata-/' -e 's|[ ].*||' | tr -d "\n"
2013-01-07 10:20:25
Functions: egrep grep ls sed tr
Tags: Ubuntu
-2

This was tested on Ubuntu 12.04 (Precise) LTS Server. It returns the name of the symlink within /dev/disk/by-id for the physical drive you specify. Change /dev/sda to the one you want, and replace ata- with scsi- or the appropriate type for your drive.

I used this to pre-configure grub-pc during a non-interactive install because I had to tell it which disk to install grub on, and physical disks don't have a UUID such as that blkid provides.

finger `whoami` | awk -F: '{ print $3 }' | head -n1 | sed 's/^ //'
sed '/foo/ s/foo/foobar/g' <filename>
2013-01-02 08:52:44
User: totti
Functions: sed
Tags: sed file optimize
-6

Use optimized sed to big file/stream to reduce execution time

Use

sed '/foo/ s/foo/foobar/g' <filename>

insted of sed

's/foo/foobar/g' <filename>
curl -s http://en.m.wikipedia.org/wiki/List_of_Internet_top-level_domains | sed -n '/<tr valign="top">/{s/<[^>]*>//g;p}'
2012-12-24 23:54:05
User: flatcap
Functions: sed
1

Quietly get a webpage from wikipedia: curl -s

By default, don't output anything: sed -n

Search for interesting lines: /<tr valign="top">/

With the matching lines: {}

Search and replace any html tags: s/<[^>]*>//g

Finally print the result: p

find -type f | xargs file | grep ".*: .* text" | sed "s;\(.*\): .* text.*;\1;"
for line in `wget --referer='http://500px.com/' --quiet -O- http://500px.com/popular | grep "from=popular" | sed -n 's/.*<img src="\([^"]*\)".*/\1/p' | sed s/"3.jpg"/"4.jpg"/ | sed s/"?t".*$//`; do wget -O $RANDOM.jpg --quiet "$line"; done
2012-12-07 16:14:36
User: bugmenot
Functions: grep sed
0

This command downloads the actual 20 most popular pictures from the website 500px. It uses a random name due to the fact the the pictures in 500px are stored with the same name.

UPDATED: doesn't work if no referrer is specified: --referer='http://500px.com/'

sed "s/^.//g" files