How can I list all files in my home directory that have read, write, and execute permissions?

  • Thread starter Thread starter Cudi1
  • Start date Start date
  • Tags Tags
    Linux
AI Thread Summary
To list all files in your home directory with read, write, and execute permissions for your user and containing "txt" in their names, the `find` command is recommended. Use `find . -name "*txt*" -perm -700 -exec ls -l {} \;` to achieve this, ensuring you check the permissions with the `-perm` parameter. It's important to note that using `-perm 700` checks for permissions only for the user, while `-perm 777` includes group and other permissions, which may not meet the requirement. The discussion emphasizes the utility of the `man` pages for command reference and suggests that understanding the specific permissions required is crucial for accurate results. Testing commands directly in a Linux environment is encouraged for verification.
Cudi1
Messages
98
Reaction score
0
1. Homework Statement :

list is long form all those files in your home directory that have read write and execute permissions for you, and whose name contains the string "txt"

2. Relevant commands, code, scripts, algorithms:
ls
find


3. The attempts at a solution (include all code and scripts):
ls -l would tell me the permissions
but find . -type perm 777 would give me files that have read write execute permissions
However , i feel like i should pipe it because i need a long form of those files
 
Physics news on Phys.org
I am not sure if I understand you correctly, but perhaps piping through grep will help?

"txt" part can be done directly using ls command.
 
Do you have access to the man page for find? Whenever you can't figure out a Unix/Linux command, always look it up in the man pages. For example, type this at the command prompt:

man find

I don't want to just blurt out the answer for you, but I'll give you two really big hints:

1. Use the -name parameter to look for the .txt's. You will need to put a backslash in front of the asterisk used as a wild card.
2. Look at the -exec parameter for find. It does what you need. You can use it to run ls -l on every file it matches. Note that there will probably be a semi-colon at the end of the command, and you must put a backslash in front of it.

Honestly, I never use find's -exec, preferring to use a pipe and xargs. Since you can't use xargs, that's out. But for future reference, xargs is really super useful. You can do things like:

find . -name \*.txt | xargs ls -l

It's amazing what you can do by chaining together Unix commands with pipes.

EDIT: Just noticed, permissions of 777 are incorrect for what you want to do. You wants to know only those that have read, write and execute FOR YOU. 777 will also require that for group and 'other' permissions. There's a symbolic form that's perhaps more obvious than octal that's documented in the man page. Also note that there are examples at the bottom of the find man page that should be rather useful.
 
Last edited:
Ok, didn't want to edit again, but I don't think what I said about permissions is quite correct for your case. Look at the -executable -readable and -writable parameters to find. That's what you want.

If you do it the other way with -exec you'll need to deal with some logic dealing with the file permissions, or end up with an incorrect list. For example, maybe a file is not owned by you, but is in a group you are in, and you have those permissions set for the group. Or maybe you do own it, and have those permissions. Or... Well, hopefully you get the idea.
 
k thanks, programing is not my subject but would this work out the same :
ls -l | find . -name "*txt*" -perm 700
i feel as if I am missing a little piece from this code
 
Cudi1 said:
k thanks, programing is not my subject but would this work out the same :
ls -l | find . -name "*txt*" -perm 700
i feel as if I am missing a little piece from this code

No, in fact that makes little sense. It also ignores most of what I posted previously.

One thing at a time. First, get the find command finding the right files. I pretty much told you exactly what parameters to use with find. For one, I told you that -perm will not work, but to use other parameters, and told you exactly which ones.

You also don't need a pipe. Piping ls -l to find makes no sense. 'find' finds the files that you want, and then you run ls -l on each of them using the -exec parameter to find.

Have you looked at the man page for find? Like I said, there's examples at the bottom that should clear up most questions you have. As they say, RTFM. It's as good advice now as it ever was.

You have access to a Linux system for this, right? You want to know if a command you built up works? Type it in and try it. It's perhaps not as hard as you're making it for yourself.
 
k sorry for the amount of questions , but why exactly wouldn't i be able to do this:
find . -perm 700 -name "*txt*" -exec ls -l {} \;
( it would allow me to list files that have only read write and execute permission for me, and it would show the long listing due to the - exec command)
?
 
Cudi1 said:
k sorry for the amount of questions , but why exactly wouldn't i be able to do this:
find . -perm 700 -name "*txt*" -exec ls -l {} \;
( it would allow me to list files that have only read write and execute permission for me, and it would show the long listing due to the - exec command)?

Depends on exactly what your teacher asked. The way you put it, that isn't completely right, I think. You stated:

Cudi1 said:
list is long form all those files in your home directory that have read write and execute permissions for you, and whose name contains the string "txt"

It's just vague enough I'm not 100% sure. If he wants all the files you have read, write and execute permissions for, then that won't work. If he wants all files that have the user permissions set to read, write and execute, then that's ok. Maybe you'd also want to make sure you own the file with the -user parameter?

Thing is, I would consider you have (for example) read permissions on a file if you can read it. The file could be in your home directory, have the read bit set, but not be owned by you. So that file may be completely unreadable for you, but has those permissions set. Or perhaps you can read it because it has the 'other' read permission bit set.

I'll leave it to you to decide what the teacher is expecting, but I would use the more intelligent -readable -writable -executable parameters, given the way it's stated. If he'd said something like 'permission BITS for your user account', I would think otherwise. I take no responsibility if that turns out to not be what he's expecting. :biggrin:

What I would probably do is both and ask him specifically which he meant, and hand him the proper one.

The command you have now seems ok. Though I think you mean -perm -700 rather than -perm 700 (make sure I'm right by testing it, of course). Depends on whether you care what the group and "other" permissions are. I'd suggest scrutinizing the man page entries for -perm and such and making sure it's doing what you want.
 
Back
Top