Find Files in Directory by Permission?

 
Thread Tools Search this Thread
Operating Systems Linux Fedora Find Files in Directory by Permission?
# 1  
Old 09-24-2009
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?
# 2  
Old 09-24-2009
Code:
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.

Last edited by varontron; 09-24-2009 at 03:28 PM.. Reason: more info
# 3  
Old 09-24-2009
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?

Last edited by Feuyaer; 09-24-2009 at 03:53 PM..
# 4  
Old 09-24-2009
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:

Code:
find ... -print | wc -l

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

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

# 5  
Old 09-24-2009
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"

Last edited by Feuyaer; 09-24-2009 at 05:16 PM..
# 6  
Old 09-24-2009
Code:
COUNT=`wc -l results.txt|cut -f1 -d' '`

cuts the first space delimited field, which is your value
# 7  
Old 09-24-2009
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

Last edited by Feuyaer; 09-24-2009 at 05:48 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find list of files missing read & execute permission

Hi, I'm writing a post-upgrade script and I want to find which files don't have read and execute to everyone. I can run a find . ! -perm, but then I have to use a list of the possible permissions (777,775, 755 etc). Is there a more elegant solution? Thanks (2 Replies)
Discussion started by: Catullus
2 Replies

2. AIX

How to set owner and permission for files/directory in directory in this case?

Hi. My example: I have a filesystem /log. Everyday, log files are copied to /log. I'd like to set owner and permission for files and directories in /log like that chown -R log_adm /log/* chmod -R 544 /log/*It's OK, but just at that time. When a new log file or new directory is created in /log,... (8 Replies)
Discussion started by: bobochacha29
8 Replies

3. UNIX for Dummies Questions & Answers

find Files in sub-directory

Hi Just want to ask, Is it possible to find a file from a directory up to its sub-directories? Thanks, cmarzan (10 Replies)
Discussion started by: cmarzan
10 Replies

4. UNIX for Advanced & Expert Users

Find all files in the current directory excluding hidden files and directories

Find all files in the current directory only excluding hidden directories and files. For the below command, though it's not deleting hidden files.. it is traversing through the hidden directories and listing normal which should be avoided. `find . \( ! -name ".*" -prune \) -mtime +${n_days}... (7 Replies)
Discussion started by: ksailesh1
7 Replies

5. UNIX for Dummies Questions & Answers

How to Find Files other than specified directory ?

Hi All, I am creating one script to Archive the older log files to Archive folder and deleting older files. For example below path contains different sub folders. So searching for log files older than 2 days then zip and moving to Archive directory in the same directory. Source files :-... (4 Replies)
Discussion started by: vadlamudy
4 Replies

6. UNIX for Dummies Questions & Answers

Find files and display only directory list containing those files

I have a directory (and many sub dirs beneath) on AIX system, containing thousands of file. I'm looking to get a list of all directory containing "*.pdf" file. I know basic syntax of find command, but it gives me list of all pdf files, which numbers in thousands. All I need to know is, which... (4 Replies)
Discussion started by: r7p
4 Replies

7. UNIX for Dummies Questions & Answers

Need help to find the files under a directory

Hi, I wanted to delete all the files under a directory "/apps/tmp/" which are two weeks older. But i should not delete the sub-directories and the contents of sub-directories. I also have searched in forum and found the following command, find . \( ! -name . -prune \) -mtime +13 -print ... (8 Replies)
Discussion started by: Sheethal
8 Replies

8. Shell Programming and Scripting

Find files in directory

Hi all I want to find a particular file type lets say .abc under /home/oracle/, the file name is start with 'D' and followed by ddmmyyyy date format, the file name should look like this D19092008.abc To my question, how can i perform the searching from the date 19/09/2008 to 29/09/2008. The... (3 Replies)
Discussion started by: coldstarhk
3 Replies

9. Shell Programming and Scripting

How to find a specific files in a many directory

Dear All, Appreciate some help here. I have a log of report. It located in several directory as below: Directory: mysscpr1 mysscpr2 mysscpr3 my_scnpr4 In the directory it contain hundred of files. i need to find a specific files that contain 'invc2345' in the directory. How... (7 Replies)
Discussion started by: selamba_warrior
7 Replies

10. UNIX for Dummies Questions & Answers

How to find the count of files in a directory

Hi Gurus WHat would be the command to check whether there is a file in particular path or not.. for ex: my file name is ExRate_20071501.csv I can have many files with same pattern but diffrentiated by date.. i have a process where i have to check if files exist in tht folder i have to... (5 Replies)
Discussion started by: sish78
5 Replies
Login or Register to Ask a Question