Commands tagged grep (401)

  • 92
    lsof -P -i -n
    OJM · 2009-09-19 18:28:48 1
  • for one line per process: ss -p | cat for established sockets only: ss -p | grep STA for just process names: ss -p | cut -f2 -sd\" or ss -p | grep STA | cut -f2 -d\"

    ss -p
    Escher · 2009-09-19 21:55:01 0
  • This is how I typically grep. -R recurse into subdirectories, -n show line numbers of matches, -i ignore case, -s suppress "doesn't exist" and "can't read" messages, -I ignore binary files (technically, process them as having no matches, important for showing inverted results with -v) I have grep aliased to "grep --color=auto" as well, but that's a matter of formatting not function.

    grep -RnisI <pattern> *
    birnam · 2009-09-22 15:09:43 3
  • show only the name of the apps that are using internet Show Sample Output

    lsof -P -i -n | cut -f 1 -d " "| uniq | tail -n +2
    edo · 2009-09-19 21:23:54 2
  • Ever ask yourself "How much data would be lost if I pressed the reset button?" Scary, isn't it? Show Sample Output

    grep ^Dirty /proc/meminfo
    h3xx · 2011-08-24 08:48:49 8
  • I have a bash alias for this command line and find it useful for searching C code for error messages. The -H tells grep to print the filename. you can omit the -i to match the case exactly or keep the -i for case-insensitive matching. This find command find all .c and .h files Show Sample Output

    find . -name "*.[ch]" -exec grep -i -H "search pharse" {} \;
    bunedoggle · 2009-05-06 15:22:49 9
  • Get your colorized grep output in less(1). This involves two things: forcing grep to output colors even though it's not going to a terminal and telling less to handle those properly.

    grep --color=always | less -R
    dinomite · 2009-05-20 20:30:19 1
  • sorts the files by integer megabytes, which should be enough to (interactively) find the space wasters. Now you can dush for the above output, dush -n 3 for only the 3 biggest files and so on. It's always a good idea to have this line in your .profile or .bashrc Show Sample Output

    alias dush="du -sm *|sort -n|tail"
    funky · 2010-03-26 10:18:57 1
  • PDF files are simultaneously wonderful and heinous. They are wonderful in being ubiquitous and mostly being cross platform. They are heinous in being very difficult to work with from the command line, search, grep, use only the text inside the PDF, or use outside of proprietary products. xpdf is a wonderful set of PDF tools. It is on many linux distros and can be installed on OS X. While primarily an open PDF viewer for X, xpdf has the tool "pdftotext" that can extract formated or unformatted text from inside a PDF that has text. This text stream can then be further processed by grep or other tool. The '-' after the file name directs output to stdout rather than to a text file the same name as the PDF. Make sure you use version 3.02 of pdftotext or later; earlier versions clipped lines. The lines extracted from a PDF without the "-layout" option are very long. More paragraphs. Use just to test that a pattern exists in the file. With "-layout" the output resembles the lines, but it is not perfect. xpdf is available open source at

    pdftotext [file] - | grep 'YourPattern'
    drewk · 2010-02-14 21:42:35 0
  • grep searches through a file and prints out all the lines that match some pattern. Here, the pattern is some string that is known to be in the deleted file. The more specific this string can be, the better. The file being searched by grep (/dev/sda1) is the partition of the hard drive the deleted file used to reside in. The ?-a? flag tells grep to treat the hard drive partition, which is actually a binary file, as text. Since recovering the entire file would be nice instead of just the lines that are already known, context control is used. The flags ?-B 25 -A 100? tell grep to print out 25 lines before a match and 100 lines after a match. Be conservative with estimates on these numbers to ensure the entire file is included (when in doubt, guess bigger numbers). Excess data is easy to trim out of results, but if you find yourself with a truncated or incomplete file, you need to do this all over again. Finally, the ?> results.txt? instructs the computer to store the output of grep in a file called results.txt. Source:

    grep -a -B 25 -A 100 'some string in the file' /dev/sda1 > results.txt
    olalonde · 2010-08-19 20:07:42 4
  • 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

    grep . *
    theist · 2011-09-01 09:16:04 4
  • Print out list of all branches with last commit date to the branch, including relative time since commit and color coding. Show Sample Output

    for k in `git branch|perl -pe s/^..//`;do echo -e `git show --pretty=format:"%Cgreen%ci %Cblue%cr%Creset" $k|head -n 1`\\t$k;done|sort -r
    brunost · 2009-06-03 08:25:00 3
  • 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.

    manswitch () { man $1 | less -p "^ +$2"; }
    dbh · 2011-08-19 16:44:48 2
  • ps and grep is a dangerous combination -- grep tries to match everything on each line (thus the all too common: grep -v grep hack). ps -C doesn't use grep, it uses the process table for an exact match. Thus, you'll get an accurate list with: ps -fC sh rather finding every process with sh somewhere on the line. Show Sample Output

    pooderbill · 2015-04-20 13:09:44 2
  • This is a command that I find myself using all the time. It works like regular grep, but returns the paragraph containing the search pattern instead of just the line. It operates on files or standard input. grepp <PATTERN> <FILE> or <SOMECOMMAND> | grepp <PATTERN> Show Sample Output

    grepp() { [ $# -eq 1 ] && perl -00ne "print if /$1/i" || perl -00ne "print if /$1/i" < "$2";}
    eightmillion · 2010-01-12 04:30:15 5
  • 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 -O - 2>/dev/null | tee lastfact | grep \<strong\> | sed "s;^.*<i>\(.*\)</i>.*$;\1;" The file lastfact, can then be examined whenever the command fails.

    wget -O - 2>/dev/null | grep \<strong\> | sed "s;^.*<i>\(.*\)</i>.*$;\1;"
    tali713 · 2010-03-30 23:49:30 1
  • [Update! Thanks to a tip from ioggstream, I've fixed both of the bugs mentioned below.] You, yes, 𝙔𝙊𝙐, can be the terror of the Internet! Why use normal, boring bullet points in your text, when you could use a ROTATED HEAVY BLACK HEART BULLET (❥)!? (Which would also be an awesome band name, by the way).  This script makes it easy to find unusual characters from the command line. You can then cut and paste them or, if you're using a GTK application, type Control+Shift+U followed by the code point number (e.g., 2765) and then a SPACE.  USAGE: Put this script in a file (I called mine "ugrep") and make it executable. Run it from the command line like so,  ugrep heart  The output will look like this,  ☙ U+2619 REVERSED ROTATED FLORAL HEART BULLET ♡ U+2661 WHITE HEART SUIT ♥ U+2665 BLACK HEART SUIT ❣ U+2763 HEAVY HEART EXCLAMATION MARK ORNAMENT ❤ U+2764 HEAVY BLACK HEART ❥ U+2765 ROTATED HEAVY BLACK HEART BULLET ❦ U+2766 FLORAL HEART ❧ U+2767 ROTATED FLORAL HEART BULLET ⺖ U+2E96 CJK RADICAL HEART ONE ⺗ U+2E97 CJK RADICAL HEART TWO ⼼ U+2F3C KANGXI RADICAL HEART  You can, of course, use regular expressions. For example, if you are looking for the "pi" symbol, you could do this:  ugrep '\bpi\b'  REQUIREMENTS: Although this is written in Bash, it assumes you have Perl installed because it greps through the Perl Unicode character name module (/usr/lib/perl5/Unicode/ Note that it would not have made more sense to write this in Perl, since the module doesn't actually include a subroutine for looking up a character based on the description. (Weird.)  BUGS: In order to fit this script in the commandlinefu limits, a couple bugs were added. ① Astral characters beyond the BMP (basic multilingual plane) are not displayed correctly, but see below. ② Perl code from the perl module being grepped is sometimes extraneously matched.  MISFEATURES: Bash's printf cannot, given a Unicode codepoint, print the resulting character to the terminal. GNU's coreutils printf (usually "/usr/bin/printf") can do so, but it is brokenly pedantic about how many hexadecimal digits follow the escape sequence and will actually die with an error if you give the wrong number. This is especially annoying since Unicode code points are usually variable length with implied leading zeros. The file represents BMP characters as 4 hexits, but astral characters as 5. In the actual version of this script that I use, I've kludged around this misfeature by zero-padding to 8 hexits like so,  /usr/bin/printf "\U$(printf "%08x" 0x$hex)"  TIP 1: The author recommends "xsel" for command line cut-and-paste. For example,  ugrep biohazard | xsel  TIP 2: In Emacs, instead of running this command in a subshell, you can type Unicode code points directly by pressing Control-Q first, but you'll likely want to change the default input from octal to hexadecimal. (setq read-quoted-char-radix 16).  TIP 3: Of course, if you're using X, and you want to type one of the more common unusual characters, it's easiest of all to do it with your Compose (aka Multi) key. For example, hitting [Compose] <3 types ♥. Show Sample Output

    egrep -i "^[0-9a-f]{4,} .*$*" $(locate | while read h d; do /usr/bin/printf "\U$(printf "%08x" 0x$h)\tU+%s\t%s\n" $h "$d"; done
    hackerb9 · 2010-12-31 16:47:59 5

  • 12
    apt-get install `ssh [email protected]_you_want_to_clone "dpkg -l | grep ii" | awk '{print $2}'`
    TuxOtaku · 2011-05-10 13:33:51 3

  • 12
    sudo dmidecode | grep Product
    bbbco · 2012-02-07 16:26:23 2
  • ... plus do a sort according frequency Show Sample Output

    find . -type f | awk -F'.' '{print $NF}' | sort| uniq -c | sort -g
    cp · 2011-02-14 09:15:29 0
  • How to force a userid to log out of a Linux host, by killing all processes owned by the user, including login shells:

    pkill -KILL -u username
    SuperFly · 2011-06-16 11:40:57 1
  • doesn't do case-insensitive filenames like iname but otherwise likely to be faster

    grep -r --include="*.[ch]" pattern .
    sitaram · 2009-08-13 01:41:12 2
  • I needed a way to search all files in a web directory that contained a certain string, and replace that string with another string. In the example, I am searching for "askapache" and replacing that string with "htaccess". I wanted this to happen as a cron job, and it was important that this happened as fast as possible while at the same time not hogging the CPU since the machine is a server. So this script uses the nice command to run the sh shell with the command, which makes the whole thing run with priority 19, meaning it won't hog CPU processing. And the -P5 option to the xargs command means it will run 5 separate grep and sed processes simultaneously, so this is much much faster than running a single grep or sed. You may want to do -P0 which is unlimited if you aren't worried about too many processes or if you don't have to deal with process killers in the bg. Also, the -m1 command to grep means stop grepping this file for matches after the first match, which also saves time. Show Sample Output

    sh -c 'S=askapache R=htaccess; find . -mount -type f|xargs -P5 -iFF grep -l -m1 "$S" FF|xargs -P5 -iFF sed -i -e "s%${S}%${R}%g" FF'
    AskApache · 2009-10-02 05:03:10 0
  • grep -o puts each occurrence in a separate line

    $ grep -or string path/ | wc -l
    ioggstream · 2009-07-16 12:50:59 3
  • mixing tabs and spaces for indentation in python would confuse the python interpreter, to avoid that, check if the file has any tab based indentation. "^V" => denotes press control + v and press tab within quotes. cat class Tux(object): print "Hello world.." grep " " print "Hello world.."

    grep "^V<TAB>" your_file
    sathyz · 2010-03-01 08:46:02 0
  •  1 2 3 >  Last ›

What's this? 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

Creates a 'path' command that always prints the full path to any file
The command creates an alias called 'path', so it's useful to add it to your .profile or .bash_profile. The path command then prints the full path of any file, directory, or list of files given. Soft links will be resolved to their true location. This is especially useful if you use scp often to copy files across systems. Now rather then using pwd to get a directory, and then doing a separate cut and paste to get a file's name, you can just type 'path file' and get the full path in one operation.

Search google and show only urls
Get the first 10 google results form a querry, but showing only the urls from the results. Use + to search diferent terms, ex: commandlinefu+google .

Extract the MBR ID of a device
Useful when you want to know the mbrid of a device - for the purpose of making it bootable. Certain hybridiso distros, for eg the OpenSUSE live ISO uses the mbrid to find the live media. Use this command to find out the mbrid of your USB drive and then edit the /grub/mbrid file to match it.

Download and extract a *tar.gz file with curl.

List all the files that have been deleted while they were still open.
If your customer deletes a file that is still in use by a process, that space does not get freed up (will not show up in df) until that process either closes the file on its own, or is killed.

Outputs a sorted list of disk usage to a text file
Recursively searches current directory and outputs sorted list of each directory's disk usage to a text file.

Display error pages in report format
This command will return a full list of Error 404 pages in the given access log. The following variables have been given to awk Hostname ($2), ERROR Code ($9), Missing Item ($7), Referrer ($11) You can then send this into a file (>> /path/to/file), which you can open with OpenOffice as a CSV

View all images
So you are in directory with loads of pictures laying around and you need to quickly scan through them all

modify a file in place with perl
changes THIS to THAT in all files matching fileglob* without using secondary files

Get AWS temporary credentials ready to export based on a MFA virtual appliance
You might want to secure your AWS operations requiring to use a MFA token. But then to use API or tools, you need to pass credentials generated with a MFA token. This commands asks you for the MFA code and retrieves these credentials using AWS Cli. To print the exports, you can use: `awk '{ print "export AWS_ACCESS_KEY_ID=\"" $1 "\"\n" "export AWS_SECRET_ACCESS_KEY=\"" $2 "\"\n" "export AWS_SESSION_TOKEN=\"" $3 "\"" }'` You must adapt the command line to include: * $MFA_IDis ARN of the virtual MFA or serial number of the physical one * TTL for the credentials

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.


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: