I'm trying to add a shell function (zsh) mexec
to execute the same command in all immediate subdirectories e.g. with the following structure
~ -- folder1 -- folder2
mexec pwd
would show for example
/home/me/folder1 /home/me/folder2
I'm using find
to pull the immediate subdirectories. The problem is getting the passed in command to execute. Here's my first function defintion:
mexec() { find . -mindepth 1 -maxdepth 1 -type d | xargs -I'{}' \ /bin/zsh -c "cd {} && $@;"; }
only executes the command itself but doesn't pass in the arguments i.e. mexec ls -al
behaves exactly like ls
Changing the second line to /bin/zsh -c "(cd {} && $@);"
, mexec
works for just mexec ls
but shows this error for mexec ls -al
:
zsh:1: parse error near `ls'
Going the exec route with find
find . -mindepth 1 -maxdepth 1 -type d -exec /bin/zsh -c "(cd {} && $@)" \;
Gives me the same thing which leads me to believe there's a problem with how I'm passing the arguments to zsh. This also seems to be a problem if I use bash: the error shown is:
-a);: -c: line 1: syntax error: unexpected end of file
What would be a good way to achieve this?
Unix recursive directory listing command/tmp/dir1 – Linux or Unix Directory to search and list files recursively. -print – List file names. -ls – Show current file in ls -dils (ls command) format on screen.
The cd command can be used to change into a subdirectory, move back into the parent directory, move all the way back to the root directory or move to any given directory.
To change to a subdirectory, type cd, a space, and the name of the subdirectory (e.g., cd Documents) and then press [Enter]. To change to the current working directory's parent directory, type cd followed by a space and two periods and then press [Enter].
How To Run a Command Multiple Times in Bash. Wrap your statement for i in {1..n}; do someCommand; done , where n is a positive number and someCommand is any command. To access the variable (I use i but you can name it differently), you need to wrap it like this: ${i} . Execute the statement by pressing the Enter key.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With