Skip to the posts
Buffalo's neighborhood journals since 2003 🌱 Plant🌶️ Spice☀️ Light🌙 Dark

tinypliny’s journal

Greppetty Grep

·

Another set of commands I need to burn into my brain:

Find all files in the current directory that contain blah in their file names
  • ls |grep blah

Wildcards in grep: all files that start with a b and end in a g with ONLY one character in between
  • grep b.g file

The asterisk * in a grep command stands for repetition. .* means "repeat any character any number of times"
  • grep "b.*g" file

Escaping characters. Preceding backslashes either remove an implied special meaning from a character or add special meaning to a "non-special" character
  • grep 'hello\.gif' file

An expression consisting of a character followed by an escaped question mark matches one or zero instances of that character.
  • bugg\?y matches bugy , buggy but not bugggy

An expression surrounded by "escaped parentheses" is treated as a single character.
  • Fred\(eric\)\? Smith matches Fred Smith or Frederic Smith

Match a selection of characters, use []
  • [Hh]ello matches lines containing hello or Hello
  • [A-Ca-k] is the same as [ABCabcdefghijk]
  • [[:alpha:]] is the same as [a-zA-Z]
  • [[:upper:]] is the same as [A-Z]
  • [[:lower:]] is the same as [a-z]
  • [[:digit:]] is the same as [0-9]
  • [[:alnum:]] is the same as [0-9a-zA-Z]
  • [[:space:]] matches any white space including tabs

The [] may be used to search for non-matches. This is done by putting a carat ^ as the first character inside the square brackets.
  • grep "([^()]*)a"

matches (hello)a, (aksjdhaksj d ka)a But not
x=(y+2(x+1))a (I don't get this part, does it match (y+2(x+1))a?)

This matches phone numbers, possibly containing a dash or whitespace in the middle.
  • grep "[[:digit:]]\{3\}[ -]\?[[:digit:]]\{4\}" file

The $ character matches the end of the line. The ^ character matches the beginning of the line.
  • grep "^From.*mscharmi" /var/spool/mail/elflord
  • grep "^[[:space:]]*hello[[:space:]]*$" file

The expression consisting of two expressions seperated by the or operator \| matches lines containing either of those two expressions.
  • grep "I am a \(cat\|dog\)" matches lines containing the string "I am a cat" or the string "I am a dog"

The expression \n where n is a number, matches the contents of the n'th set of parentheses in the expression
"Mr \(dog\|cat\) came home to Mrs \1 and they went to visit Mr \(dog\|cat\) and Mrs \2 to discuss the meaning of life matches the respective dog/cat pairs

The following characters are considered special and need to be "escaped":
? \ . [ ] ^ $

A $ sign loses its meaning if characters follow it and the carat ^ loses its meaning if other characters precede it.

Square brackets behave a little differently. The rules for square brackets go as follows:
  1. A closing square bracket loses its special meaning if placed first in a list. for example []12] matches ] , 1, or 2.
  2. A dash - loses it's usual meaning inside lists if it is placed last.
  3. A carat ^ loses it's special meaning if it is not placed first
  4. Most special characters lose their meaning inside square brackets
  • grep "$HOME" file searches file for the name of your home directory, while
  • grep '$HOME' file searches for the string $HOME


Find with specific strings on filenames
  • find . -name "*.jpg"
  • find . -iname "*.jpg" #case insenstive version

Look for specific filetypes
  • find . -type d #directories
  • find . -type f #files
  • find . -type l #links (wth?)
  • find . -type s #sockets (wth?)

Find by size:
  • find ~/Movies/ -size +1024M

Find by last modified time (last 1 day)
  • find /etc/ -user root -mtime 1
Arguments
-atime: when the file was last accessed
-ctime: when the file's permissions were last changed
-mtime: when the file's data was last modified
-amin: when (in minutes) the file was last accessed
-cmin: when (in minutes) the file's permissions were last changed
-mmin: when (in minutes) the file's data was last modified

!
exclude everything that comes after this...

Collect files that are not owned by valid users and delete them
  • find / -nouser -print0 | xargs -0 rm

Clean the images off of your *nix desktop
  • find ~/Desktop -name "*.jpg" -o -name "*.gif" -o -name "*.png" -print0 | xargs -0 mv --target-directory ~/Pictures
  • The -print0 option terminates results with a null character instead of the default newline, making it cleaner and less likely to balk in many cases

Correct the permissions on your web directory
  • find /your/webdir/ -type d -print0 | xargs -0 chmod 755
  • find /your/webdir -type f | xargs chmod 644

Show a list of files in /etc that have been modified since last month
  • find /etc -mtime -30


Refs (~completely taken with gratitude from)
737 words · edited Jul 17, 2011

💬 0 comments

💬 Write a comment