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
Click For Summary

Discussion Overview

The discussion revolves around how to list files in a home directory that have read, write, and execute permissions for the user, specifically targeting files whose names contain the string "txt". The conversation includes attempts to formulate the correct command using Unix/Linux tools, particularly focusing on the use of the `find` command and its parameters.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant suggests using `ls -l` to check permissions and `find` to locate files with specific permissions.
  • Another participant proposes using `grep` to filter results from `ls` for the "txt" string.
  • A suggestion is made to use the `-name` parameter with `find` to search for files ending in ".txt" and to utilize the `-exec` parameter to run `ls -l` on the found files.
  • Concerns are raised about using `-perm 777`, as it would require permissions for group and others, rather than just the user.
  • Participants discuss the potential confusion around permissions and suggest using `-executable`, `-readable`, and `-writable` parameters instead of `-perm` to ensure the correct files are listed.
  • One participant questions the validity of a command they proposed, which combines `ls -l` with `find`, and is told that this approach does not make sense in the context of the task.
  • Another participant expresses uncertainty about the command structure and whether it meets the assignment requirements, particularly regarding ownership and permissions.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the best command to use, with multiple competing views on how to approach the problem and differing interpretations of the assignment requirements.

Contextual Notes

There are unresolved questions regarding the exact requirements of the homework statement, particularly about whether the focus should be on user permissions or general file permissions. The discussion also highlights the importance of understanding command parameters and their implications for file ownership and accessibility.

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.
 

Similar threads

Replies
1
Views
4K
  • · Replies 16 ·
Replies
16
Views
4K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 4 ·
Replies
4
Views
2K
Replies
38
Views
5K
Replies
10
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 75 ·
3
Replies
75
Views
7K
  • · Replies 27 ·
Replies
27
Views
24K