0

I am trying to write this command on UNIX:

ls -l | sed 'p/^.rwx'

But I get the following error:

sed: -e expression #1, char 2: extra characters after command

I'm trying to print the files/dirs in which the user has all the permitions (rwx)

  • can you add a description of what you are trying to do with that command? also, parsing ls probably not a good choice here – Sundeep May 10 '17 at 15:03
  • I'm trying to print the files/dirs in which the user has all the permitions (rwx) – M.T May 10 '17 at 15:08
  • click unix.stackexchange.com/posts/364193/edit and add that information to your question... – Sundeep May 10 '17 at 15:09
  • The /p goes at the end of the sed command, not the beginning. sed -n /pattern/ p – Munir May 10 '17 at 15:10
3

Your print command needs to come at the end, and you also want to suppress printing by default:

ls -l | sed -n '/^.rwx/p'

If you're on a system with the stat command, there's another way to solve the problem:

for f in *
do
  stat -c "%a" "$f" | grep -q ^7 && printf "%s\n" "$f"
done

It's dangerous to rely on the output of ls; consider someone who created a file like this:

touch $'foo\n-rwx some file'

... that will create a separate line in the ls output that (falsely) matches the regular expression. Using a shell glob (*) avoids that issue.

Yet another way is to use find:

find . ! -name . -prune -perm -700 -ls
  • I don't encourage using ls for anything other than human consumption but for the record (and contrary to popular belief) there are cases when you can safely "parse" ls output - this is just one of them (another one that comes to mind is when you want to count items in a directory). So, you can simply do ls -lq | sed '/^.rwx/!d' and it will work with any kind of file name. – don_crissti May 10 '17 at 17:19
  • good point with the -q flag -- that gets around the filename issue. Thanks, Don! – Jeff Schaller May 10 '17 at 17:27
0

For your specific problem, consider using

find . -perms 700 -maxdepth 1 -ls

or

find . -perms /u+rwx -maxdepth 1 -ls

Reference.

  • this will recurse subdirectories (different from their original ls -l) and I think you want -700 and maybe -ls, to emulate the original ls -l behavior. – Jeff Schaller May 10 '17 at 15:31
  • And I have to use sed... – M.T May 10 '17 at 15:54
  • @JeffSchaller: good points. Updated my answer to include -maxdepth 1 and -ls. – thiagowfx May 10 '17 at 17:32
  • @M.T ok, I just provided an alternative because you didn't specify it was a requirement to use sed. – thiagowfx May 10 '17 at 17:34
-1

You want to run this:

ls -l | sed -n '/^.rwx/p'

n -> Prints only matching data

^.rwx -> Starting with any character but followed by rwx.

p -> Print the data

  • 1
    Can you please explain your answers, add some details – Romeo Ninov May 10 '17 at 19:03
  • ls -l | sed -n '/^.rwx/p' n -> Prints only matching data ^.rwx -> Starting with any character but followed by rwx. p -> Print the data – Murali Mohan May 25 '17 at 7:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.