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

  • Thread starter Cudi1
  • Start date
  • Tags
    Linux
In summary: 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)
  • #1
Cudi1
98
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
  • #2
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.
 
  • #3
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:
  • #4
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.
 
  • #5
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
 
  • #6
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.
 
  • #7
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)
?
 
  • #8
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.
 

1. What is a "Linux listing"?

A "Linux listing" refers to the output of the ls command in Linux, which displays a list of files and directories in the current working directory.

2. How do I view a Linux listing?

To view a Linux listing, open the Terminal and navigate to the directory you want to view. Then, type ls and press Enter. The list of files and directories will be displayed on the screen.

3. What information does a Linux listing include?

A Linux listing includes the name of the file or directory, its permissions, owner, size, and last modified date. It may also include additional information depending on the options used with the ls command.

4. How can I customize a Linux listing?

You can customize a Linux listing by using various options with the ls command. For example, you can use the -l option to display a long listing with more detailed information, or -a to display hidden files and directories.

5. Can I save a Linux listing to a file?

Yes, you can save a Linux listing to a file by using the ls command with the -l option and redirecting the output to a file using the > symbol. For example, ls -l > file.txt will save the listing to a file named "file.txt" in the current directory.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
Replies
38
Views
3K
  • Programming and Computer Science
Replies
1
Views
3K
Replies
10
Views
958
  • Programming and Computer Science
Replies
1
Views
827
  • Programming and Computer Science
3
Replies
75
Views
4K
Replies
4
Views
2K
Replies
2
Views
881
  • Programming and Computer Science
Replies
2
Views
17K
Back
Top