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,105 results
col() { awk '{print $('$(echo $* | sed -e s/-/NF-/g -e 's/ /),$(/g')')}'; }
2014-06-05 18:01:31
User: tekniq
Functions: awk col echo sed
0

Something I do a lot is extract columns from some input where cut is not suitable because the columns are separated by not a single character but multiple spaces or tabs. So I often do things like:

... | awk '{print $7, $8}'

... which is a lot of typing, additionally slowed down when typing symbols like '{}$ ... Using the simple one-line function above makes it easier and faster:

... | col 7 8

How it works:

The one-liner defines a new function with name col

The function will execute awk, and it expects standard input (coming from a pipe or input redirection)

The function arguments are processed with sed to use them with awk: replace all spaces with ,$ so that for example 1 2 3 becomes 1,$2,$3, which is inserted into the awk command to become the well formatted shell command: awk '{print $1,$2,$3}'

Allows negative indexes to extract columns relative to the end of the line.

Credit: http://www.bashoneliners.com/oneliners/oneliner/144/

dig MX example.com +short | cut -d' ' -f2 | sed '1q;d' | xargs dig +short
sed 's/&/\&amp;/g; s/</\&lt;/g; s/>/\&gt;/g; s/"/\&quot;/g; s/'"'"'/\&#39;/g'
sudo efibootmgr --bootnext `efibootmgr | sed -n "s/^Boot\([0-9]\{4\}\)\* Windows Boot Manager$/\1/p"` && reboot
2014-05-31 11:51:52
User: moneytoo
Functions: sed sudo
0

Quickly reboot directly to Windows (or any other operating system) without the need to enter BIOS/UEFI or boot loader (if any). It's valid for one boot only, no persistent changes to boot priorities.

sed -i 's/^.*?].*?:\s//g' skype-chat-log.txt
2014-05-26 07:48:36
Functions: sed
0

Regular expression search pattern to remove the Datetime and Name when you paste from skype chat into your text editor

From this Gist:

https://gist.github.com/webstandardcss/3967760857d6be470dda

function alls() { alias -p | sed -n "/^alias $1/p" }
2014-05-15 17:22:21
User: busterc
Functions: alias sed
Tags: alias aliases
0

Search, find and list aliases that match a regexp.

# basic search example:

alls c.*

# loose search example:

alls .*sudo.*

# remember to double escape special characters; example:

alls .*\\/.*
git status --porcelain | sed -ne 's/^ M //p' | tr '\n' '\0' | tr -d '"' | xargs -0 vim
2014-05-08 08:36:27
User: uschrisf
Functions: sed tr xargs
Tags: vim git
0

Works even with spaces in filenames.

As an alias in .gitconfig:

[alias]

editchanged = "!git status --porcelain | sed -ne 's/^ M //p' | tr '\\n' '\\0' | tr -d '\"' | xargs -0 vim"

echo thisIsATest | sed -E 's/([A-Z])/_\L\1/g'
strings * |grep -v "Apple" |grep http |uniq |sed "s/<[^>]\+>//g"
for i in `grep -ri "?\|?\|?\|?\|?" * --col | cut -d: -f1 |sort -u `;do sed -i "s/?/\&aacute;/g" $i; sed -i "s/?/\&eacute;/g" $i; sed -i "s/?/\&iacute;/g" $i; sed -i "s/?/\&oacute;/g" $i; sed -i "s/?/\&uacute;/g" $i; echo "HTMLizing file [$i]";done
2014-04-24 11:43:36
User: linuxninja
Functions: cut echo sed sort
0

If we've many files containing (?, ?, ?, ?, ? ) characters instead of ?, ?,... etc,...

we can ue this simple command line running a sed command inside a for loop searching for files containing that characters.

Hope u like it! Enjoy! ;)

openssl rand -hex 6 | sed 's/\(..\)/:\1/g; s/^.\(.\)[0-3]/\12/; s/^.\(.\)[4-7]/\16/; s/^.\(.\)[89ab]/\1a/; s/^.\(.\)[cdef]/\1e/'
2014-04-23 21:43:38
User: phil_m
Functions: sed
Tags: Network bash mac
0

The sed script ensures that the "random" MAC address is unicast (bit 0 of first byte == 0) and local (bit 1 of first byte == 1)

sed -e 's/ .*//' -e 's/\.//' -e 's/^0*//' /proc/loadavg
2014-04-18 19:12:05
User: flatcap
Functions: sed
5

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*//'
cdn() { cd $(ls -1d */ | sed -n $@p); }
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.

echo "this_is_a_test" | sed -r 's/_([a-z])/\U\1/g'
for i in *; do inkscape $i --export-png=`echo $i | sed -e 's/svg$/png/'`; done
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

psql -h <SERVER NAME HERE> -c 'SELECT version();' | grep -v 'version\|---\|row\|^ *$' | sed 's/^\s*//'
2014-03-17 18:36:40
User: pnelsonsr
Functions: grep sed
0

In a multiple PostgreSQL server environment knowing the servers version can be important. Note that psql --version returns just the local psql apps version which may not be what you want. This command dumps the PostgreSQL servers version out to one line. You may need to add more command line options to the psql command for your connection environment.

for i in `sudo /sbin/fdisk -l |grep Disk |grep dev |awk '{ print $2 }' |sed s/://g` ; do sudo /usr/bin/dd if=/dev/urandom of=$i bs=8M & done
2014-03-16 12:04:59
User: aimana007
Functions: awk grep sed sudo
Tags: bash Linux
1

This command will use the fdisk utility to find all block devices on your system, and overwrite them with data from the /dev/urandom non-blocking random number generator.

CAUTION: This will irrevocably erase EVERY SINGLE physical block storage device visible to the fdisk utility, including plugged USB devices, RAID sets, LVM, etc.

netstat -tn 2>/dev/null | grep ':80 ' | awk '{print $5}' |sed -e 's/::ffff://' | cut -f1 -d: | sort | uniq -c | sort -rn | head
2014-03-12 12:43:07
User: copocaneta
Functions: awk cut grep netstat sed sort uniq
2

IP addresses and number of connections connected to port 80.

/usr/bin/lynx -dump -width 500 http://127.0.0.1/whm-server-status | awk 'BEGIN { FS = " " } ; { print $12 }' | sed '/^$/d' | sort | uniq -c | sort -n
cat skype_log | sed -s 's/\(\[.*\]\) \(.*\): \(.*\)/<\2> \3/'
inotifywait -mr -e CREATE $HOME/bin/ | while read i; do chmod +x $(echo "$i" | sed 's/ \S* //'); done
grep -o -P '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\:[0-9]{1,5}\s->\s{5}[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\:[0-9]{1,5}' <capture file> | tr -d ' ' | sed 's/:.....//g' | sort -n | uniq -c | sort -nr
2014-03-05 21:34:42
User: santizo
Functions: grep sed sort tr uniq
1

Sort netflow packet capture by unique connections excluding source port.

bashrc-reload() { builtin unalias -a; builtin unset -f $(builtin declare -F | sed 's/^.*declare[[:blank:]]\+-f[[:blank:]]\+//'); . ~/.bashrc; }
2014-03-02 14:24:18
User: Xk2c
Functions: sed unalias unset
2

Simply sourcing .bashrc does not function correctly when you edit it and change an alias for a function or the other way round with the *same name*.

I therefor use this function. Prior to re-sourcing .bashrc it unsets all aliases and functions.