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

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

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

>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

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

Leave a Reply