Hot answers tagged

61

+1 for @jamzed terse answer, however the OP might need some explanation: ps | grep "[d]jango" Using that regex you are launching a process which its ps string will not match itself, since the regexp matches "django" and not "[d]jango". That way you'll exclude the process that has the string "[d]jango" which in this case is grep; The same can be applied to ...


58

You can use grep anyway to search through the file - it does not really care if the input file is really text or not. From 'man grep': -a, --text Process a binary file as if it were text; this is equivalent to the --binary-files=text option. --binary-files=TYPE If the first few bytes of a file indicate that the file contains ...


49

The command you are looking for is strings Its name is quite self-explanatory, it retrieves any printable string from a given file. man strings gives: STRINGS(1) NAME strings - find the printable strings in a object, or other binary, file SYNOPSIS strings [ - ] [ -a ] [ -o ] [ -t format ] [ -number ] [ -n number ] [--] [file ...]


46

You can do pretty much anything with apache log files with awk alone. Apache log files are basically whitespace separated, and you can pretend the quotes don't exist, and access whatever information you are interested in by column number. The only time this breaks down is if you have the combined log format and are interested in user agents, at which point ...


46

I don't know about using awk instead of grep, but this works for me: tail -f file.log | grep -Ev '(ELB|Pingdom|Health)' EDIT: As dmourati and Caleb pointed out, you could also use egrep instead of grep -E for convenience. On some systems this this will be an link to the same binary, in others a copy of it supplied by the grep package. Either way it lives ...


38

My version of GNU Grep has a switch for this: grep -R foo --include '*.txt' * Described as follows: --include=GLOB Search only files whose base name matches GLOB (using wildcard matching as described under --exclude).


34

You can use nmap to run a ping scan. nmap -sP 192.168.254.* Starting Nmap 5.00 ( http://nmap.org ) at 2011-03-09 11:32 GMT Host xyzzy.lan (192.168.254.189) is up (0.00022s latency). MAC Address: 00:0C:29:5B:A5:E0 (VMware) Host plugh.lan (192.168.254.196) is up (0.00014s latency). MAC Address: 00:0C:29:2E:78:F1 (VMware) Host foo.lan (192.168.254.200) is up. ...


33

$ nc localhost 9571 | awk -F: '/rating/ { print $2 }'


32

Pipe it through strings, which will strip out all of the binary code leaving just the text.


32

There's no reason that's preferable syntax on its own. It's sometimes used as a hack when "grepping" for a process (e.g., ps aux | grep [a]pache). Using that syntax prevents grep from matching its own command line in the process list. See How to prevent "ps" reporting its own process?


30

You could try grep pattern file | tail -1 or tac file | grep pattern | head -1


29

ps | grep [d]jango ps | grep d[j]ango ... ps | grep djang[o]


29

grep -F '-ref.alleles' is equivalent to: grep -F -ref.alleles (none of the characters between the apostrophes are shell metacharacters, so quoting them has no effect.) This is in turn equivalent to: grep -F -r -e f.alleles by normal parsing of - prefixed options. The -e option takes an argument, but -F and -r don't. Since you didn't specify any ...


25

You might have a look at zgrep. >$ zgrep -h grep through gzip files usage: zgrep [grep_options] pattern [files]


23

One thing I've never seen anyone else do, for reasons that I can't imagine, is to change the Apache log file format to a more easily parseable version with the information that actually matters to you. For example, we never use HTTP basic auth, so we don't need to log those fields. I am interested in how long each request takes to serve, so we'll add that ...


22

Use find ... -print0 | xargs -0 ... e.g. find /path/to -name "*.html" -print0 | xargs -0 grep -l "rumpus" from the find man page -print0 True; print the full file name on the standard output, followed by a null character (instead of the newline character that ‘-print’ uses). This allows file names that ...


21

Another way is { grep ...; bzgrep ...;} >file && has the difficulty that the bzgrep wouldn't be run if the grep failed. Note the mandatory space after the opening curly brace and semicolon after the last command. Alternatively, you can use the subshell syntax (parentheses instead of curly braces), which isn't as picky: (grep ...; bzgrep ...) ...


21

The zgrep program is available for Linux (and perhaps some Unix too). This will decompress the files and then grep through them.


19

How about this: $ tail -f logfile? And if you need to grep: $ tail -f logfile | grep foobar.


19

As far as GNU grep go, this will show you the number of lines before the match: # grep -B number Equivilent for after: # grep -A number You can download GNU Grep here: http://www.gnu.org/s/grep/


18

This is actually dependent on your shell. Quotes (either kind) are primarily meant to deal with whitespace. For instance, the following: grep hello world file1 will look for the word "hello" in files called "world" and "file1", while grep "hello world" file1 will look for "hello world" in file1. The choice between single or double quotes is only ...


18

Try piping it to egrep with a pipe separated lists of words you want to filter out: tail -f log_file | egrep -v 'ELB|Pingdom|Health' Note that using parenthesis around the list of matches is optional. Since the | is treated as a logical OR operator by grep whether it occurs as part of a sub-group or not. '(ELB|Pingdom|Health)' would function exactly the ...


18

Don't use cat for that. Instead use grep DATABASE * or grep -n DATABASE * (if you want to know the line numbers as well as the filenames) directly. See useless use of cat. To clarify a bit more: cat * actually concatenates all files as it feeds them to grep through the pipe, so grep has no way of knowing which content belongs to which file, and indeed ...


16

The -l argument should do what you want. -l, --files-with-matches Suppress normal output; instead print the name of each input file from which output would normally have been printed. The scanning will stop on the first match. (-l is specified by POSIX.)


16

This would be my approach: find dev -type f -print0 | \ # find all files xargs -0 grep 'extends MY_Output' | \ # search for your string cut -d/ -f2 | \ # extract web folder name sort | uniq # eliminate duplicates Note use of the print0 parameter to find and the -0 (zero) flag to xargs, which ...


16

Use pgrep instead: pgrep -lf django


15

DEFAULT_ROUTE=$(ip route show default | awk '/default/ {print $3}') ping -c 1 $DEFAULT_ROUTE This should solve your problem.


15

locate filename Much faster than find, if you're running the locate service, and it only finds files that existed at the time updatedb last ran (usualy the night befor under the control of a cron job). You can run updatedb by hand, but that is even slower than the find cletus suggests, and requires root. I sometimes update the database by hand after ...


15

If you have a large number of files it would be useful to incorporate xargs into the command to avoid an 'Argument list too long' error. find . -name '*.txt' -print | xargs grep <pattern>


15

Use nmap. Important to run it as root so you get the MAC addresses. Example: sudo nmap -sP 192.168.1.0/24 Will scan 192.168.1.1 - 192.168.1.255. Look up CIDR notation on wiki if you're not familiar with this subnet notation. You should be able to get nmap from the repos of any recentish Linux distro, e.g. sudo apt-get install nmap or sudo yum ...



Only top voted, non community-wiki answers of a minimum length are eligible