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 grep from sorted by
Terminal - Commands using grep - 1,530 results
wmic OS get OSArchitecture /value | grep -Eo '[^=]*$'
2014-03-15 02:04:08
User: lowjax
Functions: get grep
0

Using "wmic get * /value" within any Cygwin shell will return lots of Win/Dos newline junk ie "^M$" at the end of found value line, two lines ("$" Unix newline) above, and three below. This makes storing and or evaluating wmic queries as variables a pain. The method i suggest strips the mentioned junk, only returns the value after "OSArchitecture=", and includes only one Unix style newline. Other methods using sed|awk|cut can only handle the output of wmic cleanly when piped or using multiple sed statements.

wmic OS get OSArchitecture /value | sed 's/\r//g;s/^M$//;/^$/d;s/.*=//'

making

wmic OS get OSArchitecture /value | grep -Eo '[^=]*$'

a much cleaner and slightly less costly alternative.

ls | grep ".txt$" | xargs -i WHATEVER_COMMAND {}
/usr/bin/lynx -dump -width 500 http://127.0.0.1/whm-server-status | grep GET | awk '{print $12 $14}' | sort | uniq -c | sort -rn | head
2014-03-12 13:24:40
User: copocaneta
Functions: awk grep sort uniq
0

List the busiest scripts/files running on a cPanel server with domain showing (column $12).

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 | grep GET | awk '{print $12}' | sort | uniq -c | sort -rn | head
2014-03-12 12:31:34
User: copocaneta
Functions: awk grep sort uniq
0

Easiest way to obtain the busiest website list (sorted by number of process running).

lsof | grep -i deleted | grep REG | grep -v txt | ruby -r 'pp' -e 'STDIN.each do |v| a = v.split(/ +/); puts `:> /proc/#{a[1]}/fd/#{a[3].chop}`; end'
2014-03-11 06:02:09
User: jim80net
Functions: grep
0

Be careful, first run:

lsof | grep -i deleted | grep REG | grep -v txt

Then, give it the boot!

grep -r --include=*.php "something" /foo/bar
2014-03-07 13:26:12
User: avpod2
Functions: grep
0

Actually grep can do recursive search based on file extensions.

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.

lspci -vv | grep 'Ethernet\|Serial' | awk 'NR == 1{ printf $1 } NR == 2 { print " mac " $7 }' | sed ?e 's/-/:/g' -e 's/:f[ef]:f[ef]//g' -e 's/01:00.0/eth0/g' -e 's/01:00.1/eth1/g' -e 's/01:00.2/eth2/g' -e 's/01:00.3/eth3/g' > /etc/iftab && ifrename
2014-03-01 20:07:18
User: PROJAK_SX
Functions: awk grep lspci printf sed
0

for redhat systems works sometimes :S tested on dell poweredge r7+ systems

cat /proc/cpuinfo | grep BogoMIPS | uniq | sed 's/^.*://g' | awk '{print($1 / 4) }'
locate *\\.php|xargs grep --color=always -i -5 "namespace\s.*\W"|less
2014-02-28 13:52:15
Functions: grep locate xargs
0

Another way to view some code by keyword and basic regular expression

arecord -q -f cd -d 1 recvol.wav;sox recvol.wav -n stat 2>&1|grep RMS|grep amplitude|cut -d"." -f2|cut -c 1-2>recvol;echo $((`cat recvol`+1))>recvol;rec -t wav - silence 1 0.1 `cat recvol` -1 1.0 `cat recvol`%|lame -s 44.1 -a -v - >record.mp3
2014-02-27 23:23:55
User: geaplanet
Functions: arecord cd cut echo grep stat
0

It find out the mic recording level at the moment of run the command and if a noise level is higher it starts to record an mp3 file. The resulting file will have only the sounds not the silences.

eval $(history | cut -c 8- | grep "adb connect [0-9]" | tail -1)
2014-02-27 14:44:27
User: CanOrhan
Functions: cut eval grep tail
0

Connects to the last adb connection in history.

ps -eo pmem,comm | grep application-name
2014-02-23 13:21:29
User: Darkstar
Functions: grep ps
0

Displays memory usage for individual instances of an application that spawns multiple instances of itself. This command also works on single process applications.

sudo lsof -i -n -P | grep TCP
udevadm info -q all -n /dev/sdb | grep ID_PATH | cut -d'-' -f 2 | xargs -n 1 lspci -s
echo "Memory:" $(dmidecode --type memory | grep " MB" | awk '{sum += $2; a=sum/1024} END {print a}') "GB"
find . -exec grep -Hn what \{\} \; | less
2014-02-17 09:59:01
User: ynedelchev
Functions: find grep what
0

This command will traverse all of the folders and subfolders under current working directory. For every file inside it, it will do a search inside the content of the file for a specific term 'what'. Then it will print a list of the lines that contain that term (and match that pattern). Each matching line will be preceded with the path and name to the file and then the line number iside taht file wehre the pattern was found. Then the actual content of the matching lien will be printed.

The output will be piped throug less, so that the user can scroll through it if it goes beyond the limits of the current display window.

find . -name *.properties -exec /bin/echo {} \; -exec cat {} \; | grep -E 'listen|properties'
YYYY=2014; MM=02; for d in $(cal $MM $YYYY | grep "^ *[0-9]"); do DD=$(printf "%02d" $d); echo $YYYY$MM$DD; done
2014-02-06 11:31:57
User: fibo
Functions: cal echo grep printf
Tags: cal for loop
1

Edit YYYY and MM at the beginning of the command with the year and month you want.

Note that `DD=$(printf "%02d" $d)` will pad single digit integers with a leading zero.

Substitute `echo $YYYY$MM$DD` at the end of the line with the command you want to launch, for instance

script.pl --yyyymmdd $YYYY$MM$DD

lsblk | grep <mountpoint>
npm ls -g|grep "^[&#9500;&#9492;]\(.\+\)\?[&#9516;&#9472;] "
ls | tr [:upper:] [:lower:] | grep -oP '\.[^\.]+$' | sort | uniq -c | sort
2014-01-30 11:37:27
User: icefyre
Functions: grep ls sort tr uniq
1

displays a list of all file extensions in current directory and how many files there are of each type of extension in ascending order (case insensitive)

lsof -c diff -o -r1 | grep $file
2014-01-29 18:35:28
User: d0g
Functions: diff grep
2

When running a long `diff -r` over folders, this simulates a "verbose" mode where you can see where diff is in the tree.

Replace $file with the first part of the path being compared.

dpkg -l linux-* | awk '/^ii/{ print $2}' | grep -v -e `uname -r | cut -f1,2 -d"-"` | grep -e [0-9] | xargs sudo apt-get -y purge