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

A command to list all users? And how to add, delete, modify users?

I need a command to list all users in terminal. And how to add, delete, modify users from terminal. Solution:   To list all local users you can use: cut -d: -f1 /etc/passwd To list all users capable of authenticating (in some way), including non-local, see this reply: https://askubuntu.com/a/414561/571941 Some more useful user-management commands (also limited to local users): To add a new user you can use: sudo adduser new_username or: sudo useradd new_username See also: What is the difference between adduser and useradd? To remove/delete a user, first you can use: sudo userdel username Then you may want to delete the home directory for the deleted user account : sudo rm -r /home/username (Please use with caution the above command!) To modify the username of a user: usermod -l new_username old_username To change the password for a user: sudo passwd username To change the shell for a user: sudo chsh username To change the details for a user (for example real name): sudo chfn u...