At any given time my Linux server will have the following directory structure under /opt
:
/opt/
myapp/
<fileName>-<version>.zip
<fileName>/
derp.txt
staging/
Where <fileName>-<version>.zip
is a ZIP file with a "wildcard" name. One moment it might be fizz-1.0.1.zip
, in which case the file system would look like:
/opt/
myapp/
fizz-1.0.1.zip
fizz/
derp.txt
staging/
Then an hour later, the file system might look like:
/opt/
myapp/
buzz-2.11.15.zip
buzz/
derp.txt
staging/
I am trying to write a cleanup bash command that will scan the /opt/myapp
directory for ZIP files (*.zip
), rename them and move them to /opt/staging
, as well as to delete the derp.txt
file. For the rename, I just want to drop the hyphen and version number, so for instance transforming fizz-1.0.1.zip
into fizz.zip
. So for instance, running the script when the file system looks like:
/opt/
myapp/
fizz-1.0.1.zip
fizz/
derp.txt
staging/
Should result with:
/opt/
myapp/
fizz/
staging/
fizz.zip
I am guaranteed to only have 1 ZIP file inside /opt/myapp
at any given time. My best attempt is:
for file in *.zip; do
[[ -e $file ]] || continue
sudo mv "$file" /opt/staging/"${file%%-*}.zip"
sudo rm -rf /opt/${file%%-}/derp.txt
done
This performs the rename and moving of the ZIP file, but doesn't delete the derp.txt
. Any ideas as to where I'm going awry?
Are you sure there isn't a *
missing somewhere in ${file%%-}
? As is, it only removes a trailing -
, if any.