Find all writable files in the current directory

49,219

Solution 1

find -type f -maxdepth 1 -writable

Solution 2

The -writable option will find files that are writable by the current user. If you'd like to find files that are writable by anyone (or even other combinations), you can use the -perm option:

find -maxdepth 1 -type f -perm /222

This will find files that are writable by their owner (whoever that may be):

find -maxdepth 1 -type f -perm /200

Various characters can be used to control the meaning of the mode argument:

  • / - any permission bit
  • - - all bits (-222 would mean all - user, group and other)
  • no prefix - exact specification (222 would mean no permssions other than write)

Solution 3

to find writable files regardless of owner, group or others, you can check the w flag in the file permission column of ls.

ls -l | awk '$1 ~ /^.*w.*/'

$1 is the first field, (ie the permission block of ls -l) , the regular expression just say find the letter "w" in field one. that's all.

if you want to find owner write permission

ls -l | awk '$1 ~ /^..w/'

if you want to find group write permission

ls -l | awk '$1 ~ /^.....w/'

if you want to find others write permission

ls -l | awk '$1 ~ /w.$/'

Solution 4

-f will test for a file

-w will test whether it's writeable

Example:

$ for f in *; do [ -f $f ] && [ -w $f ] && echo $f; done

Solution 5

If you are in shell use

find .  -maxdepth 1 -type f -writable

see man find

You will find you get better answers for this type of question on superuser.com or serverfault.com

If you are writing code not just using shell you may be interested in the access(2) system call.

This question has already been asked on serverfault

EDIT: @ghostdog74 asked if you removed write permissions for this file if this would still find the file. The answer, no this only finds files that are writable.

dwaters@eirene ~/temp
$ cd temp

dwaters@eirene ~/temp/temp
$ ls

dwaters@eirene ~/temp/temp
$ touch newfile

dwaters@eirene ~/temp/temp
$ ls -alph
total 0
drwxr-xr-x+ 2 dwaters Domain Users 0 Mar 22 13:27 ./
drwxrwxrwx+ 3 dwaters Domain Users 0 Mar 22 13:26 ../
-rw-r--r--  1 dwaters Domain Users 0 Mar 22 13:27 newfile

dwaters@eirene ~/temp/temp
$ find .  -maxdepth 1 -type f -writable
./newfile

dwaters@eirene ~/temp/temp
$ chmod 000 newfile

dwaters@eirene ~/temp/temp
$ ls -alph
total 0
drwxr-xr-x+ 2 dwaters Domain Users 0 Mar 22 13:27 ./
drwxrwxrwx+ 3 dwaters Domain Users 0 Mar 22 13:26 ../
----------  1 dwaters Domain Users 0 Mar 22 13:27 newfile

dwaters@eirene ~/temp/temp
$ find .  -maxdepth 1 -type f -writable

dwaters@eirene ~/temp/temp
Share:
49,219
vehomzzz
Author by

vehomzzz

He adapts a lazy approach to a complex learning. His eclectic personality coupled with eccentric character caused much harm to masses, and to himself.

Updated on June 26, 2021

Comments

  • vehomzzz
    vehomzzz about 3 years

    I want to quickly identify all writable files in the directory. What is the quick way to do it?