Example:
find / *
Gives me all files and directories, but I want only those files I may read and those directories I may see the contents of. Otherwise I get problems as when I try to find file information for all files:
for i in ls $( find / * ); do file $i; done
Which results in:
find: /lost+found: Permission denied find: /proc/tty/driver: Permission denied find: /proc/1/task/1/fd: Permission denied find: /proc/1/fd: Permission denied find: /proc/2/task/2/fd: Permission denied find: /proc/2/fd: Permission denied find: /proc/3/task/3/fd: Permission denied # and so on ...
If it’s possible I would like it in a generic way, so that I may use the same command line regardless of which user I am logged in as, but still get those files and directories I may see as a result from find.
Use the
-readableoption tofind(assuming a modern system using GNU findutils):Using
-permand variants doesn’t work because it only looks at the file’s flags, and not whether those flags give you access.If you don’t have
-readable, you can pipe the output offindthrough this trivial Perl script which only outputs the file names of the supplied files that are readable:e.g.
but note that this will still generate output errors on
stderrasfindattempts to recurse into directories that it doesn’t have permission for. Theprint0option tofind(andxargs -0) is there to make sure that the system works on file names with embedded spaces in them.