Are you trying to locate files with specific file permissions for different purposes, such as security auditing? Fortunately, the find command provides a handy “-perm”
flag which enables users to list all the files that match the provided file permissions.
However, this blog post assumes you are already familiar with file permissions and how to check or grant them. If that’s not the case, refer to our beginner’s guide on changing file permissions.
Now that you’re prepared and have an understanding of file permissions, this guide will help you search for your desired files based on their permissions, using the well-known “find” and “ls” commands.
Find Files Based on Permissions in Linux
The syntax of the find command for locating files based on their permission is stated below:
$ find [path] -type f -perm [permissions]
Here’s what each part of the syntax means:
[path]
– It states the directory or path from where you want to begin your search. For instance, to look for the file in the root directory use“/”
.-type f
– This option filters the search results to only include regular files, excluding other types of files.-perm [permissions]
– This option specifies the permission mode you intend to find. You can add Minus(-)
or Slash(/)
prefixes before the permission mode, or no prefix at all.
Let’s quickly review permission prefixes before delving into examples for further clarity.
- No prefix – exact permissions.
- Minus
(-)
prefix – At least specified permissions, with extras allowed. - Slash
(/)
prefix – At least any category (owner/group/others) must have specified permission bit(s).
1. Find Files That Have Specific Permissions
In this example, we will look for files in the “UbuntuMint” directory that have exactly read and write permissions for the owner only “600” by executing the command stated below.
Before executing the following command, you can run “ls -l ~/UbuntuMint” to review the permissions of files within this directory:
$ ls -l ~/UbuntuMint $ find ~/UbuntuMint -type f -perm 600
You can notice that the above command has returned the “file4.txt” which exactly meets the specified criteria.
2. Find Files That Have Executable Permissions
If you want to search files with executable permissions for their owner, group, and other users, simply execute.
$ find ~/UbuntuMint -type f -perm 111
Upon executing this command, you’ll notice files that precisely match the specified executable permissions.
3. Find Files That Have Read/Write Permissions
Before delving into this example, let’s use the “ls” command within the directory to inspect current file permissions. Here, you’ll observe two files with read and write (6) permissions for the file owner, and read permissions (4) for the group and other users.
In this scenario, the find command will enumerate multiple files with “644” permission:
$ ls -l ~/UbuntuMint $ find ~/UbuntuMint -type f -perm 644
4. Find Files That Have Owner’s Read/Write Permissions
Let’s use the “-”
minus prefix before the file permissions to list all the files that possess at least read and write permissions for the file owner.
So basically the command mentioned below will return files with permissions like “600”, 601, 602, 610, 620, 630, and so on:
$ find ~/UbuntuMint -type f -perm -600
5. Find Files with Any Category Meeting Specified Permissions
Next, let’s explore the working of the slash (/)
prefix before file permissions by executing the provided command. This will retrieve files within the “UbuntuMint” directory where at least one of the categories (owner, group, or others) meets the specified permission bits.
$ find ~/UbuntuMint -type f -perm /600
6. Find Files That Have Symbolic Permissions
Users can opt for symbolic permissions instead of the numerical mode to specify file permissions. Execute the command below to find files with read and write permissions for the owner and read permissions for the group and others within the “UbuntuMint” directory:
$ find ~/UbuntuMint -type f -perm u=rw,g=r,o=r
This command is equivalent to the above command:
$ find ~/UbuntuMint -type f -perm u+rw,g+r,o+r
Note: Prefixes can be utilized with symbolic permissions as well.
Now that you have learned the usage of the find command with the “-perm”
flag, let’s explore another command that combines both the “ls” and “grep” commands.
Find Files Based on Permissions Using the ‘ls’ and ‘grep’ Commands
You can even utilize the powerful combination of the ls command piped with the grep command to display all files in the current directory and subsequently filter out files that match the specified permission.
Note: The “-l”
flag in the “ls” command helps in displaying detailed information about files including file permissions, ownership, size, modification time, and more.
Let’s run the command mentioned below to look for files that have read and write permissions for the file owner. Here in this command, the “^”
symbol specifies that the grep command will only filter files not directories:
$ ls -l | grep "^-rw-------"
However, if you want to focus on only permissions for a specific user like file owner and don’t care about permission of other user classes, you can define the permissions for that particular user bits and employ a wildcard character for the rest, as shown below:
$ ls -l ~/UbuntuMint/ | grep "^-rw-*"
This command effectively identifies all files within the “~/UbuntuMint/” directory that possess read and write permissions for the file owner.
Conclusion
If you’re seeking to identify files within a directory based on specific file permissions for security audits or other purposes, this guide is your essential resource. It offered two distinct commands, accompanied by numerous examples, to assist you in pinpointing files according to their permissions.