Ubuntu – How to search for files with a specific permission

12.04search

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.

Best Answer

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 <specify location> -type f -perm -444

For files belonging to a particular user you could use

find <location> -type f -user mike

For files executable (for all) you could use

find <location> -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 <location> -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 <location> -type f -perm 744 -o -perm 666

Directories can be searched for with -type d.

See man find for the other available permutations.

Related Question