I'm working on the following question for my class (in Unix) and I'm running into some problems.
Using the
grep
command and regular expressions, create a pipe-based chain of commands that will list all files in the default directory that others can read or write.
This is the command I have so far and it will correctly match the right lines, but I only want to to print the name of the file that has the desired permissions.
ls -la | grep "^.......rw.*:\d\d\s(.*)$"
here is the result of just typing ls -la
by the way which is what I'm "greping"
total 344
drwxr-xr-x 10 cameronpattisall staff 340 Apr 7 11:21 .
drwxr-xr-x 7 cameronpattisall staff 238 Apr 7 11:22 ..
-rw-r--r-- 1 cameronpattisall staff 6148 Apr 7 11:21 .DS_Store
-rw-r--r-- 1 cameronpattisall staff 11591 Apr 7 11:21 DifferencesToTagged.txt
-rw-r--r-- 1 cameronpattisall staff 4291 Apr 7 11:21 functions.py
-rw-r--r-- 1 cameronpattisall staff 6080 Apr 7 11:21 ls.txt
-rw-r--r-- 1 cameronpattisall staff 5511 Apr 7 11:21 prog4.py
-rw-r--r-- 1 cameronpattisall staff 345 Apr 7 11:21 shared.py
-rw-r--r-- 1 cameronpattisall staff 1832 Apr 7 11:21 subversion-log.txt
-rw-r--r-- 1 cameronpattisall staff 122295 Apr 7 11:21 testcases.txt
I guess the solution is to use:
grep -E "^-.{6}(r|.w)"
The structure of the first column is "-/d" (file/dir) + xxx
(owner) + xxx
(group) + xxx
(others).
So you want to match those starting with -
(file) and containing in the "gorup" part either r..
or .w.
.
To print just the file names, then you can pipe to grep -o '[^ ]*$'
. This will print the last block of text. Note this is terrible fragile, since in a file name with spaces it will just print the last one.
I stored your ls -la
in the file a
:
$ grep -E "^-.{6}(r|.w)" a | grep -o '[^ ]*$'
.DS_Store
DifferencesToTagged.txt
functions.py
ls.txt
prog4.py
shared.py
subversion-log.txt
testcases.txt