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?
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:
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"
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
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.
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.
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..
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!
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.