6

I am trying to automate the deployment of a webapp under development that is frequently updated. The webapp comes in the form of a zip file with unknown name, and this directory structure:

unknown_name.zip
  └── unknown_folder_name
     └──all_the_application_files

I need a bash script to:

  • remove all the contents of /var/www/ [done]
  • uncompress the unknown_name.zip archive, in a given tmp path [done]
  • cd inside that known tmp path [done]
  • cd inside the unknown_folder_name extracted from the unknown_name.zip
  • move all_the_application_files to /var/www [easy if we solve previous bullet]

Important: inside the tmp directory there will be only one directory, the one I want to go inside. My current bash script:

rm -r /var/www/*
unzip ./\*.zip \* -d /home/lese/tmp-deploy
cd /home/lese/tmp-deploy
# HERE I WOULD cd unknown_folder_name
mv * /var/www/
2
  • Environment variables may help with this. Unfortunately, to use one, it would already need to have been set, which means someone, either you or someone else, needs to have known the folder already. Also, there's a chance your customer could unset the variable before your script runs, purposefully or by accident.
    – trysis
    Apr 25, 2014 at 19:37
  • rm -r /var/www/* Gah!!! What if one of the commands fails for some stupid reason beyond your control? Then you're left without /var/www. Better to mv /var/www /var/www_$(date +"%F_%T") && mkdir /var/www.
    – RonJohn
    Mar 3, 2018 at 3:52

1 Answer 1

18

Solution

If you know for sure there's exactly one directory and nothing else (no other folder and no other file) in the current directory, you can enter that directory with

cd *

If you know that there is only one directory, but there might be non-directory and non-symlink-to-directory files in the current directory, you can use

cd */.

to select only the directory.

Explanation

A single * is expanded by the shell to a list of all file names (which includes subdirectories) in the current directory (excluding hidden files, especially the directories . and .. which are always there). If the only thing in the current directory is the subdirectory to enter, this will expand to

cd unknown_folder_name

and thus do what you want.

The trick with */. is that this expands to the "self directory" (named .) in any subdirectory, which is of course the subdirectory itself. Since file names can never contain a /, and by assumption there's only one directory, the only name it expands to will be unknown_folder_name/., which is of course the same directory as unknown_folder_name.

1
  • Great man! it is what i was looking for, thanks!
    – lese
    Apr 25, 2014 at 13:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .