Deepak Rawat
Deepak Rawat

Reputation: 37

Delete File having write permission in linux

I want to create a shell script to delete all files and sub-directories having specific permissions like only write permission to all users or write and read permission.

I tried this

    echo "deleting files with write permission"
    for file in [find -perm -222] 
do
 rm $file
 done
   echo "FIles Deleted"

Upvotes: 0

Views: 1300

Answers (1)

tripleee
tripleee

Reputation: 189427

Your syntax is off; [ does not run a new command, it just tests whether its argument is non-empty.

find itself has an option to delete things it finds, so probably use that instead.

find -type f -perm -222 -print -delete

I'm guessing you only want regular files (-type f) though -delete can handle directories, too. The -print causes the files to be printed before being deleted.

(Actually your syntax simply loops over the tokens [find, -perm, and -222]; the arguments to for are just strings. If you wanted to run a command there the syntax would be for file in $(command ...) but this is bad practice too because it will break if command outputs a file name which is not a single token.)

Upvotes: 2

Related Questions