LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Display contents of the file searched by grep or find command (https://www.linuxquestions.org/questions/linux-general-1/display-contents-of-the-file-searched-by-grep-or-find-command-4175655636/)

SecureShell 06-13-2019 08:15 AM

Display contents of the file searched by grep or find command
 
I have lots of directories in a certain directory and I am trying to find a specific file present in there. I used grep command recursively and was able to locate the file being present in there, but I don't know where it is present exactly. So, how can I use cat on that file to display the contents of that file?

Turbocapitalist 06-13-2019 08:24 AM

Welcome.

By default, the utility grep will tell you which file the pattern was found in along with the line on which it was found. If you want to see just the file name, try adding the -l option to it.

One you've found the file, you can use a pager like less or more to see it one page at a time. Despite the name less is greater than more and for whichever one you choose, you'll want to take a look at the manual page.

SecureShell 06-13-2019 08:30 AM

I found the file by using grep -l command, but how do I find in which directory that file is present? Can I display the contents of the file without knowing where exactly that file is present in the directory structure? Thank you for the answer.

Turbocapitalist 06-13-2019 08:34 AM

You'll have to know exactly which file to tell one of the pagers to display it.

So if I type,

Code:

grep -l foobar -r ./*
and it comes back with,

Code:

./a/b/c/d.txt
./a/b/e/f.txt

Then I can look at them with,

Code:

less ./a/b/c/d.txt
less ./a/b/e/f.txt

If that is not what you meant then please rephrase your problem.

SecureShell 06-13-2019 08:47 AM

When I do grep -l, it does not provide the complete path of where the file is present, it just gives me the long description of the file, along with the name of the file. The question I want to ask is how do I find the complete path of the file?

Turbocapitalist 06-13-2019 08:49 AM

It will show the relative path. If you want the absolute path, you can calculate that in your head or else use the realpath utility.

Can you show what you have and how you are stuck?

rknichols 06-13-2019 08:57 AM

Quote:

Originally Posted by SecureShell (Post 6004838)
When I do grep -l, it does not provide the complete path of where the file is present, it just gives me the long description of the file, along with the name of the file.

That is a very nonstandard implementation of grep.
What is the OS and platform where you are seeing this?
What is the output when you run "type grep" ?

SecureShell 06-13-2019 09:12 AM

Oh..I understand now. I was doing it all wrong. Actually I am trying to find a file with specific size in the given directory structure. So, for that, I was using grep -l -R command and was piping the output to the grep command as grep <filesize> to find that file. But it gives me a long description of the file without the path of the file. Is there any way to find the path of that file?

Turbocapitalist 06-13-2019 09:14 AM

Quote:

Originally Posted by SecureShell (Post 6004859)
Actually I am trying to find a file with specific size in the given directory structure.

You'll need find instead of grep. The former can list file names selected by size. The latter deals only with the contents.

SecureShell 06-13-2019 09:16 AM

Ok. Thanks for the reply. How do I use the find command to search the file with specific size?

PS- I found the answer after some searching. Thanks for the help.

Turbocapitalist 06-13-2019 09:19 AM

You'd use the -size option, either plain for an exact match, or with a + or - for above or below a certain size. Compare the output:

Code:

find /some/path/to/the/files/ -type f -size 500k -print;
find /some/path/to/the/files/ -type f -size +500k -print;
find /some/path/to/the/files/ -type f -size -500k -print;

Also dig around in the output from "man find" and look at the option -size. Be sure to search for relevant web guides online, too.

SecureShell 06-13-2019 09:23 AM

Quote:

Originally Posted by Turbocapitalist (Post 6004868)
You'd use the -size option, either plain for an exact match, or with a + or - for above or below a certain size. Compare the output:

Code:

find /some/path/to/the/files/ -type f -size 500k -print;
find /some/path/to/the/files/ -type f -size +500k -print;
find /some/path/to/the/files/ -type f -size -500k -print;

Also dig around in the output from "man find" and look at the option -size. Be sure to search for relevant web guides online, too.

Thanks. That was helpful. I have one more question. How can I list all the non-executable files in the given directory?

Turbocapitalist 06-13-2019 09:28 AM

Quote:

Originally Posted by SecureShell (Post 6004870)
How can I list all the non-executable files in the given directory?

Again with find. There are several ways but here is one using the -executable option:

Code:

find /some/path/to/the/files/ -type f -executable -print;
find /some/path/to/the/files/ -type f -not -executable -print;

See also the -perm option there in "man find"

If you are new to find, it would be important to know that between all the options there is an implied logical AND. So these two are the same:

Code:

find /some/path/to/the/files/ -type f -executable -print;
find /some/path/to/the/files/ -type f -and -executable -and -print;

If you want to make more complex queries using logical OR operators, then you'll have to keep precedence in mind.

SecureShell 06-13-2019 09:36 AM

Quote:

Originally Posted by Turbocapitalist (Post 6004876)
Again with find. There are several ways but here is one using the -executable option:

Code:

find /some/path/to/the/files/ -type f -executable -print;
find /some/path/to/the/files/ -type f -not -executable -print;

See also the -perm option there in "man find"

Thanks.. It worked.👍


All times are GMT -5. The time now is 07:40 PM.