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 grep from sorted by
Terminal - Commands tagged grep - 335 results
find . -type f -exec awk '/linux/ { printf "%s %s: %s\n",FILENAME,NR,$0; }' {} \;
ps -p pid -o logname |tail -1
ps aux | grep PID | grep -v 'grep' | awk '{ print $1 }'
2011-11-05 04:53:29
User: nssy
Functions: awk grep ps
Tags: bash awk grep ps
0

Gets the current system user running a process with the specified pid

nocomments () { cat $1 | egrep -v '^[[:space:]]*#|^[[:space:]]*$|^[[:space:]]*;' | sed '/<!--.*-->/d' | sed '/<!--/,/-->/d'; }
2011-11-04 12:47:39
User: RuizTapiador
Functions: cat egrep sed
Tags: sed grep comment
1

Hide comments and empty lines, included XML comments,

for k in `git branch|sed s/^..//`;do echo -e `git log -1 --pretty=format:"%Cgreen%ci %Cblue%cr%Creset" "$k" --`\\t"$k";done|sort
2011-11-02 06:16:22
User: ryan
Functions: echo sed
1

This fixes a bug found in the other scripts which fail when a branch has the same name as a file or directory in the current directory.

grep -Ilr "<?php" .
grep -n log4j MainPm.java | sed -e 's/^\([^:]*\):\(.*\)/\2 \1/'
2011-10-21 12:50:30
User: bash_vi
Functions: grep sed
Tags: sed grep regex
0

Uses sed with a regex to move the linenumbers to the line end. The plain regex (w/o escapes) looks like that:

^([^:]*):(.*)

#!/bin/bash find | grep -P -v "(class)|(zip)|(png)|(gz)|(gif)|(jpeg)|(jpg)" | xargs -I @ grep -H $1 @
2011-10-14 13:28:49
User: kirdie
Functions: find grep xargs
Tags: find grep
-1

calls grep on all non-binary files returned by find on its current working directory

apt-get remove $(dpkg -l | awk "/^ii linux-(image|headers)/ && ! /`uname -r`/ {print \$2}")
2011-10-09 13:58:47
User: _john
Functions: apt awk
0

since awk was already there one can use it instead of the 2 greps. might not be faster, but fast enough

svn status|grep -iR '^!'|sed 's/!/ /g'|xargs -i svn rm '{}'
2011-10-06 08:11:25
User: erdeszt
Functions: grep rm sed xargs
Tags: svn sed xargs grep
0

Helps if you accidentally deleted files from an svn repo with plain rm and you would like to mark them for svn to delete too.

comm -13 <(sort file1) <(sort file2) > file-new
2011-10-01 18:07:54
User: daa
Functions: comm sort
-2

If both file1 and file2 are already sorted:

comm -13 file1 file2 > file-new

tail -n +<N> <file> | head -n 1
2011-09-30 08:30:30
User: qweqq
Functions: head tail
-5

Tail is much faster than sed, awk because it doesn't check for regular expressions.

grep -Fvf file1 file2 > file-new
2011-09-29 06:37:13
User: wibbel4
Functions: grep
Tags: grep textfile
2

This command compares file2 with file1 and removes the lines that are in file1 from file2. Handy if you have a file where file1 was the origional and you want to remove the origional data from your file2.

grep -rnA 10 TODO *
2011-09-28 16:26:14
User: blockcipher
Functions: grep
Tags: grep
-1

This will recursively go through every file under the current directory showing all lines containing "TODO" as well as 10 lines after it. The output will be marked with line numbers to make it easier to find where the TODO is in the actual file.

while sleep 30; do tput sc;tput cup 0 $(($(tput cols)-15));echo -n " New Emails: $(curl -u username:password --silent https://mail.google.com/mail/feed/atom | grep 'fullcount' | grep -o '[0-9]\+')";tput rc; done &
grep . *
2011-09-01 09:16:04
User: theist
Functions: grep
Tags: cat grep
18

If you have a bunch of small files that you want to cat to read, you can cat each alone (boring); do a cat *, and you won't see what line is for what file, or do a grep . *. "." will match any string and grep in multifile mode will place a $filename: before each matched line. It works recursively too!!

echo $(cat /usr/share/dict/words |grep -v "'"|shuf -n4)
2011-08-31 12:48:14
User: d1v3rdown
Functions: cat echo grep
Tags: cat echo grep shuf
0

Fast and excludes words with apostrophes. For ubuntu, you can use wamerican or wbritish dictionaries, installable through aptitude.

find . -iname '*.conf' | xargs grep "searh string" -sl
grep ^Dirty /proc/meminfo
2011-08-24 08:48:49
User: h3xx
Functions: grep
30

Ever ask yourself "How much data would be lost if I pressed the reset button?"

Scary, isn't it?

function expand_url() { curl -sI $1 | grep Location: | cut -d " " -f 2 | tr -d "\n" | pbcopy }
2011-08-21 05:30:09
User: gt
Functions: cut grep tr
0

Expand a URL, aka do a head request, and get the URL. Copy this value to clipboard.

fileName() { echo "$1" | grep -o "[^/]*$"; }
2011-08-19 18:16:22
User: totti
Functions: echo grep
Tags: grep basename
-1
fileName /path/to/file.ext

quivalent to

basename /path/to/file.ext
manswitch () { man $1 | less -p "^ +$2"; }
2011-08-19 16:44:48
User: dbh
Functions: less man
Tags: grep switch
14

e.g.

manswitch grep -o

This will take you to the relevant part of the man page, so you can see the description of the switch underneath.

allVideos() { find ./ -type f -print0 | xargs -0 file -iNf - | grep ": video/" | cut -d: -f1; }
2011-08-19 11:58:59
User: totti
Functions: cut file find grep xargs
0

Videos are found using their MIME type. Thus no need to for an extension for the video file.

This is a efficent version of "jnash" cmd (4086). Thanks for jnash. This cmd will only show video files while his cmd show files having "video" anywhere in path.

manswitch() { man $1 | grep -A5 "^ *\-$2"; }
2011-08-19 08:36:54
User: totti
Functions: grep man
Tags: man grep switch
1

Find the usage of a switch with out searching through the entire man page.

Usage: manswitch [cmd] [switch]

Eg:

manswitch grep silent

____________________________

In simple words

man <cmd> | grep "\-<switch>"

Eg:

man grep | grep "\-o"

This is not a standard method but works.

system_profiler SPApplicationsDataType | grep -A3 -B4 "Kind: PowerPC"
2011-08-17 14:53:08
User: TheQL
Functions: grep
Tags: Os X grep
0

No final count, but clean and simple output.