Skip to main content

Linux find file names with given string

 I'd like to find all files in the current directory and subdirectories whose name contains the string "ram".

Solutions:
1.  find . -type f -name "*ram*"

The find command will take long time because it scans real files in file system.

The quickest way is using locate command, which will give result immediately:

2. locate "ram"
If the command is not found, you need to install mlocate package and run updatedb command first to prepare the search database for the first time.


How to install locate command on Amazon Linux?

$ sudo yum install mlocate

$ sudo updatedb


$ sudo updatedb
$ locate "ram"
/home/work/ram.txt
/usr/share/mime/application/vnd.google-earth.kml+xml.xml
/usr/share/mime/application/vnd.google-earth.kmz.xml
/usr/share/mime/text/x-google-video-pointer.xml






3. find . -type f -exec grep -H 'text-to-find-here' {} \;

Usage : find . -type f -exec grep -H 'ram' {} \;

4.grep -rnw '/path/to/somewhere/' -e "pattern"
Usage: grep -rnw . -e "ram"

-r or -R is recursive, R and r will both traverse directories correctly, but R will follow symbolic links
-n is line number, and
-w stands for match the whole word.
-l (lower-case L) can be added to just give the file name of matching files.
Along with these, --exclude, --include, --exclude-dir or --include-dir flags could be used for efficient searching:

This will only search through those files which have .c or .h extensions:
grep --include=\*.{c,h} -rnw '/path/to/somewhere/' -e "pattern"
This will exclude searching all the files ending with .o extension:
grep --exclude=*.o -rnw '/path/to/somewhere/' -e "pattern"
Just like exclude files, it's possible to exclude/include directories through --exclude-dir and --include-dir parameter. For example, this will exclude the dirs dir1/, dir2/ and all of them matching *.dst/:
grep --exclude-dir={dir1,dir2,*.dst} -rnw '/path/to/somewhere/' -e "pattern"


4.grep -Ril "text-to-find-here" path_to_search
Usage: grep -Ril "ram" .

i stands for ignore case (optional in your case).
R stands for recursive.
l stands for "show the file name, not the result itself".


5.find /path_to_search -type f | xargs grep -l 'text-to-find-here'
Usage: find /path_to_search -type f | xargs grep -l 'google'

Optional flags you may want to add to grep:
-i - case insensitive search
-l - only output the filename where the match was found
-h - only output the line which matched (not the filename)


6. grep -linsr "pattern" path_to_search
Usage: grep -linsr "google" *

i: Ignore case distinctions in both the PATTERN and the input files.
n: Prefix each line of output with the 1-based line number within its input file.
s: Suppress error messages about nonexistent or unreadable files.
r: Read all files under each directory, recursively.
l: only output the filename where the match was found


7. find / -type f -exec grep -l "text-to-find-here" {} \; 
Usage: find * -type f -exec grep -l "ram" {} \;

8. find search_path | xargs grep 'search_pattern' -sl
Usage:find . | xargs grep 'ram' -sl

./google.txt
./lv.txt
./ten_by_ten.txt
./presidential_polls.csv


9.grep -e TEXT *.log | cut -d' ' --complement -s -f1

Usage: grep -e "google" *.txt | cut -d' ' --complement -s -f1


10. find / -type f -exec grep -sH 'text-to-find-here' {} \;
Usage: find /home/work -type f -exec grep -sH 'ram' {} \;

grep -c Your_Pattern *
Usage: grep -c "ram" *
This will report how many copies of your pattern are there in each of the file in the current directory.

11. To search for the string and output just that line with the search string:
for i in $(find /path/of/target/directory -type f); do grep -i "the string to look for" "$i"; done
Usage: for i in $(find /home/work -type f); do grep -i "google" "$i"; done

To display filename containing the search string:
for i in $(find /path/of/target/directory -type f); do if grep -i "the string to look for" "$i" > /dev/null; then echo "$i"; fi; done;
Usage:for i in $(find /home/work -type f); do if grep -i "google" "$i" > /dev/null; then echo "$i"; fi; done;


 

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 fi...

Grep Command Examples of How to Search a File for a Pattern

The UNIX Grep command searches files for a user-specified text pattern. It returns a list of the matching words or shows each line of text that contains them. You can broaden the results by using wildcards. Grep also has the ability to count instances of a search phrase that appear in a file. The syntax for this UNIX command includes three parameters, but only the text pattern is required: grep [options] pattern [filename] If it contains multiple words or special characters, be sure to place single quotation marks before and after the pattern. The UNIX shell does not interpret dollar signs, brackets, parentheses, pipes or asterisks in the same way as letters. For example, an asterisk is treated as a wildcard when you don't use quotes. If you leave out the filename, UNIX will collect data from the standard input device. This is usually the keyboard. (1) Basic search with no options The following example shows what happens if you use grep to search for the word "unix...