Upload to multiple directories at once

adeyjones

Well-Known Member
Apr 26, 2019
69
8
8
Merseyside, UK
cPanel Access Level
Root Administrator
Hi, I know this isn't specifically a cPanel/WHM question and more of a linux command (if that's right?) question, but I am wondering if there is a way of deleting multiple directories at once (which each have different parents) and then uploading and extracting a zip to multiple at once too?

I am basically trying to deploy a theme update to 70 Wordpress sites that I have in one account on my server, so I would like to delete:
*/wp-content/themes/Theme1
then download my update using:
wget -P */wp-content/themes https://url.to.download.from
extract all:
unzip */wp-content/themes/v5.0.1.zip
then delete the zip files:
rm -fv */wp-content/themes/v5.0.1.zip

Would this work using * as a wildcard? If not, is there any other way?

Thanks.
 

cPRex

Jurassic Moderator
Staff member
Oct 19, 2014
12,497
1,968
363
cPanel Access Level
Root Administrator
Hey there! Unfortunately it won't be that easy as each directory and file you create will need to have unique ownership based on the account it is under. While your command may work to get the files in place, it would have them all owned by root.

You may want to explore some options similar to this:

 

adeyjones

Well-Known Member
Apr 26, 2019
69
8
8
Merseyside, UK
cPanel Access Level
Root Administrator
Hi @cPRex - all of the directories are in the same account and would therefore have the same owner, so could I use terminal via WHM and do it as the account owner? (I'm not sure on the command required to change from root to account owner).
 

cPRex

Jurassic Moderator
Staff member
Oct 19, 2014
12,497
1,968
363
cPanel Access Level
Root Administrator
If it's all under the same account it will likely work well. Assuming the user has been granted shell access, you'll just want to access SSH as the user. If you're logged in as root you can do this with the following command:

Code:
su - username
where "username" is the specific cPanel user you're working with.

I would recommend running a test of these commands before you deploy them to live sites. For example, you could create a fake directory structure to ensure your wildcards work as expected.
 

adeyjones

Well-Known Member
Apr 26, 2019
69
8
8
Merseyside, UK
cPanel Access Level
Root Administrator
Perfect thanks, i'll let you know how I get on then. If it works well, it could actually make my life so much easier with updating plugins to these 70 sites via this method instead of having to log in to 70 dashboards! (Yes I know WP does auto-update, but I have this disabled to I can test updated before they're deployed, fixing issues and restoring backups would take longer than manually updating in the long run).
 
  • Like
Reactions: cPRex

adeyjones

Well-Known Member
Apr 26, 2019
69
8
8
Merseyside, UK
cPanel Access Level
Root Administrator
I have just tested some of this on my demo account, I can upload my zip manually via FTP to my root directory so I don't need the wget step.
However, prior to extracting, i'd like to rename the original theme directory rather than remove it, and I tried:
mv */wp-content/themes/Theme1 */wp-content/themes/Theme1-old
but I am getting a message that the file or directory does not exist.
I went right through to "themes" and did mv Theme1 Theme1-old and that worked, but then going back to public_html and trying again I still get the same message.
Any ideas?
 

cPRex

Jurassic Moderator
Staff member
Oct 19, 2014
12,497
1,968
363
cPanel Access Level
Root Administrator
Try using the full path - /home/username/public_html/*/wp-content/......... or wherever that wildcard should be. Starting that command with a wildcard likely isn't going to work well.

You can also so a simple 'ls' to list the output before you apply the mv command to it, to make sure you're getting the correct list.
 

adeyjones

Well-Known Member
Apr 26, 2019
69
8
8
Merseyside, UK
cPanel Access Level
Root Administrator
With a bit of trial and error, even as root user and not account owner, i've come to the conclusion that it accepts the first wildcard (to find the directory it needs to rename) but it doesn't like the second wildcard (the destination for the renamed directory).

Logged in as root, I just tried:
mv /home/surgeryweb/public_html/*/wp-content/themes/Theme1 /home/surgeryweb/public_html/*/wp-content/themes/Theme1-old

and it gave me:
mv: cannot move ‘/home/surgeryweb/public_html/newdemo/wp-content/themes/Theme1’ to ‘/home/surgeryweb/public_html/*/wp-content/themes/Theme1-old’: No such file ordirectory
Notice it has replaced the first * with the correct directory name (the first one it has come to) but doesn't like the second one.
 

cPRex

Jurassic Moderator
Staff member
Oct 19, 2014
12,497
1,968
363
cPanel Access Level
Root Administrator
I'm not sure off the top of my head the best way to handle this one. It might be better to use "find" to generate the list and then populate the files with a for loop. If this is something you need to do frequently it would be worth the time to invest to come up with a good system for sure, but it's just going to be a matter of trial and error. I don't think you'll be able to do it with one "mv" command using multiple wildcards, though.
 

adeyjones

Well-Known Member
Apr 26, 2019
69
8
8
Merseyside, UK
cPanel Access Level
Root Administrator
I think i've cracked this, I installed Bash completion via terminal, created an executable and used the following:

for i in ./*/
do
if [ "$i" == "./cgi-bin/" ] ; then
continue
fi
cd "$i";
echo $PWD;
cd wp-content;
echo $PWD;
cd ..;
echo $PWD;
cd ..;
done


I had to put in the condition to ignore the cgi-bin directory as the loop broke when it got to that one. But it is going in and out of the directories correctly so I can now remove the echo's and put in my wget, mv, unzip and rm commands.