- Check if different user has read/write permissions to a file on linux
- Test Read Permission
- Test Write Permission
- Test As Specific User
- How to Check Folder Permission Linux
- Prerequisites
- Users and User Groups
- Checking File and Folder Permissions
- Checking File Permission Using GUI
- Checking File Permission Using CLI
- Modifying File and Folder Permissions
- Changing File Ownership
- Final Thoughts
- About the author
- Sidratul Muntaha
- How to check permissions of a specific directory?
- 10 Answers 10
- For Dir
- For file
- How do you view file permissions?
- 4 Answers 4
Check if different user has read/write permissions to a file on linux
will return 0 if otheruser can read the file, or 1 if otheruser cannot read the file. You can run test -r /path/to/file; echo «$?» to view the return code of the test command.
Use test -w to test for write permission and test -x to test for execute permission.
Test Read Permission
Attempt to read the beginning of the file and discard the normal output. You can then look for an empty string (success) or a «Permission denied» message (you can also check for other error messages such as «No such file or directory»). For example:
head -1 /path/to/file 2>&1 > /dev/null | grep 'Permission denied'
Test Write Permission
Use the touch command with the -c (—no-create) option. Combine stdout and stderr and again search for an empty string (success) or an error:
touch -c /path/to/file 2>&1 | grep 'Permission denied'
If you’re explicitly testing write access of a directory, be sure to test the directory and not a file contained within, since with the -c option, there’s no error condition if the file doesn’t exist even in a directory you don’t have write access to:
-c, if the file does not exist, do not create it and do not report this condition
Test As Specific User
The final piece of the puzzle is how to check this as a different user. As root execute the test command as the desired user with «sudo -u [username] [command]» so using your suggested user:
sudo -u apache touch -c /path/to/file 2>&1
How to Check Folder Permission Linux
Linux is a multi-user operating system. It can handle multiple users accessing the system at the same time. This is one of the key reasons why Linux is so popular in mainframes, servers, supercomputers, etc. Being a multi-user system, it’s paramount to have functional user permission management. It’s the fundamental that defines what user/group can access what part of the system.
For file access, this user permission is managed as the file permissions. In this guide, we’ll have a deep dive into the file and folder permissions in Linux.
Prerequisites
To understand the file and folder permissions, it’s important to understand several key concepts. In this section, we’ll describe each of them briefly.
Users and User Groups
Linux allows access to the systems to individual users. Any valid user can connect to the system locally or remotely and enjoy access to whatever resource is allocated.
If there are hundreds of users, then managing all their permissions individually is quite inefficient. To solve this, Linux introduces the concept of user groups. Each group may contain one or more users. Changing permissions for the user group will automatically be applied to every user within that group.
Checking File and Folder Permissions
Depending on the type of UI, there are two ways you can check the file permissions of a folder/directory.
Checking File Permission Using GUI
If your distro comes with a desktop environment, then it will also likely have a file manager installed. All the file managers can check and show the file permissions. For demonstration, let’s have a look at Nautilus, the default file manager in GNOME.
Right-click on any file/folder, and select “Properties”.
To check the permissions, head to the “Permission” tab.
Checking File Permission Using CLI
This is the more recommended (albeit slightly confusing) method of checking file permissions in Linux. We’ll be using the ls tool that comes preinstalled in all Linux distros.
To check the file permissions, run the following ls command. Check out this guide for an in-depth exploration of the ls command.
If no file location is specified, then ls will print info about all the files in the current directory. Note that ls may not print the file permissions and info if it’s a directory.
For example, when trying to check permissions for the directory “~/Desktop”, ls won’t print any info at all.
In such a situation, run ls on the parent directory of the target directory. In this case, it’s “~/”.
Explaining the file permissions
As we can see, the file permission is described as a string of some ASCII characters. This guide on Linux file permissions explains everything in more detail. Here, we’ll have a brief overview.
There are 3 permission types.
- read (r) – The user’s capability to read the file content.
- write (w) – The user’s capability to write or modify the file or contents of the directory.
- execute (x)- The user’s capability to execute the file or see the contents of the directory.
The permission string contains the following data.
- Character 1: Describes the file type(directory, symlink, setuid/setgid permissions, or sticky bit permissions).
- Character 2-4: Describes the file permission for the owner.
- Character 5-7: Describes the group permissions for the file.
- Character 8-10: Describes the file permission for everyone else in the system.
For the file type, there are a couple of different values.
- _ : No special permissions.
- d : Directory
- l: Symbolic link, also known as symlinks. Learn more about symlinks in Linux.
- s: setuid/setgid permissions.
- t: Sticky bit permissions.
Modifying File and Folder Permissions
The file permissions are important values. However, with the right authority, a user in the Linux system can change the file permission.
To change the permissions, Linux comes with a built-in tool called chmod. For example, to mark a script file as an executable, we’d run the following command.
If you wanted to remove the “executable” flag from the file, then the command would look like this.
For a beginner, it’s recommended to practice various chmod commands, for example, granting a user permission to a folder. The next guide goes deeper into chmod with explanations.
Changing File Ownership
The owner of a file also impacts the file permissions. Generally, it’s the user that originally created the file. However, different users can be assigned as the owner of the file or directory.
To change the ownership of a file, Linux comes with the tool chown.
Final Thoughts
File permission is an important concept in Linux. Checking file permission for a folder is an easy task. Linux comes with the necessary tools to manage them directly from the command line. This guide successfully demonstrates checking and modifying the file permission of files and folders.
About the author
Sidratul Muntaha
Student of CSE. I love Linux and playing with tech and gadgets. I use both Ubuntu and Linux Mint.
How to check permissions of a specific directory?
I know that using ls -l «directory/directory/filename» tells me the permissions of a file. How do I do the same on a directory? I could obviously use ls -l on the directory higher in the hierarchy and then just scroll till I find it but it’s such a pain. If I use ls -l on the actual directory, it gives the permissions/information of the files inside of it, and not of the actual directory. I tried this in the terminal of both Mac OS X 10.5 and Linux (Ubuntu Gutsy Gibbon), and it’s the same result. Is there some sort of flag I should be using?
10 Answers 10
-d, --directory list directory entries instead of contents, and do not dereference symbolic links
You might be interested in manpages. That’s where all people in here get their nice answers from.
I think the man page is poorly worded. I scoured it five times before I started googling. I don’t want directory ‘entries’ (thing ‘entered’ into directories? Like their files and sub-directories?) nor their ‘contents’ (they sound like the same concept to me), I want the directories themselves.
it’s completely standard terminology, the directories themselves are the directory entries, i.e. entries in the filesystem
It may be standard terminology, but to someone who would likely be asking such a question, it is probably confusing jargon.
You can also use the stat command if you want detailed information on a file/directory. (I precise this as you say you are learning ^^)
— indicates the beginning of the command options.
l asks for a long list which includes the permissions.
d indicates that the list should concern the named directory itself; not its contents. If no directory name is given, the list output will pertain to the current directory.
In GNU/Linux, try to use ls , namei , getfacl , stat .
For Dir
[flying@lempstacker ~]$ ls -ldh /tmp drwxrwxrwt. 23 root root 4.0K Nov 8 15:41 /tmp [flying@lempstacker ~]$ namei -l /tmp f: /tmp dr-xr-xr-x root root / drwxrwxrwt root root tmp [flying@lempstacker ~]$ getfacl /tmp getfacl: Removing leading '/' from absolute path names # file: tmp # owner: root # group: root # flags: --t user::rwx group::rwx other::rwx [flying@lempstacker ~]$
[flying@lempstacker ~]$ stat -c "%a" /tmp 1777 [flying@lempstacker ~]$ stat -c "%n %a" /tmp /tmp 1777 [flying@lempstacker ~]$ stat -c "%A" /tmp drwxrwxrwt [flying@lempstacker ~]$ stat -c "%n %A" /tmp /tmp drwxrwxrwt [flying@lempstacker ~]$
For file
[flying@lempstacker ~]$ ls -lh /tmp/anaconda.log -rw-r--r-- 1 root root 0 Nov 8 08:31 /tmp/anaconda.log [flying@lempstacker ~]$ namei -l /tmp/anaconda.log f: /tmp/anaconda.log dr-xr-xr-x root root / drwxrwxrwt root root tmp -rw-r--r-- root root anaconda.log [flying@lempstacker ~]$ getfacl /tmp/anaconda.log getfacl: Removing leading '/' from absolute path names # file: tmp/anaconda.log # owner: root # group: root user::rw- group::r-- other::r-- [flying@lempstacker ~]$
[flying@lempstacker ~]$ stat -c "%a" /tmp/anaconda.log 644 [flying@lempstacker ~]$ stat -c "%n %a" /tmp/anaconda.log /tmp/anaconda.log 644 [flying@lempstacker ~]$ stat -c "%A" /tmp/anaconda.log -rw-r--r-- [flying@lempstacker ~]$ stat -c "%n %A" /tmp/anaconda.log /tmp/anaconda.log -rw-r--r-- [flying@lempstacker ~]$
How do you view file permissions?
I want to know how to see the permissions a particular file has. Which command should I type in the terminal? However, I don’t want to change it.
4 Answers 4
If you want to see the the permission of a file you can use ls -l /path/to/file command.
ls -l acroread -rwxr-xr-x 1 10490 floppy 17242 May 8 2013 acroread
What does this mean ?
First — represents a regular file. It gives you a hint of the type of object it is. It can have following values.
- d (directory)
- c (character device)
- l (symlink)
- p (named pipe)
- s (socket)
- b (block device)
- D (door)
- — (regular file)
r represents read permission.
w represents write permission and
x represents executable permission.
First combination of rwx represents permission for the owner .
Second combination of rwx represents permission for the group .
Third combination of rwx represents permission for the other of the file.
Octal notation
Permission of file can also be represented in octal notation.
In octal notation
Read or r is represented by 4,
Write or w is represented by 2
Execute x is represented by 1.
Sum of these three is use to represent the permission.
stat command can be used to view file permission in octal notation
stat -c "%a %n" acroread 755 acroread
For owner it is 4+2+1=7 (111 in binary)
For group it is 4+0+1=5 (101 in binary) and
For other it is 4+0+1=5 (101 in binary).