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,071 results
for f in *; do fn=`echo $f | sed 's/\(.*\)\.\([^.]*\)$/\1\n\2/;s/\./-/g;s/\n/./g'`; mv $f $fn; done
2012-09-29 02:10:00
Functions: mv sed
0

This command can rename all files in a folder changing all the dots in the filename for dashes, but respecting the final dot for the extension.

git for-each-ref --sort='-authordate' --format='%(refname)%09%(authordate)' refs/heads | sed -e 's-refs/heads/--'
source <(egrep '^#define EX_.*' /usr/include/sysexits.h | sed -e 's/#define/declare -r/g' | sed 's/\//#/g' | sed -e 's/\s\{1,\}/ /g' | sed -e 's/ \([0-9]\)/\=\1/'g )
2012-09-21 13:30:37
User: fishdaemon
Functions: egrep sed
-1

Use meaningful exit codes

change "source" to "cat" to view output instead of assigning

sed 1d foo.txt
cp foo.txt foo.txt.tmp; sed '$ d' foo.txt.tmp > foo.txt; rm -f foo.txt.tmp
2012-09-13 20:57:40
User: kaushalmehra
Functions: cp rm sed
Tags: sed unix
-2

sed '$ d' foo.txt.tmp

...deletes last line from the file

top -n2 -d 0.5 | grep ^Cpu | sed 's/[[:alpha:]%]*//g' | awk 'NR == 2 {printf("%.2f\n",100-$5)}'
2012-09-06 12:56:26
User: wizzra
Functions: awk grep sed top
0

Get the current cpu % usage on your system.

sed -n -e "/^\[/h; /priority *=/{ G; s/\n/ /; s/ity=/ity = /; p }" /etc/yum.repos.d/*.repo | sort -k3n
2012-09-06 00:09:09
User: eddieb
Functions: sed sort
0

This probably only works without modifications in RHEL/CentOS/Fedora.

parcellite -c | sed 's/^/ /' | parcellite
2012-09-05 18:00:15
User: brandizzi
Functions: sed
1

This command takes the content of a Parcellite-managed clipboard manager and add one level of indentation to it. It may be useful to indent a block of code which will enter inside another, already indented one but I use it mostly to indent code I will post in Stack Overflow questions and answers.

lsmod | tail -n +2 | cut -d' ' -f1 | xargs modinfo | egrep '^file|^desc|^dep' | sed -e'/^dep/s/$/\n/g'
grep -H voluntary_ctxt /proc/*/status |gawk '{ split($1,proc,"/"); if ( $2 > 10000000 ) { printf $2 " - Process : "; system("ps h -o cmd -p "proc[3]) } }' | sort -nk1,1 | sed 's/^/Context Switches: /g'
2012-09-01 19:43:47
User: jperkster
Functions: gawk grep printf sed sort
0

This command will find the highest context switches on a server and give you the process listing.

od -An -N6 -tx1 /dev/urandom | sed -e 's/^ *//' -e 's/ */:/g' -e 's/:$//' -e 's/^\(.\)[13579bdf]/\10/'
2012-08-31 13:15:03
User: outly3r
Functions: od sed
0

- The last sed expression ensures the unicast/multicast bit is set to zero

