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?
|
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. |
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.
|
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 ./* Code:
./a/b/c/d.txt Code:
less ./a/b/c/d.txt |
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?
|
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? |
Quote:
What is the OS and platform where you are seeing this? What is the output when you run "type grep" ? |
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?
|
Quote:
|
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. |
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; |
Quote:
|
Quote:
Code:
find /some/path/to/the/files/ -type f -executable -print; 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; |
Quote:
|
All times are GMT -5. The time now is 07:40 PM. |