Linux Find Command Examples with perm option for Beginners

Find command is used to search files on basis of criteria. Using find command we can search files on basis of file size, file name, if they are readable, writable or executable and many more. So using find we can search exactly what kind of file we want to search.

Syntax of find command:

# find  {path or location where we want to search}   options   {what argument we want to search}

Such as:

Find the files which has 5M size present in /root directory.

[root@server ~]# find /root -size 5M

Find all the files whose size is more than 5 MB, then use the below example

[root@server ~]# find /root -size +5M

Find all readable files in /root

[root@server ~]# find  /root  -readable

Find command can be used with different options like – name, -user, -group etc. (See “man find” to learn about all options and their use). Here , we would learn perm the option in detail that is used with the find command.

Note: If we do not provide path or location to search then find command will start searching in present working directory and its sub-directory.

Important: User should have read and execute permission on the directory on which they are using “find command”.

-perm option: It is used to search the files with the required permission passed in the argument. Like if we want to search for all the files which have exact permission 777 in /root directory.

[root@server ~]# find /root/ -perm 777
/root/find1/ab3
/root/find_examples/file3
[root@server ~]#

So according to the output: There is file name as ab3 in /root/find1 which has exact permission 777.

Now let say our requirement changes. Which are as follows:

  • Find file with exact 777 permission (owner( r,w,x) , group(r,w,x) and other(r,w,x) )permission. Its means find all files in which owner , group and other have all 3 rights i.e.(read(r) – 4 , write(w) – 2 , execute (x)  – 1).
  • Find file with which has permission 741 or more than 741 i.e. find file which has at least 7 (rwx all three permissions for owner, must have at least read (4) but can have write and execute permission for group ,and must have minimum execute (1) but can have write and read permission for other).  For this case,  we use “–“ in front of permission. Like, find /root  -perm -741
  • Find file which has at least any one permission among 741 anywhere. That means 7(all 3 permissions anywhere among owner, group or other) or 4(read permission anywhere among owner, group or other or execute(1) permission anywhere among owner, group or other). If anyone of the criteria fulfills then that will be output. For this case,  we use “ /” in front of permission. Like, find /root  -perm  /741

In short :

  • -perm checks permission bit by bit.
  • 741 : it will search for files with exact 741 permission in each bit, nor more no less.
  • -741:it will search with 7 bit present in owner, minimum 4 ( minimum with read  permission but can have w or x) bit present in group  ,  minimum 1(execute) but can have additional permission for others
  • /741: Any one of the permissions among 7 or 4 or 1 present anywhere among owner, group or others.

Let’s understand by examples:

  • I have created directory named as find _examples in /root.
  • Creating files name as file1, file2 , file3, file 4 under /root/find_examples with different permissions
[root@server ~]# cd /root/find_examples/
[root@server find_examples]# touch file1
[root@server find_examples]# chmod 741 file1
[root@server find_examples]# touch file2
[root@server find_examples]# chmod 540 file2
[root@server find_examples]# touch file3
[root@server find_examples]# chmod 777 file3
[root@server find_examples]# touch file4
[root@server find_examples]# chmod 400 file4
root@server find_examples]#

Permission of file1 is 741.

Permission of file2 is 540.

Permission of file3 is 777.

Permission of file4 is 400.

Note: If you need output of find command in more human readable format, then we should use -exec option, examples is shown below

# find /root/find_examples/ -perm 741 -exec ls -lah { } \;

Now let’s run and see all 3 options mentioned above:

  • Search for exact match 741
[root@server ~]# find /root/find_examples/ -perm 741
/root/find_examples/file1
[root@server ~]#
  • Search for minimum 741 or more for each one i.e owner should have minimum 7 permission, same way group has minimum 4(read) permission and other with minimum execute permission. For this, we will use “-“
[root@server ~]# find /root/find_examples/ -perm -741
/root/find_examples/
/root/find_examples/file1
/root/find_examples/file3
[root@server ~]#

The output is file1 (which has exact permission 741) and file3( which has permission 777).

  • Search for files which have permission 741 anywhere i.e. if the file has permission 7 or 4 or 1 anywhere in owner, group or in others.
[root@server ~]# find /root/find_examples/ -perm /741
/root/find_examples/
/root/find_examples/file1
/root/find_examples/file2
/root/find_examples/file3
/root/find_examples/file4
[root@server ~]#

output is file1(741),file2(540),file3(777),file4(400).

So in perm option, you can pass argument with exact permission or using – or / in front of permission. Depending upon your requirements

Read Also : How to Add Local User to Sudo Group in Debian Linux

Leave a Comment

5 × 3 =