Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 06-17-2012
Registered User
 
Join Date: May 2009
Posts: 133
Thanks: 23
Thanked 0 Times in 0 Posts
How to do ls -l on results of grep and find?

Hi,

Am running the command below to search for files that contains a certain string.


Code:
grep -il "shutdown" `find . -type f -mtime -1 -print` | grep "^./scripts/active"

How do I get it to do a ls -l on the list of files? I tried doing
Code:
ls -l `grep -il "shutdown" `find . -type f -mtime -1 -print` | grep "^./scripts/active"`

and that does not work. The command just hangs there.

I was hoping to use find -exec but I can't get it to do exec and grep at the same time. FYI, the grep "^./scripts/active" is 'coz that is the directory that am interested in.

This is what I end up doing. Hoping someone can advise a one-liner


Code:
for file in $(grep -il "shutdown" `find . -type f -mtime -1 -print` | grep "^./scripts/active")
do
   ls -l $file
done

Any advise much appreciated. Thanks in advance.

Last edited by Scrutinizer; 06-17-2012 at 02:51 AM.. Reason: Add what I did - mod: extra code tags
Sponsored Links
    #2  
Old 06-17-2012
rangarasan's Avatar
Registered User
 
Join Date: Jul 2011
Location: Chennai, India
Posts: 484
Thanks: 9
Thanked 119 Times in 115 Posts
find

Hi,

Try this one,

Code:
find . -type f -mtime -1 -print -exec grep -il 'shutdown' {} \; | grep "^./scripts/active"

Cheers,
Ranga:-)
Sponsored Links
    #3  
Old 06-17-2012
Registered User
 
Join Date: May 2009
Posts: 133
Thanks: 23
Thanked 0 Times in 0 Posts
Hi Ranga,

Thanks for your reply.

Tried that and it didn't work. It seems to print all the files and not the ones that just contains the text that am looking for.

Also, I've managed to find the files that am looking for using grep -il "shutdown" `find . -type f -mtime -1 -print` | grep "^./scripts/active", but I want to a ls -l of them so I can see the timestamp of the files.
    #4  
Old 06-17-2012
rangarasan's Avatar
Registered User
 
Join Date: Jul 2011
Location: Chennai, India
Posts: 484
Thanks: 9
Thanked 119 Times in 115 Posts
Awk

Hi,

Try this one,

Code:
find . -type f -mtime -1 -print | grep "^./scripts/active" | xargs  -I '{}' ls -l {}

Cheers,
Ranga:-)
Sponsored Links
    #5  
Old 06-17-2012
Lem Lem is offline
Registered User
 
Join Date: Jun 2012
Location: Lombardia, Italy
Posts: 179
Thanks: 5
Thanked 38 Times in 38 Posts
Quote:
Originally Posted by newbie_01 View Post
How do I get it to do a ls -l on the list of files? I tried doing
Code:
ls -l `grep -il "shutdown" `find . -type f -mtime -1 -print` | grep "^./scripts/active"`

and that does not work. The command just hangs there.
Yep, but try this:


Code:
ls -l $(grep -il "shutdown" `find . -type f -mtime -1 -print | grep "^./scripts/active"`)

The option -print, instead of -print0, can easily lead to errors. You pipe the output to grep. What if you have a file with a space in its name, for example?

Besides, I don't understand very well why you're using

Code:
find . ... | grep "^./scripts/active"

instead of:

Code:
find ./scripts/active ...

So, putting all toghether, I'd try:


Code:
ls -l `find ./scripts/active -type f -mtime -1 -print0 | xargs -0 grep -il "shutdown"`

or, which is the same:

Code:
ls -l $(find ./scripts/active -type f -mtime -1 -print0 | xargs -0 grep -il "shutdown")


Last edited by Lem; 06-18-2012 at 05:05 PM..
Sponsored Links
    #6  
Old 06-17-2012
Registered User
 
Join Date: May 2012
Posts: 58
Thanks: 5
Thanked 9 Times in 9 Posts

Code:
find ./scripts/active -type f -mtime -1 -exec grep -il 'shutdown' {} \; -ls | grep -v '^\.'

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Grep no results Benou Shell Programming and Scripting 3 10-17-2011 07:46 AM
My ps -ef|grep command results are chopped off bsp18974 UNIX for Dummies Questions & Answers 1 08-14-2007 10:35 AM
List grep results slire UNIX for Dummies Questions & Answers 14 10-31-2006 10:42 AM
How to refine results of grep -p priceb Shell Programming and Scripting 2 06-28-2006 08:40 AM
Multiple Grep Results - Formatting sysera Shell Programming and Scripting 7 03-25-2004 05:04 AM



All times are GMT -4. The time now is 02:00 AM.