3

I need to list or show or query for the files in a folder (well, technically, on a USB drive, but I can navigate to it in Finder/Terminal) that are marked readonly.

All the Google-fu in the world just reveals solutions to change permissions but I don't need to do that.

My Dashcam marks videos/images readonly to save them when I press the button on it, but they're still in a folder with a few hundred MOV files, and I need a simple way to filter down to the ones I am looking for.

1
find . -type f -perm +444 \! -perm +222

searches for all files (-type f) which are readable (-perm +444) but not writable (! +perm +222).

If your mind boggles after reading up on -perm in man find you can also use the (significantly slower, especially on slow devices) option of processing the output of findyourself:

find . -type f -print0 |
    xargs -0 -n 1 sh -c '[ -r "$1" -a ! -w "$1" ] && echo "$1"' sh

This basically takes each file find finds, and runs it through a small shells script to check permissions.

PS: Hey, I didn't say the second way is less mind-boggling :-)

  • 1
    Although the second method runs slower, it might be more accurate in certain situations if I am not mistaken with this example test for example: touch test_file; sudo chown root:staff test_file; (the first method fails to list the file, although it is a read-only file for the user) – Yoric Dec 31 '18 at 10:02
  • 1
    @Yoric There is no general solution for this using find alone, see unix.stackexchange.com/questions/22421/… and the original question linked from there. In the case of the OP it's probably not an issue. – nohillside Dec 31 '18 at 11:03
  • 1
    Thanks, my head is spinning right now, so much to learn digging into "simple questions" :) – Yoric Dec 31 '18 at 11:34
1

One way is to make use of the -w option in bash to check if the file is writable or not.

Go into the directory you want to check your files, then enter:

for RO in $(find . -type f);do [ -r "$RO" ] && [ ! -w "$RO" ] && echo $RO;done

(credit to www.unix.com)

[EDIT]

To deal with spaces in file names, better to use the find -exec way rather than looping into the find:

find . -type f -exec [ -r {} ] \; -exec [ ! -w {} ] \; -exec echo {} \;

or

find . -type f -exec [ -r {} ] \; -exec [ ! -w {} ] \; -print
  • Textually parsing the output from find usually breaks on file names containing space characters and similar. May not be a problem in the context the OP has, but might hit you in other circumstances. – nohillside Dec 31 '18 at 8:59
  • @nohillside Thanks for the head up, I fixed the case replacing the for loop with using find -exec – Yoric Dec 31 '18 at 9:51
  • My (hopefully) last remark on this: find . -type f -exec [ -r {} -a \! -w {} ] \; -print, much easier to read than the find ... | xargs ... sh -c monstrosity in my own answer :-) – nohillside Dec 31 '18 at 11:13
-1

List the files and grep for the read-only pattern:

ls -l | grep '^-r--'

^ symbol indicates start the line.

We are filtering only files here by mentioning ^-, after that looking only for read permission files by specifying r--. If you want to filter read & executable permission files, you can use r-x.

If you want just the filename, you can use below command

ls -l | grep '^-r--' | awk 'NF>1{print $NF}'

Printing the file name using above command works, only if you don't have spaces in file name.

  • 2
    Grepping the ls -l is a smart way to do it, but beware that your files might start with -rw-r--r-- with root as the owner, and such files won't be listed, even though they aren't writeable for the user. – Yoric Dec 31 '18 at 4:51
  • 1
    grep ... | awk is usally not required, the second command can be rewritten as ls -l | awk '/^-r--/ {print $NF}' – nohillside Dec 31 '18 at 9:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .