Execute command in all immediate subdirectories

Can you try using this simple loop which loops in all sub-directories at one level deep and execute commands on it,

for d in ./*/ ; do (cd "$d" && ls -al); done

(cmd1 && cmd2) opens a sub-shell to run the commands. Since it is a child shell, the parent shell (the shell from which you’re running this command) retains its current folder and other environment variables.

Wrap it around in a function in a proper zsh script as

#!/bin/zsh

function runCommand() {
    for d in ./*/ ; do /bin/zsh -c "(cd "$d" && "$@")"; done
}

runCommand "ls -al"

should work just fine for you.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)