Find the answer to your Linux question:
Results 1 to 3 of 3
How can I iterate over all the files in the current directory to check for certain permissions? This is what I have: Code: #!/bin/bash for file in *.tar.gz do if ...
  1. #1
    Just Joined!
    Join Date
    Jan 2011
    Posts
    2

    Bash script: make sure all files have certain permissions

    How can I iterate over all the files in the current directory to check for certain permissions? This is what I have:

    Code:
    #!/bin/bash
    for file in *.tar.gz
    do
        if [ -r "$file" ]
        then
            echo "$file is readable"
        else
            echo "$file is NOT readable"
        fi
    done
    But this only checks that the current user has read permissions for each file. I want to check that the group "others" has read permissions for each file. How can I do this? Is there a built in function to check if a file has read permissions for the "others" group?

    Otherwise, I thought I might be able to use this
    Code:
    $ stat --format=%a file
    744
    And parse the output "744" and make sure the 3rd number is between 4 and 7 (since the octals 4-7 have read permissions for others)

    Thanks

  2. #2
    Linux Guru
    Join Date
    Nov 2007
    Posts
    1,695
    Read:

    Code:
    man find

  3. #3
    Just Joined!
    Join Date
    Jan 2011
    Posts
    2
    Beautiful. I ended up using this script:

    #!/bin/bash



    non_readable=`find . -name '*.tar.gz' ! -perm -a=r -printf "%f "`

    if [ ${#non_readable} -gt 0 ]; then

    echo "These files do not have read permissions for all:"

    ls -l ${non_readable}

    fi

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...