How to check if a file is not readable by anyone except the owner?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to check if a file is not readable by anyone except the owner?
# 1  
Old 04-28-2010
How to check if a file is not readable by anyone except the owner?

All,

I have a script where I get a filename as input and do some processing with the file that I got as input.

Requirement:
Now I have a requirement where I need to check the following:

Quote:
1. Whether the file is created by the $USER (who is running this script) AND
2. The file has permissions set in such a way that only the $USER will have read permissions to that passed file AND
3. All others (group/world) should NOT have ANY permissions.
If either of this goes wrong, the script should pop out a warning message.

I tried searching the internet but did not get an answer. Any help is appreciated.

Below is the sample code:

Code:
#! /bin/ksh

echo "Enter the File : \c"
read filename

if [ -f $filename ]; then
  Check the owner AND Permissions of the file ----> Need to code this!!!
  if [ permissions are OK ]; then ----> i.e. permissions is as mentioned above
    do processing
  else
    echo "ERROR"
  fi
fi

# 2  
Old 04-28-2010
Code:
OWNER=`ls -l file | awk '{print $3}'`
PERMS=`ls -l | awk '{print $1}' `
if [[ $OWNER = $USER && `echo $PERMS | grep '-r..------'` != "" ]]
then
your code
fi

# 3  
Old 04-28-2010
Code:
#!/bin/sh

echo -n "File: "
read file

if [ -f $file ]; then

  if [ `ls -l $file | awk '{print $3'}` = "$USER" ] && \
     [ `ls -l $file | cut -c2` = "r" ] && \
     [ `ls -l $file | cut -c5-10` = "------" ] ; then

  echo "Owner of $file is YOU and you can read it."
  echo "Group and Other can't read, write or execute it."

  else

  echo "ERROR"
  exit

  fi

else

echo "$file does not exist."

fi

# 4  
Old 04-28-2010
To quote a Larry Wall:

There is more than one way to do it.
# 5  
Old 04-28-2010
Absolutely
# 6  
Old 04-29-2010
Thanks for your replies! I will try them and let you know.

Last edited by bharath.gct; 04-29-2010 at 12:31 AM..
# 7  
Old 04-29-2010
If changing the file's mode is not a problem, a simple chmod can ensure that all of your conditions are met:

Code:
if ! chmod og-rwx "$file"; then
    echo "ERROR"
    exit 1
fi

... do processing ...

Caveat: If root may run this script, and if the file owner must stricly match the user running the script, then a check for ownerships or a chown needs to be added, since root can chmod any other user's files. For all other unprivileged users, the single chmod is enough to verify ownership as well as permissions.

An unportable alternative, if you system has one, is the stat command.

Regards,
Alister

Last edited by alister; 04-29-2010 at 12:23 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How do I make this file readable/printable?

When I do the file I get ELF 32-bit MSB executable SPARC Version 1, dynamically linked, not stripped I am almost 100% sure I was able to print a readable version of this file in the past but I cannot remember how. Is it possible to convert this file into something that can be read and or... (3 Replies)
Discussion started by: fsanchez
3 Replies

2. Solaris

Privileges : modify dir/file owner by other that's not owner

i need to do the following operations in solaris 10: 1.change owner and group owner for files which are not owned by the current user and user group 2.to can delete files in the /tmp directory which are not of the current user 3. allow to a standard user the deletion of files in the /tmp... (1 Reply)
Discussion started by: sirmark
1 Replies

3. Programming

writing to file is not readable by user

In the following code segment I write to some file using , but this write is not readable by me when i open the file. any helps would be thankful. #include<stdio.h> #include<stdlib.h> #include<unistd.h> #include<sys/types.h> #include<sys/stat.h> #include<errno.h> #include<fcntl.h>... (6 Replies)
Discussion started by: saman_glorious
6 Replies

4. Shell Programming and Scripting

check if file is readable by others

hi all, in ksh script how do i detect if a file is readable by others ?? thanks. (6 Replies)
Discussion started by: cesarNZ
6 Replies

5. Shell Programming and Scripting

check whether file is readable or not in ksh

i want to check the readability of a file inside the script. when i use if then echo the file "$sourcef" is not readable else echo something fi i am getting the error : f: unknown test operator when i tried to check the availability with if i was... (3 Replies)
Discussion started by: gotam
3 Replies

6. UNIX for Advanced & Expert Users

How UNIX admin set up this? how files of 744 of other owner can be removed by another owner?

Hi all, We have some files are under 744 permissions and the the owner is say owner1 and group1. Now we have another user owner2 of group2, owner2 can remove files of the owner1 and the permission of those files are 744, unix admin told us he did some config at his side so we can do that. ... (14 Replies)
Discussion started by: TheGunMan
14 Replies

7. SuSE

Regarding Readable check for all the files in the folder

Currently we are doing the migration to unix to linux. I am facing the new problem kganeshb@its04489:~/scripts $ ls -l | more total 340 -rw-r----- 1 kganeshb users 9038 Oct 22 13:23 109_db.txt -rw-rw---- 1 dlc users 1413 Oct 10 17:40 1.txt -rw-rw---- 1 kganeshb users 45 Jan 28 13:46 a... (2 Replies)
Discussion started by: kingganesh04
2 Replies

8. HP-UX

file in malibox is readable format?

Hi, Files coming to mailbox are in readable format? Is there any special command to read these files. suppose i have sent a file like this megh$mailx -s "mesg" xyz@server.domain<file1.dat can xyz directly read the file from his mailbox? (1 Reply)
Discussion started by: megh
1 Replies

9. Shell Programming and Scripting

Shell script that would check if the CD-rom is readable

Hi, I was wondering if there's a command in Linux that would check to see if a CD is inserted into CD-rom. I am developing a shell script that would copy a file from CD rom. For this, I have to first verify if the CD has been inserted or not. Can anyone help me? Thanks, Sundeep (1 Reply)
Discussion started by: eamani_sun
1 Replies

10. Solaris

Owner of file gets 'not owner' error for chgrp

Hi Folks, I know that changing users and groups is pretty basic admin, but this one has got me stumped. When I try to change the group of a file for which I am the owner for, it still gives me a 'Not owner' error. For example, when I am logged in as 'webadmin', I have the following file: ... (4 Replies)
Discussion started by: brizrobbo
4 Replies
Login or Register to Ask a Question