Bash – List all php files inside a folder with modification date and permissions

bashfind

I did write a script which checks all php files within a given folder for changes every hour, so I can detect possible code injections and their integrity. It's working so far, but my approach seems a little slow and forks too many ls processes. Here's the code:

/usr/bin/find /home/www -name "*.php" -exec ls -l \{} \;

Is there a better way?

Please note I need, at a minimum, full path, permissions, owner, group and modification time for each of the files.

Thanks!

Best Answer

Find has a built-in action "-ls" that should do what you need (see below). If that isn't quite the info you're after, you can also use -printf and format directives to control exactly what gets printed.

james@Brindle:/tmp/blah$ time /usr/bin/find . -name "*.php" -exec ls -l \{} \;
-rw-r--r--  1 james  wheel  0  8 Jul 08:50 ./other.php
-rw-r--r--  1 james  wheel  0  8 Jul 08:50 ./thing.php

real    0m0.015s
user    0m0.005s
sys     0m0.008s
james@Brindle:/tmp/blah$ time find . -ls
1631212        0 drwxr-xr-x    4 james            wheel                 136  8 Jul 08:50 .
1631215        0 -rw-r--r--    1 james            wheel                   0  8 Jul 08:50 ./other.php
1631213        0 -rw-r--r--    1 james            wheel                   0  8 Jul 08:50 ./thing.php

real    0m0.006s
user    0m0.002s
sys     0m0.003s
Related Topic