Skip to main content

Find filenames list by content search and file format

Generally computer users can memorize the locations of different files that they read or store. As the number of files increases, they can use descriptive filenames making it easy to guess the content by looking at the file name. However when the number of files grows higher & higher in number, it becomes a pain to remember locations or filenames. Also when a computer is used by some other person than the owner, it becomes even harder to locate some files.
Search by partial name

find -name "<filename pattern>"

Above command is useful only if you can remember the filenames at least some parts of the file name. If you are a software developer, you know how many times you would want to search files based on the content.
Search by content

find <path> -name "<file name pattern>" -exec grep -l "<text to search>" {} \;

Above command can be used to find the files based on content search. For example to find a file with a value say "db.user" in a properties file inside "/opt/work/project" folder (including sub folders) following command can be used.

find /opt/work/project -name "*.properties" -exec grep -l "db.user" {} \;

IgnoreCase search can be done by adding "-i" attribute to grep command; check following command.

find /opt/work/project -name "*.properties" -exec grep -il "db.user" {} \;
When would content search be used?

You remember some parts of the content but not the file name
Find which property file contains an specific property used in your program
Locate the Style sheet (css file) containing a specific style class or property

Comments

Popular posts from this blog

Files creation

In LINUX files can be created by using any of the three methods given below:        1.  Cat command (cat - CONCATENATION)        2. Touch command        3. Vi editor 1. cat command: cat stands for CONCATENATE. This is the basic command when we start learning LINUX/UNIX, as the name suggest it is used to create new file, concatenate files and display the content of the files on the standard output.     Different examples of cat command which will be useful for the beginners. 1. Creating new file 2. View the contents of the file 3. View the contents of  multiple files 4. Display the output of a file using page wise 5. cat command without filename arguments 6. Display the content of a file with line numbers 7. Copy the contents of a one file to another file 8. Appending the contents of one file to another file 9. Redirecting the output of multiple files into a single file. 10. Getting input using standard input operator 11. Storing the output of multiple files into

grep Word Count Command

How do I count words using grep command under Linux / Unix like operating systems? You can pass the -c option to grep command to suppress normal output and display a count of matching lines for each input file. The syntax is follows: grep -c "string" file In this example, search for a word called ‘var’ and display a count of matching lines: grep -c 'var' /etc/passwd You can pass the -v option to count non-matching lines: grep -v 'var' /etc/passwd However, this will not count words. To count exact matched words, enter: grep -o -w 'word' /path/to/file/ | wc -w The grep -o command will only display matched words and the wc -c command will display the word counts.