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 sed from sorted by
Terminal - Commands tagged sed - 331 results
googl () { curl -s -d "url=${1}" http://goo.gl/api/url | sed -n "s/.*:\"\([^\"]*\).*/\1\n/p" ;}
curl -s -d'&url=URL' http://goo.gl/api/url | sed -e 's/{"short_url":"//' -e 's/","added_to_history":false}/\n/'
2010-10-01 23:20:08
User: Soubsoub
Functions: sed
5

Use curl and sed to shorten an URL using goo.gl without any other api

check(){ curl -sI $1 | sed -n 's/Location: *//p';}
curl -s http://urlxray.com/display.php?url=http://tinyurl.com/demo-xray | grep -o '<title>.*</title>' | sed 's/<title>.*--> \(.*\)<\/title>/\1/g'
2010-09-30 10:25:18
User: karpoke
Functions: grep sed
Tags: sed grep curl
-3

We can put this inside a function:

fxray() { curl -s http://urlxray.com/display.php?url="$1" | grep -o '<title>.*</title>' | sed 's/<title>.*--> \(.*\)<\/title>/\1/g'; }; fxray http://tinyurl.com/demo-xray
curl -Is slashdot.org | sed -n '5p' | sed 's/^X-//'
macchanger --random interface
2010-09-26 11:12:31
User: JulianTosh
Tags: sed openssl
-4

macchanger will allow you to change either 1) mfg code, 2) host id, or 3) all of the above. Use this at wifi hotspots to help reduce profiling.

openssl rand -hex 6 | sed 's/\(..\)/\1:/g; s/.$//'
2010-09-23 02:31:12
User: putnamhill
Functions: sed
Tags: sed xxd openssl
14

Use the following variation for FreeBSD:

openssl rand 6 | xxd -p | sed 's/\(..\)/\1:/g; s/:$//'
for i in $(find . -iname '*.html'); do sed '/String/d' $i > $i-tmp; mv $i-tmp $i; done
2010-09-21 14:35:18
User: cadu
Functions: find mv sed
Tags: sed find
-3

Search in all html files and remove the lines that 'String' is found.

grep -ZlRr -e BAD_SCRIPT_LINE * |xargs -0 sed -i 's/BAD_SCRIPT_LINE//g'
2010-08-30 22:12:57
User: homoludens
Functions: grep sed xargs
0

recursive find and replace. important stuff are grep -Z and zargs -0 which add zero byte after file name so sed can work even with file names with spaces.

sed 's/^\s*//;s/\s*$//' -i file
2010-08-24 05:03:48
User: kaedenn
Functions: sed
Tags: sed
0

Sometimes those files have more than just spaces and tabs around them. Plus, this is just a little shorter.

man $(ls /bin | sed -n $((RANDOM % $(ls /bin | wc -l) + 1))p)
2010-08-20 17:15:33
User: putnamhill
Functions: ls man sed wc
Tags: man sed ls wc random
-2

Great idea camocrazed. Another twist would be to display a different man page based on the day of the year. The following will continuously cycle through all man pages:

man $(ls /bin | sed -n $(($(date +%j) % $(ls /bin | wc -l)))p)
dir="/bin"; man $(ls $dir |sed -n "$(echo $(( $RANDOM % $(ls $dir |wc -l | awk "{ print $1; }" ) + 1 )) )p")
2010-08-20 16:31:50
User: camocrazed
Functions: dir ls man sed
Tags: man sed awk echo wc
-2

Broaden your knowledge of the utilities available to you in no particular order whatsoever! Then use that knowledge to create more nifty one-liners that you can post here. =p

Takes a random number modulo the number of files in $dir, prints the filename corresponding to that number, and passes it as an argument to man.

ls | sed 's/.*/"&"/'
2010-08-17 15:38:51
User: putnamhill
Functions: ls sed
Tags: sed ls
-5

Looks like you're stuck with sed if your ls doesn't have a -Q option.

sed -i 's/[ \t]*$//' file
2010-07-28 11:41:34
User: yohananov
Functions: sed
Tags: sed
1

The command removes all space and/or tabulation characters preceding new line

