How to filter files in files and files which are in a sub-directory with "grep"?
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
You need to be more specific about what you are trying to accomplish.
Do not assume that we have the same thought.
Now say you have a directory with various files that are text based, and you need to search in all those specific files for a certain pattern, quickly.
for i in `ls -1 /dir/*.txt`;do cat $i | grep "pattern";done
The above command looks in the dir directory for all files ending with .txt extension, places them in a list in memory, then for each file found, it runs the cat command on it.
The output of the cat command is then piped ( | ) to grep, which searches for pattern specified.
Alternatively, if it is a long output list being generated you could send all of that information to a separate file to be later parsed like so:
for i in `ls -1 /dir/*.txt`;do cat $i | grep "pattern" >> resultfile.txt;done
Otherwise, I should put it this way.
I want to find the files whose content include the string "*.png".
And I don't exactly know which directory the files belong. So I should search the files in all the files in root "/".
Now I have no idea how to use "grep" in this way.
Can you catch my idea?
Thanks for your patience, zest and kindness.
You can also use the c option to just count the occurences of .png in a file. Then you just make an inverse grep on ":0" to get the files which have the string you searched for.
I think Mr.Bebo is right.
And what Mr.footfrisbee said is only to find the file name including string "*.png".
But I want to find the file content including "*.png".
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.