Looking to write a script for my Linux Server so look for a specific folder name, then wget a file, and untar.

Where would I start with this? I know that it has to be written in bash, but I'm very unfamiliar with the language.

Looking to get it to wget into folder /home/*/public. The * indicating that it doesn't matter what folder, as long as public exists.

  • 2
    Something like cd /home/*/public && wget http://foo/bar.tar -O - | tar x? cd only uses the first argument, so as long as one such directory exists, the command should work. – muru Jul 14 '15 at 4:38
  • @muru (and for the OP) that's assuming shell expansion on * still gives rise to right output directory... otherwise that's a good suggestion. – h.j.k. Jul 14 '15 at 8:08
  • 1
    @h.j.k. well, since "it doesn't matter what folder as long as public exists', any expanded directory will be the right directory. – muru Jul 14 '15 at 8:16
  • Okay. So I was able to get some of the command down... but now let's say that I want it to be for directories that are newly created. this is what I have so far... test -d "/root/appname/*/public" || cd "/root/appname/hello/public" && wget https://wordpress.org/latest.tar.gz && tar xzf latest.tar.gz && mv wordpress/* . && rm latest.tar.gz && rmdir wordpress ........ so the point of this is to download wordpress, and extract it when a new folder from /home/[THIS FOLDER IS CREATED VIA WEB INTERFACE]/public is created. of course, I'd only want it for new directories – – jchan94 Jul 14 '15 at 9:56

This script will download bar.tar file in every "public" directory in /home/*/


for dir in /home/*/public/
    cd ${dir}
    wget http://foo/bar.tar -O - | tar x
    cd ${base}

Of course that can be done in many different ways, but this seems easiest to understand.

  • Would this script work for only newly created folders in the home folder? I would like that. Also, I'd like to adjust it to download & extract wordpress' latest.tar.gz into public. – jchan94 Jul 14 '15 at 9:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.