简体   繁体   中英

Bash script to recursively add a file to multiple directories

I have a bunch of directory's in /home/servers2 (like a few thousand) and in each directory in server2 there is a directory called plugins Ex. /home/servers2/MyServer/plugins

In each one right now there is a file called AutoStopFinal.jar, I need to remove that, and then copy over AutoStop1.3.001 to the plugins directory from /home/servers/template/plugins. I wanted to non destuctivly test it so I tried this:

echo " Script to copy a plugin to all server plugin directories"

for i in /home/servers2/*/plugins; do
    echo rm -f $i/AutoStopFinal.jar
    echo cp -f /home/servers/template/plugins/AutoStop1.3.001.jar "$i"
done

echo " completed script ..."

But only got Script to copy a plugin to all server plugin directories. I Controled C'd out of it after about 5 minutes. But I got no echos from inside the loop, any advice?

You can probably use find to do this for you like this:

cd /home/servers2
find . -type d -name plugins -exec cp -f /home/servers/template/plugins/AutoStop1.3.001.jar {} \; -exec rm -f {}/AutoStopFinal.jar \;

This command will remove the files:

find ./servers2 -name AutoStopFinal.jar |xargs -n1 rm

This command will copy the new file into place:

find ./servers2 -name plugins |xargs -n1 cp ./servers/template/plugins/AutoStop1.3.001.jar

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM