Command example with perm option for Linux beginners

Search commands are used to search for files based on criteria. You can use the find command to find files based on file size, file name, readable, writable, input, or executable, and so on. Therefore, you can use find to find the exact type of file you are looking for.

find command syntax:

#Find {path or location to search} options {argument to search}

such as:

Locate the 5M size file in the / root directory.

[[email protected] ~]# find /root -size 5M

Find all files over 5 MB in size and use the example below

[[email protected] ~]# find /root -size +5M

Find all readable files at / root

[[email protected] ~]# find  /root  -readable

The Find command can be used with various options, such as -name, -user, and -group (see “man find” for all options and their use). This section details the options used in the find command.

Note: If you do not specify a search path or location, the find command starts searching in the current working directory and its subdirectories.

important: The user must have read and execute permissions on the directory using the “find command”.

-perm option: Used to search for files with the required permissions passed in the arguments. Like searching for all files with the exact permission 777 in the / root directory.

[[email protected] ~]# find /root/ -perm 777
/root/find1/ab3
/root/find_examples/file3
[[email protected] ~]#

So, according to the output, / root / find1 has a file name of ab3 and the exact permission is 777.

Now suppose the requirements have changed. It is as follows.

  • Search for files with exact 777 permissions (owner (r, w, x), group (r, w, x) and other (r, w, x)). That is, find all files for which owner, group, and other have all three permissions (read (r) -4, write (w) -2, execute (x) -1).
  • Search for files with permission 741 or more than 741. That is, search for files with at least 7 (requires all three rwx permissions of the owner, at least read (4), but has write and execute permissions for the group and requires minimal execution) ( 1) can have write and read permission for other users. In this case, use “–” before the permission. favorite, Find / root-perm-741
  • Search for files with at least one of the 741 permissions anywhere. That is, 7 (all three permissions in owner, group or everywhere else) or 4 (read permission in owner, group or everywhere else, or owner, group or all others Execute (1) permission at the location). If any of the criteria are met, it is output. In this case, use “/” before the permission. favorite, find / root find-perm / 741

in short :

  • -perm checks permissions bit by bit.
  • 741: Search for files with 741 permissions in each bit.
  • -741: 7 bits for owner, minimum 4 (can have minimum, w or x with read permission) bits present in group, search with minimum 1 (execute), but add to others Can have permission
  • / 741: One of 7, 4, 1 exists in any of owner, group, or other user.

Let’s understand with an example:

  • Created a directory named find_examples in / root.
  • Create with different permissions under / root / find_examples, with file names file1, file2, file3, file4
[[email protected] ~]# cd /root/find_examples/
[[email protected] find_examples]# touch file1
[[email protected] find_examples]# chmod 741 file1
[[email protected] find_examples]# touch file2
[[email protected] find_examples]# chmod 540 file2
[[email protected] find_examples]# touch file3
[[email protected] find_examples]# chmod 777 file3
[[email protected] find_examples]# touch file4
[[email protected] find_examples]# chmod 400 file4
[email protected] find_examples]#

file1 has 741 permissions.

The permission of file2 is 540.

The permission for file3 is 777.

The permission for file4 is 400.

Note: If you need the output of the find command in a more human-readable format, you must use the -exec option. An example is shown below.

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

Next, run and confirm all three options.

  • Exact Match Search 741
[[email protected] ~]# find /root/find_examples/ -perm 741
/root/find_examples/file1
[[email protected] ~]#
  • A minimum of 741 searches per group is required, meaning the owner needs a minimum of 7 permissions. In the same way, groups have a minimum of 4 (read) permissions, others require a minimum of execute permissions. Use “-” for this
[[email protected] ~]# find /root/find_examples/ -perm -741
/root/find_examples/
/root/find_examples/file1
/root/find_examples/file3
[[email protected] ~]#

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

  • Search for files with permission 741 everywhere. That is, if the file has permission 7 or 4 or 1 in the owner, group, or elsewhere.
[[email protected] ~]# 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
[[email protected] ~]#

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

Therefore, in the perm option, you can either pass arguments with the correct permissions, or use-or / before the permissions. According to your requirements

.

Sidebar