Under Linux, how can I find all the files and directories that are writable (or, actually, not writable) by a particular user?
Edit: To clarify, I meant under a particular subdir, not systemwide. And yes, that means all the permutations and combinations of user, group and world writability that would allow that user to write. I know what the question entails semantically, I was hoping for a one- or few-liner to execute to get a list of these files.
Use the 'find' command if you have findutils version 4.3.0 or greater installed:
For all files under the current directory that are writable by the current user:
For all files under the current directory that are not writable by the current user:
According to the man page:
You can create a Perl script (
writable.pl
) like this:and then use this script, as root, as follows:
filling in
USERNAME
andDIRECTORY
as appropriate.I'll use the gnu find syntax for the -perm flag in this example:
Basically -- if you throw out wacky extensions like ACLs, you've got 3 chocies - owner, group, and "other" write access. Sounds like a job for a loop.
There is plenty of room to optimize this but I'll leave that to someone else... Also, I never can remember all the details of find and crossing filesystems and that sort of nonsense. Also, make sure the output of groups is the same as on my test linux system
This is a rough example of how you'd find files writable by a user. This will when run as any user, but if you run it as a non-uid0 user you'll only find things that are in directories that the user running the script has both read and execute permissions to.
This command should find all writable directories, you can change the permissions as you see fit:
For Eddie's answer if you throw in:
Then it will traverse directories with spaces in their name as well.
Are you sure that is really the question that you want to be asking?
To say "I want to see all files that X account can write to" means every file owned by them with u+w, every file owned by any group they belong to that's set g+w, and every file world writable (o+w).
Not writable would be even harder. You'd be better off to make a list of every file, then exclude those that they can write to.
I'm not sure if this is the best way, but should do what you ask:
key is of course in
-w
switch, which can also be negatedEDIT: Thinking more about this, this script prints what is writable by current user, it obviously wouldn't work for some given user.
Linux
test -w /foldername/filename
can check whether current user has write access to file or not. The process returns 0 in case has, or non-zero if doesn’t.As admin or root you can login on behalf of a user, and combine
test
command withfind
command, so list of command will look like following:It quite bit slow to run every file through
test -w
however will check if it is writeable for sure