- The greedy space replacements are for portability across UNIX seds (note there's TWO spaces and not just one, as this web shows, in s/^ */)

Q="Hello world"; GOOG_URL="http://www.google.com/search?q="; AGENT="Mozilla/4.0"; stream=$(curl -A "$AGENT" -skLm 10 "${GOOG_URL}\"${Q/\ /+}\"" | grep -oP '\/url\?q=.+?&amp' | sed 's/\/url?q=//;s/&amp//'); echo -e "${stream//\%/\x}"
sed -e :a -e '$!N;s/\n/ /;ta'
awk '{print $1}' /proc/net/dev|grep :|sed "s/:.*//g"
sed 's/#.*//' /etc/fstab | column -t
sudo file -bs /dev/sda | sed -e 's/.*partition 1\(.*\) code offset.*/partition 1\1/g' -e 's/\(.\);/\1\n/g'
(cat ~/.bash_history;U='curl -s www.commandlinefu.com';$U/users/signin -c/tmp/.c -d'username=<USER>&password=<PASS>&submit=1'|$U/commands/favourites/json -b/tmp/.c|grep -Po 'nd":.*?[^\\]",'|sed -re 's/.*":"(.*)",/\1/g')>~/.h;HISTFILE=~/.h bash --login
2012-08-17 12:31:51
User: xenomuta
Functions: bash cat grep sed
5

This makes your commandlinefu.com's favorites appear as most recent commands in your history.

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*//'|awk '{printf("linux-headers-%s\nlinux-headers-%s-generic*\nlinux-image-%s-generic*\n",$0,$0,$0)}')
2012-08-15 10:02:12
User: mtron
Functions: awk sed sudo
3

Remove old kernels (*-generic and *-generic-pae) via apt-get on debian/ubuntu based systems. Tested on ubuntu 10.04 - 12.04.

buf () { oldname=$1; if [ "$oldname" != "" ]; then datepart="$(date +%Y-%m-%d).bak"; firstpart=`echo $oldname | cut -d "." -f 1`; newname=`echo $oldname | sed s/$firstpart/$firstpart.$datepart/`; cp -iv ${oldname} ${newname}; fi }
2012-08-15 08:31:44
User: juliushibert
Functions: cp cut sed
1

Appends the input file with the date format YYYY-MM-DD.bak. Also runs silently if you remove the -v on the cp at the end of the function.

echo 00:29:36 | sed s/:/*60+/g | bc
cat index.html | grep -o '<a .*href=.*>' | sed -e 's/<a /\n<a /g' | sed -e 's/<a .*href=['"'"'"]//' -e 's/["'"'"'].*$//' -e '/^$/ d'
for i in $(tmux list-windows -F '#{window_index}'); do panenames=$(tmux list-panes -t $i -F '#{pane_title}' | sed -e 's/:.*$//' -e 's/^.*@//' | uniq); windowname=$(echo ${panenames} | sed -e 's/ /|/g'); tmux rename-window -t $i $windowname; done
2012-08-03 01:43:51
User: mrfixit42
Functions: echo sed
Tags: tmux byobu
1

*I run this with byobu as as a custom status bar entry that runs every 10 seconds by putting it in a script here:

.byobu/bin/10_update_windows

There's no output to stdout, so nothing is displayed on the status bar.

*Presumes that #{pane_title} is set to the hostname or prompt containing the host name. In my case, it's in this format:

$USER@$HOSTNAME:$PWD

The sed commands may need to be modified if your pane_title is different.

*If you want to strip out a common part of a hostname, add the following before '| uniq'

-e 's/[COMMON PART]//'

I use that to strip out the domain of the servers I connect to, leaving the subdomain.

egrep_escape() { echo "$1" |sed -re 's/([\\.*+?(|)^$[])/\\\1/g' -e 's/\{/[{]/g'; }
2012-08-02 16:54:43
User: regnarg
Functions: echo sed
Tags: grep escape
0

Use if you want to include untrusted literal strings in your grep regexes.

I use it to list all mounts below a directory:

dir=/mnt/gentoo; cat /proc/mounts |awk '{print $2}' |egrep "^$(egrep_escape "$dir")(/|$)"

/mnt/gentoo

/mnt/gentoo/proc

/mnt/gentoo/sys

/mnt/gentoo/dev

/mnt/gentoo/home

Works even if $dir contains dangerous characters (e.g. comes from a commandline argument).

dmesg -T|sed -e 's|\(^.*'`date +%Y`']\)\(.*\)|\x1b[0;34m\1\x1b[0m - \2|g'
2012-07-31 22:21:07
User: jlaunay
Functions: dmesg sed
18

Use sed to color the output of a human-readable dmesg output

egrep 'ServerAlias|ServerName' /etc/apache2/sites-enabled/*.conf | awk '{printf "%s\t%s\n",$2,$3}' | sed 's/www.//' | sort | uniq
2012-07-25 12:51:15
User: staeff
Functions: awk egrep sed sort
0

Get a list of all the unique hostnames from the apache configuration files. Handy to see what sites are running on a server. When i saw the command i had some ideas to make it shorter. Here is my version.