Thursday, February 08, 2007

find command with examples

find command all we know but here u find some example,

find . -name "rc.conf" -print
This command will search in the current directory and all sub directories for a file named rc.conf.

find /usr/src -not \( -name "*,v" -o -name ".*,v" \) '{}' \; -print
This command will search in the /usr/src directory and all sub directories. All files that are of the form '*,v' and '.*,v' are excluded. Important arguments to note are:

* -not means the negation of the expression that follows
* \( means the start of a complex expression.
* \) means the end of a complex expression.
* -o means a logical or of a complex expression.
In this case the complex expression is all files like '*,v' or '.*,v'

The above example is shows how to select all file that are not part of the RCS system. This is important when you want go through a source tree and modify all the source files... but ... you don't want to affect the RCS version control files.

find . -exec grep "www.athabasca" '{}' \; -print
This command will search in the current directory and all sub directories.

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"

4 comments:

Anonymous said...

find . -type f -regex '.*\/\.svn.*' -exec ls "{}" \;

Anonymous said...

1. #find ~/ -name 'test*' -exec rm {} \;
# Removes all test files from user's home directory.

2. #find /home/prabhat/projects -mtime 1
Lists all files in /home/prabhat/projects directory tree that were modified within the last day.
# mtime = last modification time of the target file
# ctime = last status change time (via 'chmod' or otherwise)
# atime = last access time

3. #find '/home/prabhat' -type f -atime +5 -exec rm {} \;
Deletes all files in "/home/prabhat that have not been accessed in at least 5 days.
# "-type filetype", where
# f = regular file
# d = directory
# l = symbolic link, etc.

4. #find /etc -exec grep '[0-9][0-9]*[.][0-9][0-9]*[.][0-9][0-9]*[.][0-9][0-9]*' {} \;
# Finds all IP addresses (xxx.xxx.xxx.xxx) in /etc directory files.
#find /etc -type f -exec cat '{}' \; | tr -c '.[:digit:]' '\n' | grep '^[^.][^.]*\.[^.][^.]*\.[^.][^.]*\.[^.][^.]*$'
#filtered resut out

Anonymous said...

find . -type f \( -name "*.class" -o -name "*.sh" \)

Example of how to search in the current directory and all subdirectories for files ending with the the extensions ".class" and ".sh"

find . -type f \( -name "*cache" -o -name "*xml" -o -name "*html" \)
here is an example of how to search the current directory for files that end in any of three different files extensions

JAvin @ HashTable vs HashMap in Java said...

Nice article. here is my way of using find command in Unix hope this would be useful for you.

SHOW ENGINE INNODB STATUS

  The SHOW ENGINE INNODB STATUS command in MySQL provides detailed information about the internal state of the InnoDB storage engine. This ...