I am trying to determine which files will be touched based on their modified date. My directory is a top a list of perhaps several levels of subdirectories.

From a terminal window I run this command:

find Program.8.koeb/ -mtime -10

Which correctly produces a list of files modified within the past 10 days. However if I want to query this list to make sure that the files I want to touch using the find command, I would run this command:

find Program.8.koebe/ -mtime -10 -exec ls -gotrhR {} \;

Which produces a long list of all the files within directories of my current working directory, including subdirectories, along with the named directory find is assigned to look. So, -exec ls -gotrhR {} \; seems to be producing the same output as:

find . -mtime -10 -exec ls -gotrhR {} \;

Which is not my intention.

  • 3
    Why would you run ls -R, when find is already recursive? Why would you run ls manually at all, when find has an -ls command? – muru Feb 25 '16 at 17:09
  • As far as I know, the -ls in find does not take switch arguments, thus the output is rather cluttered with unreadable file size numbers - there is also no total size values given for each listed directory. I use the -go switch in ls to remove my user name from all the listed files owner and groups, since this is of no use to someone who will read a print out of the results. – Gustav Feb 26 '16 at 13:19
  • the size reported by ls is useless as a size of the directory and its contents. Are you looking for du? – muru Feb 26 '16 at 13:23

When you pass a directory to ls, it lists the files in that directory, not the directory itself. With the -R flag, ls lists files in subdirectories recursively. To make ls list directories rather than their contents, pass the -d option.

The -t and -r flag are useless since they only affect sorting and you're listing one file at a time.

find Program.8.koebe/ -mtime -10 -exec ls -gohd {} \;

With GNU find, you can use -printf instead of invoking an external command. The output format will be similar but not exactly identical.

find Program.8.koebe/ -mtime -10 -printf '%M %n %s %M %c %p\n'
| improve this answer | |
  • thanks for the answers Gilles - the -t and the -r are actually quite helpful for me in the reports that I create, though. I knew of the -printf switch but never used it so much. I haven't tried to tweak the -printf parameters to produce a clean read out yet. – Gustav Feb 26 '16 at 13:52
  • 1
    @Gustav ls -rt doesn't do anything when you pass one file at a time. find … -exec ls -trd {} + would call ls in batches, so it will sort, but only per batch: if there are too many files, ls will be called multiple times and so the output won't be sorted. To get sorted output, your best bet is to use a parseable date format (ideally YYYY-MM-DD HH:MM:SS) and pipe through sort. Or use zsh globbing flags (which requires installing the zsh package). – Gilles 'SO- stop being evil' Feb 26 '16 at 14:03

Just add '-type f' cause -exec ls -l {} will include those are directories and list them, too.

| improve this answer | |

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.