Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

This script is not working the way I thought it would .
I though it would find all the scripts that have every one rwx permissions changed to the permissions of xx5

#!/bin/bash
# the / makes find inclusive


for file in `find . -perm /007 `
do
permissions=`stat -c %a $file`
permissions=${permissions:0:2}5
echo $permissions $file
chmod $permissions $file
done



#find . -perm /007

My intentions are to at least secure some files

share|improve this question
1  
How is it not working the way you thought it would? Are you getting any errors? Try running it with bash -x <yourscript> to trace the execution. –  Herman Torjussen May 17 '13 at 21:32
    
You are sure that you want to change only o=rwx to o=rx and not simply take away the write access for others i.e. making o=rw o=r? –  Hauke Laging May 17 '13 at 23:33
    
Sounds like you want chown -R o-w . –  Stephane Chazelas May 18 '13 at 6:31
    
@StephaneChazelas The point is to limit your chown call to the necessary files. May be a huge waste of time otherwise. –  Hauke Laging May 18 '13 at 22:45
    
Somw quick thoughts: Do your file paths have spaces? Do you have permission to chmod the files? What error are you getting? –  Benubird May 22 '13 at 13:29
add comment

3 Answers

chmod -R o-w .

Will remove write permissions to others for every file in a safe way. It will however update the ctime of every file including the ones for which others already didn't have write access.

With GNU chmod, you can make it show which files needed updated with the -c option:

$ chmod -cR o-w .
mode of `./a' changed from 0777 (rwxrwxrwx) to 0775 (rwxrwxr-x)

If you only want to remove w from others where they have rwx, you can do:

find . ! -type l -perm -7 -exec chmod o-w {} +

(and add -vv with GNU or FreeBSD chmod to see the details). But (especially since you're dealing with world writeable files) beware there's a race condition there, where someone with bad intentions having write access to any of the directories under there could replace a file with a symlink to some other file in between the time find checks it and chmod is executed and have you remove the write permissions to that file instead and cause havoc (think of a symlink to /tmp for instance).

Since you're using GNU stat syntax, chances are your find is the GNU find. Note that GNU find could display file attributes long before GNU stat was introduced so you don't need both.

find . ! -type l -perm -7 -printf '%m %p\n' -exec chmod o-w {} +
share|improve this answer
add comment

/007 will show your only files that have no permissions for owner and group, and all permissiosn (rwx) for other.

You might have more luck with /o=rwx. That will match only the other permissions for the file.

EDIT FOR CORRECTNESS:

Apparently, you'll need to use -perm -o=rwx, because the /o is an inclusive filter, and would match files where other has x,w,xw,r,rx,rw, or rwx permissions. Changing the leading character of the filter from / to -, makes the filter exclusive, and would only match files with rwx in the other field.

share|improve this answer
    
can you explain /0=rwx –  James Andino May 17 '13 at 21:59
1  
You are wrong about /o=rwx. That matches e.g. files with o=r, too. -perm -o=rwx is needed. –  Hauke Laging May 17 '13 at 23:56
    
thanks for the correction. –  Tim Kennedy May 18 '13 at 0:21
    
@JamesAndino the /o=rwx filter is a permissions filter that looks for r(read), w(write),x(execute) bits being set. The o means to compare the filter to the other field. You could also specify u for user, or g for group. or combinations, like -perm -u=rwx,o=rwx to match on both the user and group permissions. –  Tim Kennedy May 18 '13 at 0:32
add comment

Your approach is a performance nightmare: You create two processes for every file! One completely uselessly because find already has this information and can easily print it. This is a better solution:

find . -perm -o=rwx -printf "%m %p_\0" 2>/dev/null | 
  while read -r -d '' perms path; do
    path="${path%_}"
    echo "${perms} '${path}'" >&2
    printf "%s\0" "$path"
  done | xargs -0 chmod o=rx
share|improve this answer
    
I just learned @ least 4 things, and 1 of them is I don`t kno what im doing –  James Andino May 18 '13 at 22:34
    
@JamesAndino Sorry, I didn't want to make it more complicated than necessary but then the edit by someone else about the \0 stuff appeared... My recommendation: Read all questions and answers about shell and shell tools for a week. You will feel different afterwards. :) –  Hauke Laging May 18 '13 at 22:57
    
no, no! It was a compliment @ you.Ty. –  James Andino May 19 '13 at 1:02
    
|| any one, Could you point me @ a set of reasonable bash scripts for doing things like saving files moving things around, along the lines of your answer. I know that I have a lot of garbage habits in shell from oogling shity code. PLZ help! –  James Andino May 19 '13 at 2:41
    
@JamesAndino If you don't get a good answer to that here then you should make that a separate question. I can offer this: shelldorado.com –  Hauke Laging May 19 '13 at 2:54
show 3 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.