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)

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

3 Answers 3

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
2
  • 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. Commented May 10, 2017 at 17:19
  • good point with the -q flag -- that gets around the filename issue. Thanks, Don! Commented May 10, 2017 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.

4
  • 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. Commented May 10, 2017 at 15:31
  • And I have to use sed... Commented May 10, 2017 at 15:54
  • @JeffSchaller: good points. Updated my answer to include -maxdepth 1 and -ls. Commented May 10, 2017 at 17:32
  • @M.T ok, I just provided an alternative because you didn't specify it was a requirement to use sed. Commented May 10, 2017 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

2
  • 1
    Can you please explain your answers, add some details Commented May 10, 2017 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 Commented May 25, 2017 at 7:29

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.