Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Loop through a directory and perform action on files with specific permissions in unix

I want to loop through a directory with many subdirectories that have hidden files. I want to loop the directory and open only files that have some certain permission, "drwx—–T+" in this case.

My current script is

#!/bin/sh
cd /z/vendors  #vendors has a list of directories which contain hidden files
for FILE in *; do
    if [ <what should i put here to select files with permission "drwx-----T+">]; then
        cd "$FILE" #and do something here e.g open the hidden files
        cd ..
    fi
done

I don’t know what test condition to use, I know the command ls -l | grep "drwx-----T+" will list the files I need but how can I include this in the if test

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The exit status of grep indicates whether the input matched the pattern. So you can use it as the condition in if.

if ls -ld "$FILE" | grep -q -F 'drwx-----T+'; then
    # do what you want
fi

The -q option prevents grep from printing the match, and -F makes it match a fixed string rather than treating it as a regular expression (+ has special meaning in regexp).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading