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