#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2010
    Posts
    5
    Rep Power
    0

    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
  2. #2
  3. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,002
    Rep Power
    9398
    Pretty sure ls can't do it, but find can.
    Code:
    $ man find
    Be sure to only include files.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2010
    Posts
    5
    Rep Power
    0
    Originally Posted by requinix
    Pretty sure ls can't do it, but find can.
    Code:
    $ man find
    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?
  6. #4
  7. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,002
    Rep Power
    9398
    Read the manual page. Because the flags are right there, and it's not like they're named cryptically.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2010
    Posts
    5
    Rep Power
    0
    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.
  10. #6
  11. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,002
    Rep Power
    9398
    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)?
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,442
    Rep Power
    1688
    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
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2010
    Posts
    5
    Rep Power
    0
    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.
  16. #9
  17. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,002
    Rep Power
    9398
    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.
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,442
    Rep Power
    1688
    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

IMN logo majestic logo threadwatch logo seochat tools logo