Tuesday, June 27, 2017

12.04 - How to search for files with a specific permission



How can I search for files that have a specific permission. For example, I have 10000 files and I want to find the ones that have the READ ONLY flag. In another case I want to search for another that has a particular owner. Or in another to see the files that are READ ONLY and EXECUTABLE.



It's probably easiest to use the find command, which allows you to recursively search through the directory tree. For example, if you particularly wanted to find files that were read-only, you could type




find  -type f -perm -444


For files belonging to a particular user you could use



find  -type f -user mike


For files executable (for all) you could use




find  -type f -perm -777


For those that are executable and read-only for all you would use 555 in place of 777 in the example above. You can also search for files that belong to a group by substituting -user mike for -group mike.



To negate the search terms and so search for the exact opposite, you can use an exclamation mark like this:



find  -type f ! -perm -444 



Note: Specifying a dash before the permissions (e.g. -perm -444) means that all files that have a read only flag will be found and not just those that are 444; to search for 444 exactly and only that, simply remove the dash (e.g. -perm 444).



Note2: Combinations of permissions can be sought as well using -a for and and -o for or; for example to find exactly these permissions, type:



find  -type f -perm 744 -o -perm 666


Directories can be searched for with -type d.




See man find for the other available permutations.


No comments:

Post a Comment

11.10 - Can't boot from USB after installing Ubuntu

I bought a Samsung series 5 notebook and a very strange thing happened: I installed Ubuntu 11.10 from a usb pen drive but when I restarted (...