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 - 317 results
sed -e 's/ .*//' -e 's/\.//' -e 's/^0*//' /proc/loadavg
2014-04-18 19:12:05
User: flatcap
Functions: sed
2

Show the current load of the CPU as a percentage.

Read the load from /proc/loadavg and convert it using sed:

Strip everything after the first whitespace:

sed -e 's/ .*//'

Delete the decimal point:

sed -e 's/\.//'

Remove leading zeroes:

sed -e 's/^0*//'
echo thisIsATest | sed -r 's/([A-Z])/_\L\1/g'
2014-04-11 13:36:08
User: flatcap
Functions: echo sed
Tags: sed
1

Convert a camelCase string into snake_case.

To complement senorpedro's command.

sed -n "/^.\{73,\}/p" < /path/to/file
2014-03-20 12:31:57
User: flatcap
Functions: sed
0

Filter out lines of input that contain 72, or fewer, characters.

"sed -n" : don't print lines by default

"/^.\{73,\}/" : find lines that start with 73 (or more) characters

"p" : print them

cat /proc/cpuinfo | grep BogoMIPS | uniq | sed 's/^.*://g' | awk '{print($1 / 4) }'
echo -n text | hexdump -C
2014-02-21 09:15:15
User: Tomofumi
Functions: echo hexdump
Tags: sed hex ascii
0

hexdump could be used for conversion too!

quickscript () { filename="$1"; history | cut -c 8- | sed -e '/^###/{h;d};H;$!d;x' | sed '$d' > ${filename:?No filename given} }
2014-02-09 12:19:29
User: joedhon
Functions: cut sed
1

In order to write bash-scripts, I often do the task manually to see how it works. I type ### at the start of my session.

The function fetches the commands from the last occurrence of '###', excluding the function call. You could prefix this with a here-document to have a proper script-header.

Delete some lines, add a few variables and a loop, and you're ready to go.

This function could probably be much shorter...

sed -n 's/.*\(\(\(^\| \)[0-9]\{1,3\}\.\)\{1\}\([0-9]\{1,3\}\.\)\{2\}[0-9]\{1,3\}\) .*/\1/gp'
2014-01-29 23:18:14
User: smkr
Functions: sed
Tags: sed ip address
0

looks for IPs at the beginning of the line or prefixed by a space

