vehomzzz
vehomzzz

Reputation: 44588

Find all writable files in the current directory

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

Upvotes: 33

Views: 60082

Answers (11)

Reinaldo Gil
Reinaldo Gil

Reputation: 628

Find files writeable by owner:

find ./ -perm /u+w

Find files writeable by group:

find ./ -perm /g+w

Find files writeable by anyone:

find ./ -perm /o+w

Find files with defined permission:

find ./ -type -d -perm 0777
find ./ -type -d -perm 0755
find ./ -type -f -perm 0666
find ./ -type -f -perm 0644

Disable recursive with:

-maxdepth 1

Upvotes: 1

JaseC
JaseC

Reputation: 3209

If you want to find all files that are writable by apache etal then you can do this:

sudo su www-data
find . -writable 2>/dev/null 

Replace www-data with nobody or apache or whatever your web user is.

Upvotes: 0

syam
syam

Reputation: 562

I know this a very old thread, however...

The below command helped me: find . -type f -perm /+w

You can use -maxdepth based on how many levels below directory you want to search. I am using Linux 2.6.18-371.4.1.el5.

Upvotes: 0

Idelic
Idelic

Reputation: 15582

The problem with find -writable is that it's not portable and it's not easy to emulate correctly with portable find operators. If your version of find doesn't have it, you can use touch to check if the file can be written to, using -r to make sure you (almost) don't modify the file:

find . -type f | while read f; do touch -r "$f" "$f" && echo "File $f is writable"; done

The -r option for touch is in POSIX, so it can be considered portable. Of course, this will be much less efficient than find -writable.

Note that touch -r will update each file's ctime (time of last change to its meta-data), but one rarely cares about ctime anyway.

Upvotes: 1

ghostdog74
ghostdog74

Reputation: 342443

stat -c "%A->%n" *| sed -n '/^.*w.*/p'

Upvotes: 0

David Waters
David Waters

Reputation: 12028

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

Upvotes: 3

Paul R
Paul R

Reputation: 212979

-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

Upvotes: 3

Dennis Williamson
Dennis Williamson

Reputation: 360143

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)

Upvotes: 19

ghostdog74
ghostdog74

Reputation: 342443

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.$/'

Upvotes: 4

muruga
muruga

Reputation: 2122

for  var in `ls`
do
if [ -f $var -a -w $var ]
then
echo "$var having write permission";
else
echo "$var not having write permission";
fi
done

Upvotes: 1

matja
matja

Reputation: 4169

find -type f -maxdepth 1 -writable

Upvotes: 40

Related Questions