2

How would I count the number of files in a given directory that the current user has read permissions and write permissions on ?

I am starting with:

echo "whats the directory you want to check ?"
read dir

not sure then should I use a find command ?

  • Note that without assistance from superuser, a user can not necessarily know all the files he has access to (like for files in directories he can enter but not read). – Stéphane Chazelas Sep 22 '15 at 14:38
  • I can't really think of a non-convoluted way of doing this in bash. Can you use other scripting languages or does it have to be pure bash? – Gravy Sep 22 '15 at 14:45
  • has to be pure bash im afraid – johndoe12345 Sep 22 '15 at 15:03
  • i have tried fileread=$find "$dir" -readable -printf . | wc -c ) and then echo that but it just tells me i have read permissions to all the files in the directory, even though i have removed all permissions from one file (verified this by trying to less it and it says permission denied. Is there a way to do this with a for loop? something like for files in $dir then do hte find command ? – johndoe12345 Sep 22 '15 at 15:04

3 Answers3

5

You need to ask root to get you the list of files (for the ones that are below directories you can access but not read) and then check for the rights:

sudo find "$dir" -print0|perl -Mfiletest=access -l -0ne'++$n if-r&&-w}{print+$n'

If you don't care about files that are below non-readable directories (but you can still read and write), with GNU find:

find "$dir" -writable -readable -printf . | wc -c

Note that both check the access permissions (of every type of file including directories), it's not only based on permissions. It should give you the number of files that you would successfully open in read+write mode (without creation). For instance, for symlinks for which permissions are rwxrwxrwx, it only reports those that point to a file that you have read and write permission to.

0

So, I do not know if this will help you along, but an alternative to using find is to print all information from all files, and whittle it down to just user permissions then count. Perhaps something like:

$> cd dir
$> count="$(ls -l | cut -d ' ' -f1 | cut -c 2-4 | grep rw | wc -l | sed 's/ //g')"
-1

The find utility and its -perm option are your friend here.

$ find <DIR> -type f -perm /u+r,u+w | wc -l

In a shell script you can get the value of given arguments with the special variables $1, $2, ..., $n. To use to current directory if none was given, you can go this way:

DIR=.
if [ "$1" ]
then
    DIR="$1"
fi
find $DIR -type f -perm /u+r,u+w | wc -l
Spack
  • 1,997
  • thanks. that works good. do you know how i would set the search directory to the current directory if the user doesnt enter any ? I am trying to use case 0) to set the dir as the c urrent one but cant figure it out – johndoe12345 Sep 24 '15 at 14:22
  • hmm. tried that but its just taking my directory as . even if i enter a directory. i read in dir from the user input to "what directory do you want to check" so dir=. then just sets it at the root level im guessing. if the user runs the script and enters a dir then i want the fileread execute on that directory, if the user just leaves it blank and presses enter then i want it to run on the current directory (whatever that is , it could be anything, depends on where the user is) – johndoe12345 Sep 25 '15 at 08:02
  • @johndoe12345 how do you request the user's input? Via arguments or using read? Can you update your question with the full script? – Spack Sep 25 '15 at 10:00
  • 1
    No, that's wrong. That checks whether the owner of the file (not the current user) has either (not both) read or write (or both) access to the file. You also have missing quotes around $DIR and are making the assumption that file names don't contain newline characters. – Stéphane Chazelas Sep 29 '15 at 11:39