Commands using grep (1,935)


  • 18
    mount -t ntfs-3g -o ro,loop,uid=user,gid=group,umask=0007,fmask=0117,offset=0x$(hd -n 1000000 image.vdi | grep "eb 52 90 4e 54 46 53" | cut -c 1-8) image.vdi /mnt/vdi-ntfs
    Cowboy · 2009-08-23 17:25:07 12
  • This function takes a word or a phrase as arguments and then fetches definitions using Google's "define" syntax. The "nl" and perl portion isn't strictly necessary. It just makes the output a bit more readable, but this also works: define(){ local y="$@";curl -sA"Opera" "http://www.google.com/search?q=define:${y// /+}"|grep -Po '(?<=<li>)[^<]+';} If your version of grep doesn't have perl compatible regex support, then you can use this version: define(){ local y="$@";curl -sA"Opera" "http://www.google.com/search?q=define:${y// /+}"|grep -Eo '<li>[^<]+'|sed 's/<li>//g'|nl|perl -MHTML::Entities -pe 'decode_entities($_)' 2>/dev/null;} Show Sample Output


    18
    define(){ local y="$@";curl -sA"Opera" "http://www.google.com/search?q=define:${y// /+}"|grep -Po '(?<=<li>)[^<]+'|nl|perl -MHTML::Entities -pe 'decode_entities($_)' 2>/dev/null;}
    eightmillion · 2010-01-29 05:01:11 18
  • Particularly useful on OS X where netstat doesn't have -p option. Show Sample Output


    18
    lsof -i -P | grep -i "listen"
    patko · 2010-10-14 09:37:51 10
  • 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!! Show Sample Output


    18
    grep . *
    theist · 2011-09-01 09:16:04 10

  • 17
    curl -s http://checkip.dyndns.org/ | grep -o "[[:digit:].]\+"
    lv4tech · 2009-05-14 09:43:31 13
  • Use multiple patterns with grep -v. So you can print all lines in a file except those containing the multiple patterns you specify.


    16
    grep 'test' somefile | grep -vE '(error|critical|warning)'
    zlemini · 2009-10-23 23:21:36 12
  • find all computer connected to my host through TCP connection. Show Sample Output


    16
    netstat -lantp | grep ESTABLISHED |awk '{print $5}' | awk -F: '{print $1}' | sort -u
    bitbasher · 2011-07-21 21:23:10 16
  • This will give you the Dell Service tag number associated with your machine. Incredibly useful when you need that number for tech support or downloads. Show Sample Output


    15
    sudo dmidecode | grep Serial\ Number | head -n1
    nlinux · 2009-02-18 14:54:28 3182
  • This command takes the output of the 'last' command, removes empty lines, gets just the first field ($USERNAME), sort the $USERNAMES in reverse order and then gives a summary count of unique matches. Show Sample Output


    15
    last | grep -v "^$" | awk '{ print $1 }' | sort -nr | uniq -c
    hkyeakley · 2009-02-18 16:38:59 13
  • Several times, I find myself hitting my up arrow, and changing the search term. Unfortunately, I find myself wasting too much time typing: grep kernel /var/log/messages Redirecting STDIN allows me to put the search term at the end so I less cursor movement to change what I'm searching for: < /var/log/messages grep kernel If you're using the emacs keyboard binding, then after you press your up arrow, press CTRL+w to erase the word. If this has already been submitted, I couldn't find it with the search utility.


    15
    < /path/to/file.txt grep foo
    atoponce · 2009-03-29 02:43:40 16
  • Just a simple way without the need of additional tools. Of course, replace eth0 with your IF. Show Sample Output


    15
    while [ /bin/true ]; do OLD=$NEW; NEW=`cat /proc/net/dev | grep eth0 | tr -s ' ' | cut -d' ' -f "3 11"`; echo $NEW $OLD | awk '{printf("\rin: % 9.2g\t\tout: % 9.2g", ($1-$3)/1024, ($2-$4)/1024)}'; sleep 1; done
    hons · 2011-03-22 10:02:23 8
  • Usefull for when you don't have nmap and need to find a missing host. Pings all addresses from 10.1.1.1 to 10.1.1.254, modify for your subnet. Timeout set to 1 sec for speed, if running over a slow connection you should raise that to avoid missing replies. This will clean up the junk, leaving just the IP address: for i in {1..254}; do ping -c 1 -W 1 10.1.1.$i | grep 'from' | cut -d' ' -f 4 | tr -d ':'; done Show Sample Output


    14
    for i in {1..254}; do ping -c 1 -W 1 10.1.1.$i | grep 'from'; done
    SuperJediWombat · 2010-04-07 16:57:53 7
  • For automated unit tests I wanted my program to run normally, but if it crashed, to add a stack trace to the output log. I came up with this command so I wouldn't have to mess around with core files. The one downside is that it does smoosh your program's stderr and stdout together. Show Sample Output


    14
    gdb -batch -ex "run" -ex "bt" ${my_program} 2>&1 | grep -v ^"No stack."$
    kurt · 2010-12-29 17:46:31 28
  • Grep will read the contents of each file in PWD and will use the REs $1 $2 ... $n to match the contents. In case of match, grep will print the appropriate file, line number and the matching line. It's just easier to write ff word1 word2 word3 Instead of grep -rinE 'word1|word2|word3' . Show Sample Output


    14
    ff() { local IFS='|'; grep -rinE "$*" . ; }
    RanyAlbeg · 2011-06-10 10:25:10 137

  • 14
    curl -s https://api.github.com/users/<username>/repos?per_page=1000 |grep git_url |awk '{print $2}'| sed 's/"\(.*\)",/\1/'
    wuseman1 · 2019-11-19 20:31:19 261
  • Purge all configuration files of removed packages Show Sample Output


    13
    sudo aptitude purge `dpkg --get-selections | grep deinstall | awk '{print $1}'`
    kelevra · 2009-04-28 11:44:04 15
  • Finds all corrupted jpeg files in current directory and its subdirectories. Displays the error or warning found. The jpeginfo is part of the jpeginfo package in debian. Should you wish to only get corrupted filenames, use cut to extract them : find ./ -name *jpg -exec jpeginfo -c {} \; | grep -E "WARNING|ERROR" | cut -d " " -f 1 Show Sample Output


    13
    find . -name "*jpg" -exec jpeginfo -c {} \; | grep -E "WARNING|ERROR"
    vincentp · 2009-06-03 22:08:48 11
  • This one uses dictionary.com


    13
    pronounce(){ wget -qO- $(wget -qO- "http://dictionary.reference.com/browse/$@" | grep 'soundUrl' | head -n 1 | sed 's|.*soundUrl=\([^&]*\)&.*|\1|' | sed 's/%3A/:/g;s/%2F/\//g') | mpg123 -; }
    matthewbauer · 2010-03-13 04:23:56 12
  • Though without infinite time and knowledge of how the site will be designed in the future this may stop working, it still will serve as a simple straight forward starting point. This uses the observation that the only item marked as strong on the page is the single logical line that includes the italicized fact. If future revisions of the page show failure, or intermittent failure, one may simply alter the above to read. wget randomfunfacts.com -O - 2>/dev/null | tee lastfact | grep \<strong\> | sed "s;^.*<i>\(.*\)</i>.*$;\1;" The file lastfact, can then be examined whenever the command fails.


    13
    wget randomfunfacts.com -O - 2>/dev/null | grep \<strong\> | sed "s;^.*<i>\(.*\)</i>.*$;\1;"
    tali713 · 2010-03-30 23:49:30 81
  • Trick to avoid the form: grep process | grep - v grep Show Sample Output


    13
    ps axu | grep [a]pache2
    EBAH · 2012-12-15 19:37:19 33
  • Put it in your ~/.bashrc usage: google word1 word2 word3... google '"this search gets quoted"' Show Sample Output


    13
    function google { Q="$@"; GOOG_URL='https://www.google.de/search?tbs=li:1&q='; AGENT="Mozilla/4.0"; stream=$(curl -A "$AGENT" -skLm 10 "${GOOG_URL}${Q//\ /+}" | grep -oP '\/url\?q=.+?&amp' | sed 's|/url?q=||; s|&amp||'); echo -e "${stream//\%/\x}"; }
    michelsberg · 2013-04-05 08:04:15 9
  • The trick here is to use the brackets [ ] around any one of the characters of the grep string. This uses the fact that [?] is a character class of one letter and will be removed when parsed by the shell. This is useful when you want to parse the output of grep or use the return value in an if-statement without having its own process causing it to erroneously return TRUE. Show Sample Output


    12
    ps aux | grep "[s]ome_text"
    SiegeX · 2009-02-17 02:10:50 12
  • greps for search word in directory and below (defaults to cd). -i case insensitive -n shows line number -H shows file name


    12
    grep --color=auto -iRnH "$search_word" $directory
    tobiasboon · 2009-02-21 19:16:33 18
  • Highlights the search pattern in red.


    12
    grep -i --color=auto
    P17 · 2009-04-27 15:03:28 8
  • This helped me find a botnet that had made into my system. Of course, this is not a foolproof or guarantied way to find all of them or even most of them. But it helped me find it.


    12
    cat /var/lib/dpkg/info/*.list > /tmp/listin ; ls /proc/*/exe |xargs -l readlink | grep -xvFf /tmp/listin; rm /tmp/listin
    kamathln · 2009-09-09 18:09:14 14
  •  < 1 2 3 4 >  Last ›

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. 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.

