Search code examples
linuxshellfindls

On Linux, FIND command with LS not showing proper result without option -d


I need to check all the files created in last day inside current folder which has million of files.

If I used following command correct 80 or 90 files are shown. which is correct.

find . -ctime -1 -exec ls -ld {} \;

but if I change ls option to rt or l or anything else without d. it shows wrong result. It shows thousands of file. I eventually have to press control C to stop the output. All following are wrong never ending results.

find . -ctime -1 -exec ls -l {} \;
find . -ctime -1 -exec ls -t {} \;
find . -ctime -1 -exec ls -rt {} \;
find . -ctime -1 -exec ls  {} \;

Can someone tell what is going on here. Why it works only with d. following works though

find . -ctime -1 -exec ls -lrtd  {} \;

Solution

  • If you do ls /tmp, you'll see why. It gives you all the files in the /tmp directory rather than the details on the directory itself.

    You can 'fix' this by using -d to have ls show the directory rather than the files within it (as you've seen) or by using find -type f to restrict your search to regular files only. That's assuming it's only regular files you're interested in, there are also things like FIFO pipes and devices which exist in the hierarchy, but there are other variations on -type that will pick those up if needed (see find man page for details).