Linux Command Line Tips for Updating Files (and Directories)

I host many websites (most of them WordPress) and I have a plugin I add for my clients in /mu-plugins/.  It’s just some standard settings I want across all sites, including a menu item in the admin, like WPEngine, where my clients can click and read about NevTec Hosting, request help, etc.  I have a few sites per server.

As you know, it’s a standard folder structure…and updating my client’s /mu-plugins/functions.php file can be a pain to go to Transmit and go through each /siteA.com/wp-content/mu-plugins, /siteB.com/wp-content/mu-plugins, etc, and update the functions.php file if I update it.  Although I’m talking about these commands specifically for my WordPress setup, they can be used for any set up, including a non-website Linux set up – anywhere you’d want to update the contents from one file to multiple that follow the same hierarchy.

These took me a few hours to figure out how to get the commands to work as I was wanting.  Hopefully these tips can save someone some time!

 

Situation: Updating one file (functions.php) in /mu-plugins/ for multiple WordPress clients on the same server

If you have a unique file name, you can simplify the command slightly, but if you’ve ever used WordPress, you know there is a functions.php for any theme and potentially some plugins too.  I just wanted to target functions.php specifically in the /mu-plugins/ directory.

I updated one site’s /mu-plugins/functions.php and CDed to that directory in Terminal.  Running this command copies (and overwrites) the functions.php in the /mu-plugins/ directory for any of my other sites on this server.

Web server folder structure:

/var/www/siteA.com/wp-content/mu-plugins/functions.php
/var/www/siteB.com/wp-content/mu-plugins/functions.php
etc

find /var/www -path */mu-plugins/functions.php ! -path *$(pwd)* -exec sh -c 'cat functions.php > "$1"' -- {} \;
  • NOTE: The “!  -path *$(pwd)*” part of the command is saying exclude the current directory
  • Update the file you want on one site (/mu-plugins/functions.php in my case)
  • In the Terminal, CD to that directory
  • Run the command – it’s saying to look for other functions.php files in the /mu-plugins/ directory of your web folder, and it’s copying the contents of functions.php from the current site you are CDed to in the Terminal

 

Situation: Similar to above, but what if you have more than just one file you want to copy?

I ran into a situation where  I had some directories and files within those that I also wanted to copy to the other sites.

find /var/www -path */mu-plugins ! -path *$(pwd)* -exec cp -r * {} \;
  • Update the files you want on one site (/mu-plugins/ and all files and subdirectories in my case))
  • In the Terminal, CD to that directory
  • Run the command – it’s saying to look for other /mu-plugins/ directories in your web folder, and it’s copying the contents of the /mu-plugins/ directory from the current site you are CDed to in the Terminal

 

Situation: Deleting specified files from a specific hierarchy

This is another helpful command to delete any files and/or directories, but only within a certain folder structure.  Instead of running: rm -r test* (which would delete all files and directories starting with “test” from the current directory you’re in and any child directories, this command deletes only those “test” files and folders within a the /mu-plugins/ directory.

find /var/www -path */mu-plugins/test* -exec rm -R {} \;