Find Files in Directory by Permission?

Hello. I need to write a script that lets the user pick a directory. Then, all files are looped through, and the ones with read-write (for current user I think) are listed. Ending with a count of those files, but that parts easy. What I'm confused about is the middle.

So far I have

#!/bin/bash

clear

return=0
echo "please enter a directory name."
read directory

if [ -d "$directory" ]
then
echo "The $directory directory exists, looking..."
#LOOP SECTION HERE

else
echo "$directory does not exist"
return=1
fi
exit $return

Now I havent finished the WHOLE thing, but where I have the #comment is where I'm stuck and presumably where I'd begin the loop. How do I search the user input $directory for read-write files?

find /path/to/$directory -user $username -perm -u+r,-u+w

this will give this list of files, i.e. you don't need a loop.

Thanks, I'll try that now. Yeah I was doing some more research and ended up with

find $directory -type f -print -perm 666

This seemed to have worked, but it's listing non-existant files!

i.e. the folder "bin" has two files. Both are scripts ive made.
But this was listing each twice plus a non-existant one.
"read-write-access.txt" was listed as-is, PLUS a "read-write-access.txt~" was listed. With a tidle. Same as the other code. Plus a file called "Code" is listed, even though that file hasn't been in there for a while, and it won't show for me when I look in there. Odd. But I'll try your solution right now, thanks.

Edit: Your code gives the error "find: invalid argument -u+6 to -user"

Edit: Actually, could I get some help with where I'd put a counter to get the number of files found under these parameters?

the invalid arg error seems like it would have been caused by mixing up the options and args (i.e. -user -u+6 rather than -perm -u+6)

the strange results could be caused by subdirectories. find $directory refers to the search root. you can use the -depth flag to limit directory descending or the
-wholename flag to specificy the path

for result counts, I usually do something like this:

find ... -print | wc -l

if you need to output the list _and_ get the count, you can do:

find ... -print > results.txt
COUNT=`wc -l results.txt`

Edit: 10th edit to this post haha.

Now it's just a final problem. I need to concactonate two things. My text saying "you've found X ($COUNT) values". The string and the count in the same sentence.

Problem: $COUNT ends up being, if I echo it I get "1 results.txt"

COUNT=`wc -l results.txt|cut -f1 -d' '`

cuts the first space delimited field, which is your value

But it still shouldn't have been saying 1 - I had 4 results.

Now at if [ COUNT -gt 0 ] , line 23, i get the error integer expression expected

Is there no simpler way? Like at the -Find, there's an -Exec at the end, so each count adds 1 to a value called Count that was initially 0? I tried this earlier but got wierd errors, maybe you can tell me how it's done lol

....and now I cant get it to echo COUNT. Even when I make it = "lol" and echo is immediately. Just not showing up. /sigh

can you post the script?

the find ..-exec flag is for operating on every find result. Think of batch renaming, archiving, or deleting.

The VMWare won't allow me to have a shared clipboard. I could, however, take a pic.

http://img85.imageshack.us/img85/5025/codep.jpg

I tried to post an image. Says a moderator needs to accept it. Ugh.

But this is making no sense. I could be done with this now if it weren't for Fedora deciding it's going to defy logic. The big line starting with Find. Right? That equals 6 for my test directory. Which is correct. That big line is what my "Count" value should be. So I decide I'll write that count to "COUNT". Then IMMEDIATELY, for the sake of testing, echo it to verify it'll spit out the right value. But nothing. It's actually NOT echoing when I'm clearly telling it to. I'm very confused.

Example: COUNT=find $directory -type f -perm 666 -print | wc -l
echo $count

and nothing displays.

Then I was like "COUNT="loltest"
echo $count

and nothing. Why aren't my values displaying now?

http://img85.imageshack.us/img85/5025/codep.jpg

echo $count will display the value of 'count' but not of 'COUNT'.

what does 'echo $COUNT' display, or are you just typing in haste?

Nope.

I can say

COUNT="lol"
echo $COUNT

and nothing is happening....?

I really don't know what happened. I just made a NEW code and simply told it to echo something. And nothing comes up. It....stopped echoing entirely? Wow.

is this right? I'm trying an If statement to check if the number isn't 0.

if [ find $directory -type f -print -perm 666 ] -gt 0

...etc etc. But it's not working. No error messages, but it's not working.
I tried putting the -gt 0 in the brackets, but same thing.

Ok, I checked your image.

You are doing this:

find $directory -type f -perm 666 -print | wc -l > results.txt

which is outputting the result of 'wc' to 'results.txt' Thus results.txt contains something like

 6 results.txt

Then you set

count=wc -l results.txt

which is always going to be 1.

What you want to do instead is:

find $directory -type f -perm 666 -print > results.txt
count=wc -l results.txt|cut -f1 -d' '

that will output the results of the find to the file, and then you wordcount on it.
you can insert a 'cat results.txt' to check its contents too.

not sure about your new echo problem.

Yeah the echo thing is still a problem. But I re-entered that code you typed and everything works the same still. But still I can't echo anything else AND no matter what sort of If-Else I put it to ensure it's not zero, it does nothing. I even tested the If-Else once by changing the parameter to SDFSEFDG WRGDF. Just random text. And nothing happened. No error. It's ignoring my text. Ugh. I guess I'll have to hand in this assignment without an If-Else if this programs going to break down on me like this.

Thanks for your help thus far though. I have one last part of the assignment that I might need to ask some questions about, if you're still willing to answer a few questions here.

---------- Post updated at 08:42 PM ---------- Previous update was at 07:14 PM ----------

Sorry for the double post, I wasn't sure if just editing the last post would make my helper here re-check the topic.

Now I need to do something similar then different. Have the user input a directory again. Verify it. But then:
-Loop through allll (?) the files and find how many total sub-directories branch from the specified directory.
-Name the newest, and oldest, of these sub-directories. I really don't know where to begin. I might have a grasp on looping but I'm not sure of what I'd use to go through directories....whether there's a simple command, or if I need to use a whole lot of ls shenanigans to go through a hundred things....?

Heh. Any tips, pointers, or good commands to use here?

-Edit: I'm impressed with myself lol. In a single day of Unix I've figured out most of this assignment I'm one currently. The one thing I can't quite get is bugging me though. If the selected directory has only one subdirectory, it is to be named. In a message. There's messages like "no subdirectories were found", but I need a way to find the name of the one and only subdirectory, if a folder has just one. cd......sooomething. I'm not sure of the name, and I need to find out.. :smiley:

okay, so what I recommend when you have the time is to read the man page for 'sh' or 'bash' which will explain how 'for' works, as well as backticks, redirects, substitution, etc.

also read the man pages for 'find' and 'ls' --especially 'ls'.

so in your directory example

cd mydirectory
COUNT_SUBDIRS=`find . -type d ... |wc -l` # directories - get the number 

to list a directory by time, use 'ls -t' the newest will be at the top (head -1) the oldest at the bottom (tail -1)

to loop through the list in this order use the construct:

for newvar in list
do
...stuff...
done

or for example

for each in `ls -t`
do
  # test for directory
  # store oldest dir  (should be first dir encountered)
  # store newest dir (should be last dir encountered)
done

thanks for the bits! hope this helps. def read those man pages!

dv

Thanks, I have 90% of these scripts done.

My last problem is that still, despite your code, I cannot list the oldest and newest subdirectories. I tried other solutions online but there's not much, and it only seems to display the oldest/newest in THAT directory without branching further...oh well...this stuff is due in an hour anyhow. I'll cross my fingers, and if I don't get it by then, then at least this stuff is better than nothing. Thanks so much for the assistance.