//Tips tagged grep
Latest tips by RSS
Click here to subscribe
Follow Shell-Fu on Twitter
Click here to follow
Follow Shell-Fu on identi.ca
Click here to follow
Place a filename at the beginning of the line to make it easier to edit the search at the end of the command.

$ </var/log/messages grep foo

$ </var/log/messages grep bar
$ </var/log/messages grep user1

View Comments »

Print a random shell-fu tip:
links -dump "http://www.shell-fu.org/lister.php?random" | grep -A 100 -- ----


View Comments »

Want to check the amount of used, free and total memory and swap from the command line? This script displays memory and swap information. Fully posix compliant and should work with all 2.[2-6].* kernels .

#fetch and process memory information
[ -f /proc/meminfo ] && {
Buffers=`grep -we 'Buffers' /proc/meminfo | cut -d' ' -f2- | tr -d "[A-Z][a-z] "`
Cached=`grep -we 'Cached' /proc/meminfo | cut -d' ' -f2- | tr -d "[A-Z][a-z] "`
MemFree=`grep -ie 'MemFree' /proc/meminfo | cut -d' ' -f2- | tr -d "[A-Z][a-z] "`
MemTotal=`grep -ie 'MemTotal' /proc/meminfo | cut -d' ' -f2- | tr -d "[A-Z][a-z] "`
SwapCached=`grep -ie 'SwapCached' /proc/meminfo | cut -d' ' -f2- | tr -d "[A-Z][a-z] "`
SwapFree=`grep -ie 'SwapFree' /proc/meminfo | cut -d' ' -f2- | tr -d "[A-Z][a-z] "`
SwapTotal=`grep -ie 'SwapTotal' /proc/meminfo | cut -d' ' -f2- | tr -d "[A-Z][a-z] "`
MEMUSED="$(( ( ( ( $MemTotal - $MemFree ) - $Cached ) - $Buffers ) / 1024 ))"
MEMTOTAL="$(( $MemTotal / 1024))"
MEMPER="$(( ( $MEMUSED * 100 ) / $MEMTOTAL ))"
[ "$SwapTotal" -gt "1" ] && {
  SWAPUSED="$(( ( ( $SwapTotal - $SwapFree ) - $SwapCached ) / 1024 ))"
  SWAPTOTAL="$(( $SwapTotal / 1024))"
  SWAPPER="$(( ( $SWAPUSED * 100 ) / $SWAPTOTAL ))" 
} || {

# display the information
/bin/echo "Memory"
/bin/echo "Used: $MEMUSED"
/bin/echo "Free: $MEMFREE"
/bin/echo "Total: $MEMTOTAL"
/bin/echo "Swap"
/bin/echo "Used: $SWAPUSED"
/bin/echo "Free: $SWAPFREE"
/bin/echo "Total: $SWAPTOTAL"

View Comments »

Generate a random 8 character password containing a-z, A-Z and 0-9:

egrep -ioam1 '[a-z0-9]{8}' /dev/urandom

View Comments »

Grepping for a process will return the grep command, this can be avoided by adding '| grep -v grep' to a command or easier in some cases altering the regular expression by adding brackets around a character.

ps | grep 'ss[h]'

The regular expression 'ss[h]' matches the literal string 'ssh' when it appears in the process list, but does not accidentally match the string 'ss[h]' when it appears in the process list as 'grep ss[h]'.

View Comments »

Find all files with given name (you can use Bash expansion if you'd like), and Grep for a phrase:
find . -name  -exec grep "phrase" {} \;

To display the filename that contained a match, use -print:
find . -name  -exec grep "phrase" {} \; -print

Or, use Grep options to print the filename and line number for each match:
find . -name  -exec grep -Hn "phrase" {} \;

The string `{}` is replaced by the current filename being processed everywhere it occurs in the arguments to the command. See the `find` man page for more information.

View Comments »

It's easy to remove (or copy, move etc.) all files that match a given criteria, but harder to move all but ones excluded by a criteria.

To do this we can combine grep's -v option with Unix command substitution:

$ ls
1.txt   2.txt   3.txt   4.txt
$ rm `ls | grep -v 4\.txt`
$ ls

View Comments »

Subtract a small file from a bigger file.

grep -vf filesmall filebig

View Comments »

ps | grep processName | grep -v grep | awk '{print "kill -9 " $2}' | sh

Take the '| sh' off the end before running to check the commands that will be run.

View Comments »

lynx -dump http://www.spantz.org | grep -A999 "^References$" | tail -n +3 | awk '{print $2 }'

View Comments »

A simple shell script to get the latest stable version of the linux kernel:


kernelV=`finger finger@kernel.org | grep 'stable version' | awk '{print $NF}'`
wget -c http://www.kernel.org/pub/linux/kernel/v2.6/linux-$kernelV.tar.bz2

View Comments »

I've often seen people using the find command on a directory to get all the files, then using these with grep to find files containing a particular word. This can however, be done better just using grep:

grep --color=auto -inHR "search_for" /in/dir

greps for occurances of 'search_for' in the specified directory and below, or the current directory if none specified.


-i case insensitive
-n shows line number
-H shows file name
-R recursive

View Comments »

Displays a random xkcd comic. Requires ImageMagick.

wget http://dynamic.xkcd.com/comic/random/ -O -| grep <img src="http://imgs.xkcd.com/comics | 
 sed s/<img src="// | sed s/"[a-z]*.*// | wget -i - -O -| display

View Comments »

use `grep -o` to select only the pattern matched rather than the whole line:

grep -o 'GET [^ ]\+ ' /var/log/httpd_access.log | sort | uniq -c | sort -n | head -n 100 > top100

View Comments »

If your version of grep supports coloring matches, you can use the following to give a calendar with the current date colored:

cal | grep --color=auto -E  \(\ \|^\)$(date +%e)\(\ \|\$\)\|\$

View Comments »

Grep file(s) and highlight the grep matches using less...

grep query *.php | less +/query

View Comments »

# by dj.r4iden
echo "Your ip Address is" `lynx --source http://www.formyip.com/ |grep The | awk {'print $5'}`

View Comments »

Go to [http://www.bbc.co.uk/cgi-perl/weather/search/new_search.pl] and search for your location. Copy the link to the 3 day forecast feed, for example [http://feeds.bbc.co.uk/weather/feeds/rss/5day/world/0105.xml]. Then just put that link into the following command to get a quick command line weather forecast.

wget -q -O - http://feeds.bbc.co.uk/weather/.../XXXXX.xml | grep title | sed -e "s/<[^>]*>//g" -e "s/°//g" | egrep "^[A-Z]"

$ wget -q -O - http://feeds.bbc.co.uk/weather/feeds/rss/5day/world/0105.xml | grep title | sed -e "s/<[^>]*>//g" -e "s/°//g" | egrep "^[A-Z]"
BBC - Weather Centre - Forecast for San Francisco, United States of America Tuesday: sunny, Max Temp: 22C (72F), Min Temp: 13C (55F) Wednesday: sunny, Max Temp: 22C (72F), Min Temp: 15C (59F) Thursday: sunny, Max Temp: 25C (77F), Min Temp: 14C (57F)

View Comments »

example, look for all the TODO and HACK strings I left in large java project and show a bit of context before and after using the -C switch of grep.

find path_to_start | grep \\.java | xargs egrep -C3 "TODO|HACK" | less

View Comments »

I use the following to list non-system users. It should be portable though won't work on systems without the getent command.

alias lsusers='getent passwd | tr ":" " " | awk "\$3 >= $(grep UID_MIN /etc/login.defs | cut -d " " -f 2) { print \$1 }" | sort'

View Comments »

A slight improvement to #64 -- drops one grep and adds line numbers

find _path_to_start_ -name '*.php' | xargs egrep -nC3 'TODO|HACK' | less

View Comments »

My little "iso2cd" alias. Not clean, but handy. The Burning device will be auto detected.

example call:
iso2cd debian_lenny_final.iso

alias iso2cd="cdrecord -s dev=`cdrecord --devices 2>&1 | grep "\(rw\|dev=\)" | awk {'print $2'} | cut -f'2' -d'=' | head -n1` gracetime=1 driveropts=burnfree -dao -overburn -v"

View Comments »

The following isn't particularly pretty and should be considered a work in progress, but it's quite fun.

Get examples of ways a command can be used direct from shell-fu by adding the following alias:
function examples { lynx -width=$COLUMNS -nonumbers -dump "http://www.shell-fu.org/lister.php?tag=$1" | \
sed -n '/^[a-zA-Z]/,$p' | egrep -v '^http|^javas|View Comm|HIDE|] \+|to Share|^ +\*|^ +[HV][a-z]* l|^ .*efu.*ep.*!$' | \
sed -e '/^  *__*/N;s/\n$//g' | less -r; }

This pulls out the tips tagged by the given command. (Make sure you tag any tips you submit!)

View Comments »

Home Latest Browse Top 25 Random Hall Of Fame Contact Submit