Check write permissions linux

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 ~]$ 

Источник

Command to check whether the current user has write permission

I have to create a script to check if the current user has write permission on a file. If not, an error should appear. I wrote this:

if [namei -m /path/to/textfile.txt | grep w]; then echo "message" 

[ -w «/path/to/textfile.txt» ] || echo «/path/to/textfile.txt is not writable by $USER.» or change to && [ -w «/path/to/textfile.txt» ] && echo «/path/to/textfile.txt is writable by $USER.»

Читайте также:  Linux graphics card problems

1 Answer 1

Let’s work through the issues with your code. As dessert mentioned in a comment, the first problem is the syntax of the [ command. We need to put spaces between [ and its arguments, because the shell uses spaces to separate fields, and thinks that [namei is the command you want to run.

I’m going to use a file in my home directory called file , which I know I have write permission on, in my examples. I have put your code on one line, to make it easier to test interactively. It will work in the same way as if there were a newline after then .

$ if [namei -m playground | grep w]; then echo "writeable" No command '[namei' found, did you mean: Command 'namei' from package 'util-linux' (main) [namei: command not found 

Let’s fix the spaces around [ and ] :

$ if [ namei -m playground | grep w ]; then echo "writeable" > 

Now I get a prompt to type something, a > . The prompt tells me that the shell is waiting for me to enter something. The simplest form of the syntax of if is

if condition; then command; fi

so the shell is waiting for the fi . If I enter it at the prompt, I get this:

$ if [ namei -m file | grep w ]; then echo "writeable" > fi bash: [: missing `]' grep: ]: No such file or directory 

Looks like the pipe | is causing problems here, since [ can’t find its last argument, and grep thinks its last argument is ] . Since [ (or the one we’re using here anyway) is a shell builtin, we should be able to get some information about it using help

$ help [ [: [ arg. ] Evaluate conditional expression. This is a synonym for the "test" builtin, but the last argument must be a literal `]', to match the opening `['. 

We can’t use a pipe in the middle of a command’s arguments. In other situations, we might be able to put the commands in a subshell by enclosing them in ( ) , but that is a syntax error in [ (and test ).

The real problem here is that what we are trying to pass to the [ command is not something it can evaluate as true or false. If the argument you try to pass to test is a command, test does not use the exit status of that command as I think you are expecting. You can pass the output of a command to [ using command substitution, but that is not what you or your code want(s) to do.

I think what your code is trying to do is check that grep found a w in the output of namei -m somefile . We don’t need the test command for that at all.

$ help if if: if COMMANDS; then COMMANDS; [ elif COMMANDS; then COMMANDS; ]. [ else COMMANDS; ] fi Execute commands based on conditional. The `if COMMANDS' list is executed. If its exit status is zero, then the `then COMMANDS' list is executed. Otherwise, each `elif COMMANDS' list is executed in turn, and if its exit status is zero, the corresponding `then COMMANDS' list is executed and the if command completes. Otherwise, the `else COMMANDS' list is executed, if present. The exit status of the entire construct is the exit status of the last command executed, or zero if no condition tested true. 

So if we are only interested in whether a command succeeds, we can put the command as a condition for if :

$ if namei -m file | grep w; then echo "writeable"; fi -rw-rw-r-- file writeable 

You can even use -q to suppress output from grep and collect only its exit status (success if a match is found):

$ if namei -m file | grep -q w; then echo "writeable"; fi writeable 

But that’s not a good way to test whether the file is writeable by you. Let’s try another test file:

$ touch file_with_w $ chmod 444 file_with_w $ stat -c %A file_with_w -r--r--r-- $ if namei -m file_with_w | grep -q w; then echo "writeable"; fi writeable 

If we parse the output of namei -m for a file with a w in its name, this method will make it appear to be writeable even if it is read only for everyone.

Читайте также:  Горячее резервирование linux серверов

Parsing the output of commands is sometimes the right way to get the information you want. But there is usually a better way, in the form of an option to that command, or a different command with a similar purpose.

Let’s go back to test aka [ command and see if it has any useful options. You can enter help test , or for more detail read the Conditional Expressions section of the Bash manual

File operators: . -w FILE True if the file is writable by you. 

Aha! the test command has exactly what we want, as mentioned in answer to Checking for a file and whether it is readable and writable

$ if [ -w file ]; then echo "writeable"; fi writeable $ if [ -w file_with_w ]; then echo "writeable"; fi $ 

But you mentioned that you wanted the command to output an error message if the file is not writeable, rather than a statement that it is writeable. For that we need an else statement

$ if [ -w file_with_w ]; then echo "writeable"; else echo "not writeable"; fi not writeable 

As a script, you could write that command like this:

#!/bin/bash if [ -w "$1" ]; then echo "writeable" else echo "not writeable" fi 

Then you could give it execute permission (replace script with the actual filename)

and run it with the file you want to test as its argument (referenced in the script with $1 ):

$ ./script file writeable $ ./script file_with_w not writeable 

If you only want to get an error message when the file is non-writeable, and no output when it is, you can negate the test condition instead of writing an else:

$ if [ ! -w file_with_w ]; then echo "not writeable"; fi not writeable 

As Panther mentioned in a comment, you might also want to make use of the variable $USER , which should expand to the name of the user running the shell. You can thus make a more impressive error message:

$ if [ ! -w file_with_w ]; then echo "file is not writeable by $USER"; fi file is not writeable by zanna 

For simple test commands, you can also use [ and test and [[ without if , and use shell operators for the logic:

$ [ -w file_with_w ] || echo "not writeable" not writeable 

|| is the shell’s logical OR. It means if the previous command failed, then execute the next command

The shell’s logical AND is && :

$ [ ! -w file_with_w ] && echo "not writeable" not writeable 

&& means if the previous command succeeded, then execute the next command

All of this works because the test command gives a helpful exit status as described in help test :

Exit Status: Returns success if EXPR evaluates to true; fails if EXPR evaluates to false or an invalid argument is given. 

Источник

Читайте также:  Мониторинг температуры процессора линукс

How to Check File Permissions in Ubuntu

Sagar Sharma

Check file permission in Ubuntu

In that case, the chances of you were not allowed to access the file are pretty high.

The usual way to see the file permission is to use the long listing option with ls command:

But you need to understand the concept of file permission and ownership to actually understand its meaning.

use ls command in ubuntu to check file permission

Another way check the file permission in ubuntu is to use the getfacl utility:

how to check file permission in Ubuntu

There are other methods to check file permissions but before that, let’s understand file permission in Linux.

Understanding file permissions in Ubuntu

Linux by its nature gives the ability to have multiple users in a single system and that came with the threat as any other user can access your sensitive documents.

And this is where the file permissions come to save you.

So there are 3 types of ownership in Linux:

While there are 3 types of permission in Linux:

For better understanding, let’s take an example of the ls command:

use ls command in ubuntu to check file permission

So you get a sequence of permission but how you are supposed to know which is for the user and which is for group users? Well, here’s the illustration:

File permissions in linux explained

So now you know the basics, let’s have a look at multiple methods by which you can check file permissions.

Check file permission in Ubuntu

In this guide, I’m going to share 3 ways by which you can check for file permissions in Linux. So let’s start with the widely used one.

Using the ls command

The ls command is generally used to list files in a specific directory but you can also use it to get additional info such as file permissions.

To check file permissions using, you’d need to pair the ls command with the -l option.

how to check file permissions in ubuntu using the ls command

Similarly, you can also use -a option to show hidden files:

how to list hidden files in ubuntu using ls command

And if you want to know what each element in the above output stands for, I’d recommend checking the other guide of ours:

Using stat command

While the ls command gave me the list of files with read-write permission attached to each one, the stat command requires a file name as it gets even more details than the ls command.

To list file details, you just have to pair the filename with the stat command:

use stat command to get file details in ubuntu

But if you are someone like me who prefers only necessary details, the stat command allows users to modify the output with the —format option:

stat --format "R/W/E permissions: %A, Group owner: %G, User owner: %U" a.sh

use stat command with variables to get specific info in ubuntu

Here, I’ve used 3 variables with text attached to them making them more relevant:

  • %A shows file permission.
  • %G gets the name of the group owner.
  • %U will show the user owner.

Using the getfacl utility

This is my favorite utility when it comes to checking file permissions in Linux. And it comes pre-installed in Ubuntu too!

The reason why it is my favorite is that it only shows the necessary details and conveniently presents details.

Pair the filename with the getfacl utility and that’s it:

use getfacl command to check file permissions in Ubuntu

Wrapping Up

This was my take on how you can check file permissions in Ubuntu with some basics. And if you want to know how change the permission, use the chmod command.

Источник

Оцените статью
Adblock
detector