iconv -f $(file -bi filename.ext | sed -e 's/.*[ ]charset=//') -t utf8 filename.ext > filename.ext
dpkg-query -W --showformat='${Installed-Size}\t${Package}\n' | sort -nr | less
ls --color=never -1| grep -E "[0-9]{4}"|sed -re "s/^(.*)([0-9]{4})(.*)$/\2 \1\2\3/" | sort -r
prepend () { array=("$@"); len=${#array[@]}; file=${array[$len-1]}; text=${array[@]:0:$len-1}; printf '%s\n' 0a "$text" . w | ed -s "$file"; }
2013-12-09 21:59:26
User: zlemini
Functions: ed printf
Tags: sed replace
3

Syntax:

$ prepend content to add [filename]

Uses ed, so no temp files created.

find -type f -name '*.conf' -exec sed -Ei 's/foo/bar/' '{}' \;
2013-11-21 16:07:06
Functions: find sed
0

note that sed -i is non-standard (although both GNU and current BSD systems support it)

Can also be accomplished with

find . -name "*.txt" | xargs perl -pi -e 's/old/new/g'

as shown here - http://www.commandlinefu.com/commands/view/223/a-find-and-replace-within-text-based-files-to-locate-and-rewrite-text-en-mass.

wget "us.download.nvidia.com$(wget -qO- "$(wget -qO- "nvidia.com/Download/processFind.aspx?psid=95&pfid=695&osid=19&lid=1&lang=en-us"|awk '/driverResults.aspx/ {print $4}'|cut -d "'" -f2|head -n 1)"|awk '/url=/ {print $2}'|cut -d '=' -f3|cut -d '&' -f1)"
2013-11-21 03:04:59
User: lowjax
Functions: awk cut head wget
1

Download latest NVIDIA Geforce x64 Windows7-8 driver from Nvidia's website. Pulls the latest download version (which includes beta). This is the "English" version. The following command includes a 'sed' line to replace "english" with "international" if needed. You can also replace the starting subdomain with "eu." "uk." and others. Enjoy this one liner! 1 character under the max :)

wget "us.download.nvidia.com$(wget -qO- "$(wget -qO- "nvidia.com/Download/processFind.aspx?psid=95&pfid=695&osid=19&lid=1&lang=en-us" | awk '/driverResults.aspx/ {print $4}' | cut -d "'" -f2 | head -n 1)" | awk '/url=/ {print $2}' | sed -e "s/english/international/" | cut -d '=' -f3 | cut -d '&' -f1)"
sed -n '/jan\|Jan\|JAN\|JAn\|jAn\|jAN\|jaN/p' data.txt > jan-only-data.txt
grep -lr "foo" . | xargs sed -i "s/foo/bar/g"
2013-10-28 13:02:47
User: sergeylukin
Functions: grep sed xargs
1

Replace "foo" with "bar" in all files in current directory recursively

sed -i 's/^/ls -l /' output_files.txt
2013-10-07 15:12:53
User: sonic
Functions: sed
Tags: sed replace
0

The original command is great, but I often want to prepend to every line.

say () { mpv $(sed -E "s;([a-Z]*)( |$);http://ssl.gstatic.com/dictionary/static/sounds/de/0/\1.mp3 ;g" <<< $*); }; say hello world "how is it" going
wget -qO - http://www.asciiartfarts.com/random.cgi | sed -n '/<pre>/,/<\/pre>/p' | sed -n '/<table*/,/<\/table>/p' | sed '1d' | sed '$d' | recode html..ascii
for fil in *.JPG; do datepath="$(identify -verbose $fil | grep DateTimeOri | awk '{print $2"_"$3 }' | sed s%:%_%g)"; mv -v $fil $datepath.jpg; done
2013-08-02 01:42:04
Functions: mv
0

Requires ImageMagick.

Extracts date taken from image and renames it properly.

Based on StackOverflow answer.

rhost() { if [[ $1 =~ ^[0-9]+$ ]]; then sed -i "$1"d ${HOME}/.ssh/known_hosts; else echo "rhost [n]"; fi }
2013-08-01 21:10:34
User: lowjax
Functions: echo sed
-1

Quickly remove the conflicting line (key) from current users known_hosts file when there is an SSH host conflict. Very nice when you get tired of writing out full commands. Ideally you would place this into your .bash_profile

Usage: rhost [n]

Example: rhost 33 (removes line 33 from ~/.ssh/known_hosts)

Function assumes the $HOME exists, you could alternatively use "~/.ssh/known_hosts"

Mac OSX likes a space for sed -i "$1" d

svn info | sed -n "/URL:/s/.*\///p"
pgrep -lf
dpkg-query --show --showformat='${Package;-50}\t${Installed-Size}\n' `aptitude --display-format '%p' search '?installed!?automatic'` | sort -k 2 -n | grep -v deinstall | awk '{printf "%.3f MB \t %s\n", $2/(1024), $1}'
2013-07-26 23:18:20
User: EvilDennisR
Functions: awk grep sort
0

The other commands were good, but they included packages that were installed and then removed.

This command only shows packages that are currently installed, sorts smallest to largest, and formats the sizes to be human readable.

Print to screen all the directories above the pwd, showing: mountpoints, symbolic links, and permissions (with colors)
2013-07-25 22:55:08
User: malathion
Functions: screen
Tags: sed namei
-1

Useful for finding out quickly if you're still in the same filesystem or are in a descendant of a symbolic link or mountpoint. For plain text output, unset the color substitutions or strip them from the function.

curl -s $1 | grep -o -i '<a href="//images.4chan.org/[^>]*>' | sed -r 's%.*"//([^"]*)".*%\1%' | xargs wget
2013-07-22 10:33:55
User: bugmenot
Functions: grep xargs
1

first grep all href images then sed the url part then wget