简体   繁体   中英

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"

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.)

Try this code. If you get permission issues use sudo su .

echo "Deleting files with write permission"
for file in $(find -perm -222 -type f)
do
 rm $file
 done
echo "Files Deleted"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM