Using Find and Grep

Find

Find Types of Files

Find all regular files (not directories or links, etc) starting from current directory.

find . -type f

File Types

   -type t
             True if the file is of the specified type.  Possible file types
             are as follows:

             b       block special
             c       character special
             d       directory
             f       regular file
             l       symbolic link
             p       FIFO
             s       socket

Finding Files Younger than X

Numeric arguments to find can be prefixed with + or - if you want to find less than/greater than n instead of exactly n

Examples:

Less than 10 minutes old.

find . -mmin -10

More than 10 minutes old

find . -mmin +10

Less than 1 day old

find . -mtime -1

More than 1 day old

find . -mtime +1

Exactly 1 day old

find . -mtime 1

Accessed less than 1 minute ago

find . -amin -1

Find Files based on File Size

find . -size +100000c  # find files w/size > 100,000 bytes
find . -size -100000c  # find files w/size < 100,000 bytes

Find Text in Files

Find files ending in .c, that contain foo them.

find . -name \*.c | xargs grep foo

Same thing, but print filename and line numbers of where you found 'foo'.

find . -name \*.c | xargs grep -n 'foo'

If you want to search for files that match X, then find Y in them, and can't remember the find/grep combinations, you can try one of these scripts:

Count Files in Folder

find /path/to/directory -type f -print | wc -l

View details of files

find /path/to/directory -type f -ls

Perform Operations on Files

From http://www.athabascau.ca/html/depts/compserv/webunit/HOWTO/find.htm#EX03

How to search for a string in a selection of files (-exec grep …).

find . -exec grep "www.athabasca" '{}' \; -print 

This command will search in the current directory and all sub directories. All files that contain the string will have their path printed to standard output.

If you want to just find each file then pass it on for processing use the -q grep option. This finds the first occurrance of the search string. It then signals success to find and find continues searching for more files.

find . -exec grep -q "www.athabasca" '{}' \; -print 

This command is very important for process a series of files that contain a specific string. You can then process each file appropriately. An example is find all html files with the string “www.athabascau.ca”. You can then process the files with a sed script to change those occurrances of “www.athabascau.ca” with “intra.athabascau.ca”.

Moving Files

find . -iname "*jpg" -exec mv {} . \;

Prune

You use the -prune option to tell find not to bother with certain directories. This can speed up your search considerably. You specify -prune after the expression that you don't want to see. For example:

FIXME

Grep

This should be its own page. FIXME

Character Groups

Use square braces to identify character groups. Characters are literal (do not need to be escaped).

# Find any of the characters between the [ and ] below in some_file
grep '[*.\^$]' some_file
# You can search for ] in a character group if the ] is the 
# first character after the opening brace.
# Finds the ] as well as other characters in some_file.
grep '[][*.\^$]' some_file
 
find-grep.txt · Last modified: 2009/08/04 12_51 by tookelso
 
Except where otherwise noted, content on this wiki is licensed under the following license:CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki