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 :

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

Hello,

Please use the following script for same.

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.

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

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.

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

With find (recursive!)

#!/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}'
1 Like

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

You mean, list either wrong given owner or wrong given group?
Then it's this one:

#!/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}'
1 Like

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.

You can omit the group here, or omit the user by setting the 2nd argument to ""

#!/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}'
1 Like

Thanks Again but am getting error when i run the script.

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

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.

Sorry, /bin/sh needs the space character escaped in the :+ modifier.
I have changed that in my script.

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 ?

What have you tried?

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

1 Like

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.

#!/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.