?
Solved

How to check permission of a file or path?

Posted on 2006-10-27
8
Medium Priority
?
271 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 336 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 332 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 332 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

How many times have you wanted to quickly do the same thing to a list but found yourself typing it again and again? I first figured out a small time saver with the up arrow to recall the last command but that can only get you so far if you have a bi…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
Suggested Courses
Course of the Month11 days, 1 hour left to enroll

770 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