1

I want to search the following by grep + show the seven most recently modified files. I can list those files by ls -1t | head -n 7. Code which order alphabetically as default

grep -i "root" *.tex

OS: Debian 8.7

1
  • 1
    What do you mean by "Code but without custom order"? You have already answered your own question. grep -i "root" $(ls -t *.tex | head -n7).
    – DopeGhoti
    Jun 13, 2017 at 15:59

1 Answer 1

2

Use zsh:

grep -i -- root *.tex(.om[1,7])
  • (.om[1,7]): glob qualifier
  • .: regular files only
  • om: order by last modification time (youngest first like in ls -t)
  • [1,7] only first to seventh ones.

If you also want to consider symlinks to tex files (and consider the mtime of the file at the end of the symlink chain), add a - glob qualifier before the . which tells zsh that the following glob qualifiers apply to the target of the symlinks. If you also want to consider hidden tex files, add the D glob qualifier.

3
  • What is the syntax (om[begin,end])? How does om come? Jun 13, 2017 at 16:00
  • Great! How did you find the list of these glob qualifiers in Zsh manual? - - Adding D like grep -i root *.tex(om[1,7]D)? Jun 13, 2017 at 16:07
  • 1
    @Léo, see link. In info zsh (you may need to install the zsh-doc package), bring the index with i and type qualif<Tab> Jun 13, 2017 at 16:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .