4

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?

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

2 Answers 2

5

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.

7
  • doesn't work :( grep: invalid option -- - Usage: grep [OPTION]... PATTERN [FILE]... Try grep --help' for more information.`
    – VextoR
    Dec 25, 2012 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, 2012 at 6:38
  • when I use find it says: invalid predicate -newermt
    – VextoR
    Dec 25, 2012 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, 2012 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, 2012 at 7:43
1

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*'

1
  • it says: find . -type f -newermt 2012-12-24 ! -newermt 2012-12-25 -iname '*50424876*' find: invalid predicate -newermt'`
    – VextoR
    Dec 25, 2012 at 6:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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