tags:

views:

300

answers:

8

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

+1  A: 

-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
Paul R
-1 useless without an example ...
lexu
Also useless without saying what command these options are meant to be applied to.
bignose
@lexu/bignose - it's a `bash` question. `-f` and `-w` are bash operators. What more do you need ?
Paul R
@Paul R: look at murugaperumal's reply .. (your explanation + his example would receive a +1 from me)
lexu
@lexu: this seems overly critical - my answer was perfectly valid and contained all the required information - anyone using *nix must expect conciseness bordering on terseness, or they won't get very far.
Paul R
I typed `-f` at the Bash prompt and got a command not found error. /sarcasm
Dennis Williamson
@all: example now added
Paul R
thx for the edit, -1 removed. sry, it won't let me upvote, dunno why?
lexu
@lexu - thanks - 3 upvotes and 3 downvotes now - I guess I'll have to settle for that... ;-)
Paul R
@Paul R, the criticism was because you gave options without saying what command you're referring to. Those options are not "bash operators"; they are options to the `test` command, also spelled as the `[` command.
bignose
+4  A: 

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
David Waters
I see the following error with your solution: find: invalid predicate `-writable'
vehomzzz
sorry had . in wrong place , fixed now, look at man find to see how to user this command
David Waters
if you remove all writable permission to a file and then use the command, does it still find the file? i am curious
ghostdog74
+7  A: 
find -type f -maxdepth 1 -writable
matja
It issues a warning about the order of options on my system.
Pavel Shved
which Linux are you using?
matja
+1 for maxdepth and type f, I have included these in my answer.
David Waters
-writeable is NOT supported.
vehomzzz
@Pavel, simply swap the `-type f` and `-maxdepth 1` to get rid of the message
gnibbler
if you remove all writable permission to a file and then use the command, does it still find the file? i am curious
ghostdog74
The GNU `find` command needs the starting directory as its first argument.
bignose
@bignose: No, it assumes the current directory. @ghostdog74: `touch 444 file` or `touch 555 file` or `chown somebodyelse:somebodyelse file` or many variations and it won't find it.
Dennis Williamson
@Dennis , that's what i thought. hence my question.
ghostdog74
A: 
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
muruga
don't use ls with for loop to parse file names. also quote your variables in the if/else test.
ghostdog74
+2  A: 

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.$/'
ghostdog74
that's great! can ypu explain this awk command pelase
vehomzzz
what does tilde mean?
vehomzzz
the tilde is awk's regex operator for "match". much like Perl's `=~` regex operator
ghostdog74
A: 

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)
Dennis Williamson
A: 
stat -c "%A->%n" *| sed -n '/^.*w.*/p'
A: 
Idelic
Have you tried `touch` on a read-only file as root or on a file that you own? The ability to update a timestamp does not imply that the file is writable.
Chris Johnsen
If you're root, read-only files on writable file systems are writable, so I don't see the problem there.
Idelic