echo -n "text" | od -A n -t x1 |sed 's/ /\\x/g'
2010-07-14 15:31:36
User: camocrazed
Functions: echo od sed
Tags: sed hex ascii od
4

If you're going to use od, here's how to suppress the labels at the beginning. Also, it doesn't output the \x, hence the sed command at the end. Remove it for space separated hex values instead

echo -n 'text' | xxd -ps | sed 's/[[:xdigit:]]\{2\}/\\x&/g'
2010-07-13 21:46:30
User: camocrazed
Functions: echo sed
Tags: sed hex ascii
0

Same as another one I saw, just with a cleaner sed command

Edit: updated the sed command to use the [[:xdigit:]] character class - more portable between locales

Note that it will have a newline inserted after every 32 characters of input, due to the output of xxd

echo "${1}" | egrep '^[[:digit:]]*$' ; if [ "$?" -eq 0 ] ; then sed -i "${1}"d $HOME/.ssh/known_hosts ; else printf "\tYou must enter a number!\n\n" ; exit 1 ; fi
2010-07-11 23:09:11
User: DaveQB
Functions: echo egrep exit printf sed
Tags: sed
-13

I have this as a file called deletekey in my ~/bin.

Makes life a little easier.

ssh-keygen -R <the_offending_host>
2010-07-11 19:37:24
User: bunam
Functions: ssh ssh-keygen
Tags: sed
42

In this case it's better do to use the dedicated tool

sed -i 8d ~/.ssh/known_hosts
sed -n '4{p;q}'
variable="foo" && sed 's/bar/'$variable'/g' $variable.conf >> $variable.temp && sed '1,5d' $variable.temp && mv $variable.temp $variable.conf
2010-07-09 22:12:51
User: jdorfman
Functions: mv sed
Tags: bash sed nginx
0

I wrote this script to speed up Nginx configs. This (long) one liner can be run via BASH. You will see that we set a variable in bash called 'foo' and the streamline editor (sed) finds 'bar' in 'foo.conf' next it writes that output to a temp file (foo.temp) and removes the first 5 lines (that aren't needed in this case) & lastly it moves (overwrites) foo.temp to foo.conf

cut -d: -f1 /etc/passwd | sort
sudo lsof|sed 's/ */ /g'|cut -f3 -d' '|sort -u
2010-07-07 08:20:28
User: binaryten
Functions: cut sed sort sudo
-4

Most systems (at least my macbook) have system users defined, such as _www and using "users" for example will not list them. This command allows you to see who the 'virtual' users are on your system.

vim --version | grep -P '^(\+|\-)' | sed 's/\s/\n/g' | grep -Pv '^ ?$'
2010-07-02 02:57:19
User: evaryont
Functions: grep sed vim
Tags: vim sed grep
2

The above output is for a custom compiled version of Vim on Arch Linux.

Just a quick shell one liner, and presents a list of all the enabled and disabled (those prefixed with a '-') features.

statt(){ C=c;stat --h|sed '/Th/,/NO/!d;/%/!d'|while read l;do p=${l/% */};[ $p == %Z ]&&C=fc&&echo ^FS:^;echo "`stat -$C $p \"$1\"` ^$p^${l#%* }";done|column -ts^; }
2010-06-11 23:31:03
User: AskApache
Functions: column read sed
3

This shows every bit of information that stat can get for any file, dir, fifo, etc. It's great because it also shows the format and explains it for each format option.

If you just want stat help, create this handy alias 'stath' to display all format options with explanations.

alias stath="stat --h|sed '/Th/,/NO/!d;/%/!d'"

To display on 2 lines:

( F=/etc/screenrc N=c IFS=$'\n'; for L in $(sed 's/%Z./%Z\n/'<<<`stat --h|sed -n '/^ *%/s/^ *%\(.\).*$/\1:%\1/p'`); do G=$(echo "stat -$N '$L' \"$F\""); eval $G; N=fc;done; )

For a similarly powerful stat-like function optimized for pretty output (and can sort by any field), check out the "lll" function

http://www.commandlinefu.com/commands/view/5815/advanced-ls-output-using-find-for-formattedsortable-file-stat-info

From my .bash_profile ->

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