Solved

How to check permission of a file or path?

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to have a cron job run until a condition is met 12 64
centos linux 65 125
open source backup solution 1 28
how to configure linux OS using Ubuntu 7 37
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
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 navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

786 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