Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

for example I type

ls -altr | grep "23 Dec"

so it will show files for 23 Dec only and I want to use grep for them, like

ls -altr | grep "23 Dec" | xargs grep -l "some_string"

but this doesn't work)

How to do that?

share|improve this question
    
OS or what distribution is it? –  warl0ck Dec 25 '12 at 6:47
    
@warl0ck Red Hat Enterprise Linux Server release 5.8 (Tikanga) 2.6.18-308.1.1.el5 –  VextoR Dec 25 '12 at 7:36
1  
Note that you really shouldn't parse the output of ls –  rahmu Feb 2 '13 at 10:53

2 Answers 2

up vote 5 down vote accepted

If you want to use your way, try maybe:

ls -altr | grep "23 Dec" | awk '{print $9}' | xargs -i grep -l "some_string" {}

or with find I would do:

find . -type f -newermt 2012-12-23 ! -newermt 2012-12-24 -exec grep -l "some_string" {} \;

In the find command, don't forget the escaped semicolon. Also note that find works much the same way as xargs when used with the -exec option.

share|improve this answer
    
doesn't work :( grep: invalid option -- - Usage: grep [OPTION]... PATTERN [FILE]... Try grep --help' for more information.` –  VextoR Dec 25 '12 at 6:31
    
yes, you need awk to print the right column that contains the filename, if you want to use xargs. –  ixtmixilix Dec 25 '12 at 6:38
    
when I use find it says: invalid predicate -newermt –  VextoR Dec 25 '12 at 6:43
    
did you copy and paste the command above? it works fine for me. also, what OS are you running and what version of find are you using (find -version). in my case, i'm using find (GNU findutils) 4.4.2 –  ixtmixilix Dec 25 '12 at 7:40
    
Red Hat Enterprise Linux Server release 5.8 (Tikanga) 2.6.18-308.1.1.el5 GNU find version 4.2.27 –  VextoR Dec 25 '12 at 7:43

If you were trying to find some files, that was modified on a specific date,

there would be no need for pipe, (ls uses modify time by default)

find . -type f -newermt 2012-12-23 ! -newermt 2012-12-24 -iname '*some_string*'

share|improve this answer
    
it says: find . -type f -newermt 2012-12-24 ! -newermt 2012-12-25 -iname '*50424876*' find: invalid predicate -newermt'` –  VextoR Dec 25 '12 at 6:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.