LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 06-13-2019, 08:15 AM   #1
SecureShell
LQ Newbie
 
Registered: Jun 2019
Posts: 7

Rep: Reputation: Disabled
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?
 
Old 06-13-2019, 08:24 AM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,328
Blog Entries: 3

Rep: Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726
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.
 
Old 06-13-2019, 08:30 AM   #3
SecureShell
LQ Newbie
 
Registered: Jun 2019
Posts: 7

Original Poster
Rep: Reputation: Disabled
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.

Last edited by SecureShell; 06-13-2019 at 08:32 AM.
 
Old 06-13-2019, 08:34 AM   #4
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,328
Blog Entries: 3

Rep: Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726
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.
 
Old 06-13-2019, 08:47 AM   #5
SecureShell
LQ Newbie
 
Registered: Jun 2019
Posts: 7

Original Poster
Rep: Reputation: Disabled
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?
 
Old 06-13-2019, 08:49 AM   #6
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,328
Blog Entries: 3

Rep: Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726
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?
 
Old 06-13-2019, 08:57 AM   #7
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,781

Rep: Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214
Quote:
Originally Posted by SecureShell View Post
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" ?
 
Old 06-13-2019, 09:12 AM   #8
SecureShell
LQ Newbie
 
Registered: Jun 2019
Posts: 7

Original Poster
Rep: Reputation: Disabled
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?
 
Old 06-13-2019, 09:14 AM   #9
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,328
Blog Entries: 3

Rep: Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726
Quote:
Originally Posted by SecureShell View Post
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.
 
Old 06-13-2019, 09:16 AM   #10
SecureShell
LQ Newbie
 
Registered: Jun 2019
Posts: 7

Original Poster
Rep: Reputation: Disabled
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.

Last edited by SecureShell; 06-13-2019 at 09:19 AM.
 
Old 06-13-2019, 09:19 AM   #11
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,328
Blog Entries: 3

Rep: Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726
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.
 
1 members found this post helpful.
Old 06-13-2019, 09:23 AM   #12
SecureShell
LQ Newbie
 
Registered: Jun 2019
Posts: 7

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Turbocapitalist View Post
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?
 
Old 06-13-2019, 09:28 AM   #13
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,328
Blog Entries: 3

Rep: Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726
Quote:
Originally Posted by SecureShell View Post
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.

Last edited by Turbocapitalist; 06-13-2019 at 09:29 AM.
 
1 members found this post helpful.
Old 06-13-2019, 09:36 AM   #14
SecureShell
LQ Newbie
 
Registered: Jun 2019
Posts: 7

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Turbocapitalist View Post
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.👍
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Tried everything, searched and searched: Php7 (LAMP install) signmeuptoo Linux - Newbie 64 08-01-2017 12:35 PM
Dolphin file manager does not update display when moving or deleting searched-for files grumpyskeptic Linux - Software 1 05-09-2017 10:48 AM
[SOLVED] How to exclude a particular directory from being searched while using grep function akthomas1 Linux - Newbie 1 04-14-2016 04:10 AM
[SOLVED] how to use cp find and grep together to copy a list of files using find with grep babhijit Linux - Newbie 10 07-03-2013 12:25 PM
I searched and searched so... NeeWom Linux - Software 0 04-16-2004 12:14 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 02:06 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration