Solved

How to check permission of a file or path?

Posted on 2006-10-27
8
268 Views
Last Modified: 2010-04-20

Hello,

I know this is possible by "stat" command or  "ls"  command but I'm looking for a command to return me if $USER (current user) has  privilege or permission to a specific /dir1/dir2/a_file

1) Let's say user1 runs script1
2) user1 has access to some paths and no access to some others
3) I need a command to return a  true/false value or anything to check it in my script befoer following a procedure.

Thanks
0
Comment
Question by:akohan
8 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 17823825
perl -e 'print 0+-r "/dir1/dir2/a_file"'
perl -e 'print 0+-w "/dir1/dir2/a_file"'
perl -e 'print 0+-x "/dir1/dir2/a_file"'
0
 

Author Comment

by:akohan
ID: 17824423

Thanks you so much but it has to be in bash.

Thanks again for your resonse.
AkOHAN

0
 
LVL 84

Accepted Solution

by:
ozo earned 84 total points
ID: 17824443
#!/bin/bash
READ=`perl -e 'print 0+-r "/dir1/dir2/a_file"'`
WRITE=`perl -e 'print 0+-w "/dir1/dir2/a_file"'`
EXECUTE=`perl -e 'print 0+-x "/dir1/dir2/a_file"'`
0
 
LVL 58

Assisted Solution

by:amit_g
amit_g earned 83 total points
ID: 17827918
stat -c%a TheFile

would return you the octal access rights for example 644 but that might need more tests. You could do

if [ -r TheFile ]
then
    echo file exists and is readable.
fi

if [ -w TheFile ]
then
    echo file exists and is writable.
fi

if [ -x TheFile ]
then
    echo file exists and is executable.
fi

if [ -d TheFile ]
then
    echo file exists and is a directory.
fi

if [ -d TheFile -a -r TheFile ]
then
    echo file exists and is a directory and is redable.
fi

if [ -d TheFile -a -w TheFile ]
then
    echo file exists and is a directory and is writable.
fi

if [ -d TheFile -a -x TheFile ]
then
    echo file exists and is a directory and is executable i.e. can do cd to it.
fi

0
 
LVL 23

Assisted Solution

by:Mysidia
Mysidia earned 83 total points
ID: 17829498
If  Kpathsea's installed, there is an access command...

if  [ access -rw /path/to/file ] ; do
  blah blah
done

Assuming 'access' is in PATH, usually it's in some weird place like /usr/share/texmf/bin/access
The access command uses the access() system call, shell test checks permission bits.

The difference matters if the filesystem is mounted read-only.

if [ -w TheFile ] ; then
    echo file exists and is writable.
fi

Will report that the file is writable, if you match write permissions, even if the filesystem the file sits on happens to be read-only (and writing would be denied based on that).
0

Featured Post

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Using 'screen' for session sharing, The Simple Edition Step 1: user starts session with command: screen Step 2: other user (logged in with same user account) connects with command: screen -x Done. Both users are connected to the same CLI sessio…
Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question