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,569 results
smartctl -a /dev/sda |grep Writ |awk '{print $NF/2/1024/1024/1024 " TeraBytes Written"}'
2014-10-21 03:40:32
User: khyron320
Functions: awk grep
2

You must have smartmontools installed for this to work. This also assumes you 512 byte sector sizes, this is pretty standard.

ls | tr '[[:punct:][:space:]]' '\n' | grep -v "^\s*$" | sort | uniq -c | sort -bn
2014-10-14 09:52:28
User: qdrizh
Functions: grep ls sort tr uniq
Tags: sort uniq ls grep tr
3

I'm sure there's a more elegant sed version for the tr + grep section.

gcloud components list | grep "^| Not" | sed "s/|\(.*\)|\(.*\)|\(.*\)|/\2/" | xargs echo gcloud components update
2014-10-13 20:52:25
User: wires
Functions: echo grep sed xargs
0

Google Cloud SDK comes with a package manager `gcloud components` but it needs a bit of `sed` to work. Modify the "^| Not" bit to change the package selection. (The gcloud --format option is currently broken)

url=`curl http://proxybay.info/ | awk -F'href="|" |">|</' '{for(i=2;i<=NF;i=i+4) print $i,$(i+2)}' | grep follow|sed 's/^.\{19\}//'|shuf -n 1` && firefox $url
2014-10-04 19:08:13
User: dunryc
Functions: awk grep sed
-1

polls the pirate bay mirrors list and chooses a random site and opens it for you in firefox

git reflog --date=local | grep "Oct 2 .* checkout: moving from .* to" | grep -o "[a-zA-Z0-9\-]*$" | sort | uniq
2014-10-03 15:12:22
User: Trindaz
Functions: grep sort
0

Replace "Oct 2" in the first grep pattern to be the date to view branch work from

for line in `docker ps | awk '{print $1}' | grep -v CONTAINER`; do docker ps | grep $line | awk '{printf $NF" "}' && echo $(( `cat /sys/fs/cgroup/memory/docker/$line*/memory.usage_in_bytes` / 1024 / 1024 ))MB ; done
gs -dNOPAUSE -sDEVICE=pdfwrite -sOUTPUTFILE=merged.pdf -dBATCH `ls | grep foo`
docker images | grep '' | awk '{print $3}' | xargs docker rmi
docker ps -a | grep 'Exit' | awk '{print $1}' | xargs docker rm
ps -efL | grep <Process Name>
for I in *.CR2; do if [ `exiv2 pr -p a -u $I | grep 'xmp.Rating' | awk '{ print $4 }'` == "1" ]; then echo $I; fi; done
( eval $(grep -z '^DBUS_SESSION_BUS_ADDRESS' /proc/$(pgrep -u $USER plasma-overlay)/environ); export DBUS_SESSION_BUS_ADDRESS; kquitapp plasma-overlay )
2014-09-01 12:49:58
User: mig_hg
Functions: eval export grep
0

So this first obtains address of the DBUS session, as it's not available by default over SSH. Then it tells plasma-overlay to exit. `kquitapp` is pretty much an equivalent of the qdbus calls.

netstat -anp | grep ":<port>"
cat /var/log/syslog | grep score= | awk '{print $15}' | more
tail -1f /var/opt/fds/logs/TraceEventLogFile.txt.0 | grep <msisdn> | tee <test-case-id>.trace | tr '|' '\n'
2014-08-21 19:29:07
User: neomefistox
Functions: grep tail tee tr
0

This command allows to follow up a trace on SDP (CS5.2), at the same time as the trace records are stored in the file with "raw" format.

Trace files in native format are useful to filter the records before to translation from '|' to '\n'.

Example:

grep -v OP_GET <raw-records>.trace | tr '|' '\n'
grep Failed auth.log | rev | cut -d\ -f4 | rev | sort -u
2014-08-14 14:57:41
User: supradave
Functions: cut grep rev sort
0

Find the failed lines, reverse the output because I only see 3 indicators after the IP address, i.e. port, port#, ssh2 (in my file), cut to the 4th field (yes, you could awk '{print $4}'), reverse the output back to normal and then sort -u (for uniq, or sort | uniq).

ls -la | grep ^l
tail -f LOG_FILE | grep --line-buffered SEARCH_STR | cut -d " " -f 7-
2014-08-07 10:40:45
User: pjsb
Functions: cut grep tail
Tags: grep cut tail -f
0

Outputs / monitors the content of the LOG_FILE , which matches the SEARCH_STR. The output is cutted by spaces (as delimiter) starting from column 7 till the end.

yum list installed| awk '{print $1}'| grep -e "x86" -e "noarch" | grep -v -e '^@'| sort
2014-08-06 23:13:24
Functions: awk grep
0

Great for moves, re-installs etc since it is not version specific yet is architecture specific.

Centos yum list is well know for wrapping lines .

netstat -tn 2>/dev/null | grep :80 | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -nr | head
watch -n5 ss \| grep -c WAIT
2014-07-30 17:09:08
User: djpohly
Functions: grep watch
Tags: tcp ss iproute2
0

Uses the suggestions by jld on #12421 as well as the new iproute2 tools instead of old net-tools.

ifconfig eth0 | grep inet | awk '{ print $2 }'
2014-07-23 20:43:15
User: smorg
Functions: awk grep ifconfig
Tags: centos
0

I just use this to see my ip on the server I'm working on

grep -r "<script" | grep -v src | awk -F: '{print $1}' | uniq
2014-07-23 06:24:31
User: sucotronic
Functions: awk grep
Tags: PHP javascript
2

Useful to crawl where the javascript is declared, and extract it a common file. You can redirect it to a file to review item by item.

postqueue -p | grep -A 1 "over quota" | grep @ | sort | uniq | tr --delete ' '
find . -name "*.php" -exec php -l {} \; | grep found
2014-07-11 14:17:34
User: azizsaleh
Functions: find grep
0

Same but will only returns the invalid file (great when emailing the list to the team).