Share Your Commands


Check These Out

'hpc' in the box - starts a maximum of n compute commands modulo n controlled in parallel
the block of the loop is useful whenever you have huge junks of similar jobs, e.g., convert high res images to thumbnails, and make usage out of all the SMP power on your compute box without flooding the system. note: c is used as counter and the random sleep $ r=`echo $RANDOM%5 |bc`; echo "sleep $r"; sleep $r is just used as a dummy command.

Get MX records for a domain
command is shorter, output unnecessary longer

Save a file you edited in vim without the needed permissions (no echo)
Write a file you edited in Vim but that you do not have the permissions to write to (unless you use sudo.) Same as #1204 but without the echo to stdout that I find annoying.

Extract audio from Mythtv recording to Rockbox iPod using ffmpeg
There are some pretty good live performances on late night TV. With Mythtv I record David Letterman, Jimmy Fallon, and Conan nightly all in HD from over the air broadcasts. If I find a live performance I like I copy it to my Rockboxed iPod using this command. The Rockbox firmware knows how to downmix 5.1 audio. The command above extracts the audio from the video starting at 58 minutes and 15 seconds. It ends at the end of the file since this was the last performance of the recording. The command creates an ac3 file. I copy the ac3 file to my Rockbox iPod and rock on.

Job Control
background and disown, but with a proper one-line syntax

find and reduce 8x parallel the size of JPG images without loosing quality via jpegoptim

list files recursively by size

Extract audio from a video
With the -vn switch we make our intentions clear and ask FFmpeg not to bother itself with the video. Next we specify the audio codec copy, which tells FFmpeg to use the same codec to encode the audio, which it uses to decode it. To keep things simple, we'll just keep the sampling and bitrate values the same.

Convert (almost) any video file into webm format for online html5 streaming
If you're using the experimental vorbis encoder (homebrew version of libffmpeg)

list files recursively by size


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: