Take this path for example: /var/www/html.Inside the dir html, there are a few subdirectories. I need the system to go into each one of these sub-directories via cd (it must be cd), and then running a command when it is inside this dir, go back to /var/www/html and so repeat the process with the next dir.This is what I tried, but it seems ineffective:find /var/www/html/* -type d -maxdepth 0 ! -name 'phpmyadmin' -exec cd * && wp plugin update --all {} \;
Asking Bash to cd into each directory under some path and run a command when it is inside?
bash;shell script;scripting;find;cd command
Lets first take apart your command:find /var/www/html/* -type d -name 'phpmyadmin' -maxdepth 0 ! -exec cd * && wp plugin update --all {} \;What this is actually going to do is:The shell is going to expand /var/www/html/*. Lets assume it expands to /var/www/html/foo and /var/www/html/bar.The shell is going to expand * (the one in your cd *). Note that this is current working directory. Lets assume you have pop and tart.The shell is going to run find /var/www/html/foo /var/www/html/bar -type d -name phpmyadmin -maxdepth 0 ! -exec cd pop tart.find is going to throw an error about an unterminated command, because it saw -exec but no ; or +.So to be clear what's happening, the shell is breaking apart the line, and only passing part of what you meant to be an argument to find.The cleanest, and simplest way to do this with find is:find /var/www/html/* -type d -name phpmyadmin -maxdepth 0 -exec sh -c 'cd $1 && wp plugin update --all' -- {} \;This quotes the entire command, feeds it to sh, and then passes the directory as an argument.Also this is personal taste, but for stuff like this, I would say it's easier to not use find:for dir in /var/www/html/*/; do cd $dir && wp plugin update --all; done