Thursday, June 25, 2009

find command - search content of a file

find in Unix is a very powerful tool for users which give you a very good flexibility. find command has a very broad usage among users. Here is the way how you can use find command to search files and content within files.

In order to search for a particular file named "test.txt" in /home directory, use
find /home -name "test.txt" -print
Search all the .txt files in /home directory, use
find /home -name "*.txt" -print
if you want to search for a string through all the files .txt files, there are two ways
find /home -name "*.txt" -exec grep -q "string-to-be-searched" '{}' \; -print
find /home -name "*.txt" | xargs grep "string-to-be-searched"
second one is more fast and efficient. Removal of -q in the first command will output the text segment where the specified string is. If you want to search all files for the string, just remove -name "*txt" option.

No comments:

Post a Comment