I have a directory /home/ubuntu/uploads . I need to check if it contains any zip file. if it contains then I need to move the oldest modified file according to date to a different directory called /home/ubuntu/temp. Then I need to unzip the zip file we got in /home/ubuntu/temp to a directory called /home/ubuntu/s3. And at last I need to mark the file as successfully moved.
Here is what I am doing in bash script ->
#!/bin/bash if [[ -z `ls -t /home/ubuntu/uploads/*.zip | tail -1` ]] ; then echo 'No new zip file to move !' exit fi TIME=`date +%Y-%m-%d_%H-%M-%S` UPLOADS='/home/ubuntu/uploads' TEMP='/home/ubuntu/temp' BUCKET='/home/ubuntu/s3' FILENAME=`ls -t /home/ubuntu/uploads/*.zip | tail -1 | xargs -n 1 basename ` FILEPATH=`ls -t /home/ubuntu/uploads/*.zip | tail -1` cp $FILEPATH $TEMP unzip -u $TEMP/"$FILENAME" -d $BUCKET rm -rf $TEMP/* mv /home/ubuntu/uploads/"$FILENAME" /home/ubuntu/uploads/"$FILENAME".success.$TIME
It kind of works but it is not able to handle zip filenames containing space.
If someone can suggest a better and improved version of this please