Script to check files ownership


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to check files ownership
# 1  
Script to check files ownership

Hi All,

I wanted to check the files ownership and permission based on the path given it as arguments thru script.

I was able to get the required command using ls but i would like this command to put in a script and check the file ownership against the what it needs to be and report back if it's doesn't match with filename.

command i have got :

Code:
ls -ld /dtr/tools/bin/*.* | awk '{print $1, $3, $4, $9}'

from the above command, i get the out with file permission, ownership, filename..

now, I want this command to put into script and pass the path as argument and ownership as arugment to check against each files and report back the filename if ownership doesn't match.

Can anyone help me on this pls.

Thanks,
Optimus
# 2  
Hello,

Please use the following script for same.

Code:
value=$1
cd $value
ls -ltr | awk 'BEGIN{print "File name""\t""owner name""\t" "group name"} {print $9"\t"$3"\t"$4}'


Output will be as follows.

Code:
File name       owner name      group name
without_length_70_lines singh1        users
value_braces    singh1        users
to_find_min_and_max_number      singh1        users
timings_file_query.ksh  singh1        users
timings_file_query      singh1        users
test_data       singh1        users
test_chumma1    singh1        users


Thanks,
R. Singh
# 3  
That is a useless use of ls *, ls does not need it. If the number of files is large enough, * will break down when plain ls would be fine, too.

I started putting awk's output into a loop then realized the loop itself kind of makes awk unnecessary.

Code:
ls -l /dtr/tools/bin/ | while read PERMISSION OWNER GROUP SIZE MON DAY YEAR FILENAME
do
        echo "filename is $FILENAME"
        # Rest of code here
done

# 4  
With find (recursive!)
Code:
#!/bin/sh
startdir=$1
owner=$2
[ -n "$startdir" -a -n "$owner" ] || exit
find "$startdir" \! -user "$owner" -exec ls -ld {} + | awk '{print $1, $3, $4, $9}'
# or
#find "$startdir" \! -user "$owner" -ls | awk '{print $3, $5, $6, $11}'

This User Gave Thanks to MadeInGermany For This Post:
# 5  
Thanks MadeinGermany

script works as expected but I have a question, how can I check the owner, to which group belong to.

say from the script if i need to pass both owner and group to check on each file from the path.. how can i do that..

all i need to do is check the group and owner, if it matches what been send throu arguments then fine if not report back that file details.

Can you please help
# 6  
You mean, list either wrong given owner or wrong given group?
Then it's this one:
Code:
#!/bin/sh
startdir=$1
owner=$2
group=$3
[ -n "$startdir" -a -n "$owner" -a -n "$group" ] || exec echo 'need arguments: startdir owner group'
find "$startdir" \! \( -user "$owner" -a -group "$group" \) -exec ls -ld {} + | awk '{print $1, $3, $4, $9}'
# or
#find "$startdir" \! \( -user "$owner" -a -group "$group" \) -ls | awk '{print $3, $5, $6, $11}'

This User Gave Thanks to MadeInGermany For This Post:
# 7  
Thanks MadeInGermany.

The solution which you gave is kind of working. Is there a way in which we can skip or leave if any of the file owner/group needn't to be checked.

I was thinking, keeping some config file, where in which we can specify the filename, owner and group it belongs to. Then after we run the script, it read this config file first and then check the files one by one from the mention path. If the same filename found then it should crosscheck the owner/group mentioned against config file and the actual exists one. if it differs then report file has different owner/group and expected.

If the file owner/group matches with what had in config, then report back saying everything is fine.
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #237
Difficulty: Easy
Paul Baran was the first to build a theoretical model for communication using packet switching.
True or False?

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

Featured Tech Videos