I have below command which displays files/directories having EXEC_EC


ls -ltr | grep 'EXEC_EC'

By using above command: Can we write a find command to filter the files created before 7 days?

I am trying to list the files having name EXEC_EC and trying to find the files older than 7 days.

Sample output

example input: 

dr-xr-xr-x   1 home abc       999 Jun 30 11:12 EXEC_EC_ABC_1
dr-xr-xr-x   1 home abc       999 Jul 25 12:44 EXEC_EC_ABC_2
dr-xr-xr-x   1 home abc       999 Aug  1 22:10 EXEC_EC_ABC_3
dr-xr-xr-x   1 home abc       999 Aug  2 22:20 Number
dr-xr-xr-x   1 home abc       999 Aug  2 22:20 Number2

i am trying to get below output 


It does not make sense to use grep for filtering if you use find anyway.

find . -mindepth 1 -maxdepth 1 -name '*EXEC_EC*' -mtime +7

By default find traverses the whole subtree (i.e. the contents of all subdirectories). -mindepth 1 -maxdepth 1 prevents that as the example works on the content of a single directory only. -mindepth 1 makes find ignore the directory arguments (in this case, ., not necessary but in the general case). -maxdepth 1 prevents descending into the subdirectories.

  • @ Thanks a lot Hauke for the reply . I have added sample output in my question.. Is this possible to get same output ? I tried this command but i am getting empty output
    – Arya
    Aug 2 '20 at 18:36
  • @Arya I removed the -ls. Should do as expected now. Aug 2 '20 at 18:41
  • @can i know what is meant by mindept /maxdepth and how it works.. (By this way this solution worked) :)
    – Arya
    Aug 2 '20 at 18:43
  • @Arya I added that information to the question. Aug 2 '20 at 18:49
  • @Thanks a lot Hauke. Can i ask one more doubt ?
    – Arya
    Aug 2 '20 at 19:10

This is what you are looking for ?

$ find . -name '*EXEC_EC*' -mtime +7 -print

. = Represent your current directory,you can replace that with a directory path .

-mtime - Represents the file modification time and is used to find files older than 7 days.

  • The question says files/directories. Your solution handles files only. Aug 2 '20 at 18:37
  • I added sample input/output in my question
    – Arya
    Aug 2 '20 at 18:37
  • 1
    Updated my answer . But i will go with @Hauke Laging as mindepth and maxdepth gives more accurate results.
    – Harish P C
    Aug 2 '20 at 18:47

You don't need to pipe the output of ls into grep. Grep has a built in option for that.

grep -rle 'EXEC_EC'

-l will only list files -r recursive means will go through all files starting from you current path -e after this grep expects your pattern as a string

  • 1
    -r is not recursive with ls. Furthermore you mix up fire content with file name. Aug 2 '20 at 18:24
  • Thanks for the answer. but this doesn't works. I have some n number of files in one directory. Among then i required only files having name : 'EXEC_EC' and then on top of that i want to find all the list of files older than 7 days.
    – Arya
    Aug 2 '20 at 18:27
  • @Hauke Laging. The author didn't specify he NEEDED to use ls for it. As you can read I said he doesn't need to pipe it.
    – mad_a_i
    Aug 2 '20 at 18:30
  • 1
    Right but the example outputs the content of one directory without the subdirectories. Yours includes subdirectories. That is a severe problem (not the worst, though). Aug 2 '20 at 18:35

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.