Solved

How to check permission of a file or path?

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

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

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

rdate is a Linux command and the network time protocol for immediate date and time setup from another machine. The clocks are synchronized by entering rdate with the -s switch (command without switch just checks the time but does not set anything). …
SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
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…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

717 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