Solved

How to check permission of a file or path?

Posted on 2006-10-27
8
264 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
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.:

760 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now