Script to check files ownership


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to check files ownership
# 8  
Old 01-27-2014
You can omit the group here, or omit the user by setting the 2nd argument to ""
Code:
#!/bin/sh
set -f
startdir=$1
owner=$2
group=$3
[ -n "$startdir" ] && [ -n "$owner" -o -n "$group" ] || exec echo 'need arguments: startdir [owner] [group]'
ownerexpr=${owner:+-user\ $owner}
groupexpr=${group:+-group\ $group}
find "$startdir" \! \( $ownerexpr $groupexpr \) -ls | awk '{print $3, $5, $6, $11}'


Last edited by MadeInGermany; 01-28-2014 at 08:57 AM.. Reason: space escaped in :+ modifier
This User Gave Thanks to MadeInGermany For This Post:
# 9  
Old 01-28-2014
Thanks Again but am getting error when i run the script.

Code:
root@ttky # ./checkfileownership.sh /root/tools/bin root root
./checkfileownership.sh: root}: not found
./checkfileownership.sh: root}: not found

am doing something wrong here, I passed path, owner and group as arguments to the script.

Scripts first needs to look/read into config file, in which there will be some exception files and it's owner/group info.

So, when script start searching the files from mentioned path, if the same file found which was there in config file, it should skip that file and continue/proceed to next file..

say, we have config file by name ownerconfig.cnf and it has some file information which needs to skipped for checking owner/group

Code:
filename  owner  group
abc.txt    root    tadmin
chperf.sh rtadmin rtadmin

now, in our main script, first we need to read this ownerconfig.cnf file and then goto the path and keep checking the files for it's owner/group against mention owner/group which was passed as arguments during script running.

If it finds same file say abc.txt in the path, then all it has to do is skip the checking of this file(as it's exceptional file which needn't to be checked) and proceed with other file in the mention path.
# 10  
Old 01-28-2014
Sorry, /bin/sh needs the space character escaped in the :+ modifier.
I have changed that in my script.
# 11  
Old 01-28-2014
Thanks, Now i don't see any error after adding \ in the script.

Can you please tell how can we add a logic to skip finding ownership of the specific files by ready config file ?
# 12  
Old 01-28-2014
What have you tried?

You cannot simply keep asking us to write every bit of your script for you piece by piece...

Last edited by Corona688; 01-28-2014 at 11:40 AM..
This User Gave Thanks to Corona688 For This Post:
# 13  
Old 02-08-2014
Thanks Crontab. Sorry to reply very late. I was out of station..

I did tried something to skip file checking which will be there in whitelist file but still not working.

Code:
#!/bin/sh
set -f
startdir=$1
owner=$2
group=$3
WHITELIST="(/tmp/file_whitelist.txt)"
ownerexpr=${owner:+-user\ $owner}
groupexpr=${group:+-group\ $group}
listcount=0
whitelist_matches=0
while IFS="" read -r matchedentry; do
if [[ "$matchedentry" =~ $WHITELIST ]]; then
((whitelist_matches++))
else
echo -e "$matchedentry\r"
((listcount++))
fi
done < <(find "$startdir" \! \( $ownerexpr $groupexpr \) -ls | awk '{print $3, $5, $6, $11}')
if (( $listcount > 0 )); then
echo "$listcount items are having by '$Owner' ($whitelist_matches whitelisted)."
else
echo "Files which are Not in the : ($whitelist_matches whitelisted)."
fi

Can you please let me where it's going wrong.

All am trying to do is, script to check the file ownership thru script but skip some files checking and return message if all the file are in same ownership/pass if not, display files which are not having required ownership.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

need a shell script to extract the files from source file and check whether those files existonserve

Hi, I am new to shell scripting.Please help me on this.I am using solaris 10 OS and shell i am using is # echo $0 -sh My requirement is i have source file say makefile.I need to extract files with extensions (.c |.cxx |.h |.hxx |.sc) from the makefile.after doing so i need to check whether... (13 Replies)
Discussion started by: muraliinfy04
13 Replies

2. UNIX for Advanced & Expert Users

SuSE-11 Ownership of files having root got changed

Hi Experts, I have create a new user with uid and gid as 0 in SuSE-11 Server. After that all the files having root owner ship are showing as new user name as owner. If I login as root, and type 'id' command, it also shows the new user. Sample output from my server. host:~ # id uid=0(test)... (4 Replies)
Discussion started by: vipinable
4 Replies

3. UNIX for Dummies Questions & Answers

Ownership of files in bin directory

When I checked to see who or what owns the files in my bin directory I noticed that some were owned by root while many others were owned by bin. Should I be concerned that there are files in this directory owned by bin or is bin the same as root as it pertains to limiting access to the files in... (3 Replies)
Discussion started by: j490428
3 Replies

4. UNIX for Dummies Questions & Answers

Getting rid of files with no ownership

I am in the process of learning how to do system administration (just on my own Linux machine) and have been working with the find command. One of the things I tried was find / -nouser -o -nogroup I redirected the output of my find query into a text file, and when I did a wc -l on it, it... (1 Reply)
Discussion started by: kermit
1 Replies

5. UNIX for Dummies Questions & Answers

copy all files and folders and cjange or remove ownership

So tried: cp -r -p test1/ user@machine:///srv/www/vhosts/domain.co.uk/httpdocs/backup/ but this didn't work either :( Anyone able to help with this? Many thanks Mr M (3 Replies)
Discussion started by: misterm
3 Replies

6. UNIX for Advanced & Expert Users

command to copy files with original ownership

Hi, I need a command that to copy files from others and to keep files' ownership. Example: I copy file.txt from users "abc" to my local, and file.txt is own by user "abc" in local. Thanks in advance! (3 Replies)
Discussion started by: need_help
3 Replies

7. UNIX for Dummies Questions & Answers

100% ownership to files

ok. this is a bit complicated. i have a manager here who wants me to give another user access to all the files he owns. he wants this other user to have the same rights has he does. the problem has been that whenever the manager creates a new file the permissions on the files created... (3 Replies)
Discussion started by: Terrible
3 Replies

8. Linux

files ownership/permission problem

all the files and directories in my system are owned by root only.i try to(from root loggin) change the permission on the file but not permitted.can any one help to fix my problem .also while installing any software always error occur like no makefile available (1 Reply)
Discussion started by: jop
1 Replies

9. UNIX for Dummies Questions & Answers

ownership of files

Hi, While changing ownerships from the root on a server i'm managing, i typed chown -R username:users * and it changed all ownership to username. Can someone tell me if there is someway I can set things back the way they were before? I can't even su username from the root. Am I going to just... (4 Replies)
Discussion started by: szhu
4 Replies

10. UNIX for Dummies Questions & Answers

How do I change ownership of a directory and all of it's files.

How do I change ownership of a directory and all of it's files without changing permissions? (1 Reply)
Discussion started by: mborin
1 Replies
Login or Register to Ask a Question