November 28th, 2010, 07:37 PM
-
Scripting, counting the number of readable, writable, and executable items
Hello, I'm writing a script in sh in which the first command line argument is a directory. from that, iI need to count the number of readable, writable, and executable items in the directory. I know using $1 represents the directory, and ls would display all the items in the directory, and that ls | wc -l would count the number of items. I'm uncertain as to how to filter ls so that only the readable/writable/executable files are shown and counted, and since I don't know if i'm the user, group, or other I cant check the permissions.
would someone be able to show me how to do this? Thanks
November 28th, 2010, 07:47 PM
-
Pretty sure ls can't do it, but find can.
Be sure to only include files.
November 28th, 2010, 08:03 PM
-
Originally Posted by requinix
Pretty sure ls can't do it, but find can.
Be sure to only include files.
I tried using find, but I can't find a flag that only makes the readable/writable/executable be shown (so they can be counted). I cant use the -perm flag, since I don't know if I'm the user, a group, or an other. Do you know how I could use find?
November 28th, 2010, 09:01 PM
-
Read the manual page. Because the flags are right there, and it's not like they're named cryptically.
November 28th, 2010, 09:28 PM
-
Originally Posted by requinix
Read the manual page. Because the flags are right there, and it's not like they're named cryptically.
I just re-looked through the manual pages of find, and I still do not not see flags, that print out only the readable items, writable items, or the executable items. The only command I see that relates to it is -perm, which I cant use, since I have no idea if i'm the user, group, or other. -type also doesn't have a flag that shows which items are readable, writable, or executable as well, so unless I am completing missing something, man find does not give me a flag that does it.
November 29th, 2010, 02:02 AM
-
Hmm. Just noticed: my Ubuntu 10.04 has -executable, -readable, and -writable tests, while they don't appear in other places - such as here. So sorry about that: yours probably doesn't have them then.
-perm +/- mode is what you're looking for; whether you use -mode or +mode depends on the definition of "executable" et al. That is, does "executable" mean "executable by anyone" (you'll need u and/or g and/or o +x) or "executable by the owner/group/world" (u+x or g+x or o+x)?
November 29th, 2010, 06:49 AM
-
As requnix says, the more important thing is to decide exactly what is required when you say executable, etc. Is that by the user running the script, the owner of the file/directory being checked, anyone, or at least someone, etc.
It may not be what you want, but something like the following may suffice for you. I have used the assumption that you did not want sub directories 'scanned'.
Code:
#!/bin/sh
# Make sure we have a directory
if [ $# -ne 1 ]
then
echo "Must specify a directory name"
exit 2
else
if [ ! -d "$1" ]
then
echo "Specified directory does no exist"
exit 3
fi
fi
# set up a temporary file name
TF=/tmp/$0.tmp
# Long listing (minus user and group, for sake of efficiency) to temp file
# copying only the first field which is the attributes, as we only care about counts
ls -log "$1" | awk '{print $1}' | grep -v total > $TF
# Look for each attribute in turn and count (the -c) it
r_count=$(grep -c "r" $TF)
w_count=$(grep -c "w" $TF)
x_count=$(grep -c "x" $TF)
# show the results
echo "Read: $r_count"
echo "Write: $w_count"
echo "Execute: $x_count"
# Tidy up
rm $TF
exit
The moon on the one hand, the dawn on the other:
The moon is my sister, the dawn is my brother.
The moon on my left and the dawn on my right.
My brother, good morning: my sister, good night.
-- Hilaire Belloc
November 30th, 2010, 01:48 PM
-
Originally Posted by requinix
Hmm. Just noticed: my Ubuntu 10.04 has -executable, -readable, and -writable tests, while they don't appear in other places - such as So sorry about that: yours probably doesn't have them then.
-perm +/- mode is what you're looking for; whether you use -mode or +mode depends on the definition of "executable" et al. That is, does "executable" mean "executable by anyone" (you'll need u and/or g and/or o +x) or "executable by the owner/group/world" (u+x or g+x or o+x)?
the problem with that, is I mean executable by me. However, I dont know if i'm the user, group, or other for that file, so I can't really use the -perm flag.
November 30th, 2010, 06:26 PM
-
Something to check (I'm not near a Unix box right now): whether test(1)'s -r[eadable] -w[riteable] and -x[ecutable] tests run in the current user's context or not. I believe it does.
December 1st, 2010, 06:50 AM
-
It does, yes [ -x file ] will be true if file is executable by the user running the command.
The moon on the one hand, the dawn on the other:
The moon is my sister, the dawn is my brother.
The moon on my left and the dawn on my right.
My brother, good morning: my sister, good night.
-- Hilaire Belloc