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)
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
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.
-q flag -- that gets around the filename issue. Thanks, Don!
For your specific problem, consider using
find . -perms 700 -maxdepth 1 -ls
or
find . -perms /u+rwx -maxdepth 1 -ls
ls -l) and I think you want -700 and maybe -ls, to emulate the original ls -l behavior.
-maxdepth 1 and -ls.
sed.
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
/pgoes at the end of the sed command, not the beginning.sed -n /pattern/ p