0

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

  • So where does it actually fail? – DevilaN May 29 at 9:22
  • I need to know if this can be done easily with just one for loop in a standard form. Also check for bad corrupted zip files and remove them from source directory. – Anirban Dutta May 29 at 9:28
  • Can you update your question accordingly with proper statement of what question(s) you really have and what is expected behaviour of script? – DevilaN May 29 at 9:48
  • instead of ls better use find it can handle non-trivial file names. always use double quotes for file names – alecxs May 31 at 11:40
0

This worked ..thanks everyone !

#!/bin/bash

# logs last error
#exec 3>&1 4>&2
#trap 'exec 2>&4 1>&3' 0 1 2 3
#exec 1>log.out 2>&1

# check for file names with space and rename
for file in /home/ubuntu/uploads/*' '*
do
  if [ -e "${file// /_}" ]
  then
    printf >&2 '%s\n' "Warning, skipping $file as the renamed version already exists"
    continue
  fi

  mv -- "$file" "${file// /_}"
done

# check for zip files
if [[ -z  `ls -t /home/ubuntu/uploads/*.zip | tail -1` ]] ; then
    echo 'No new zip file to move !'
    exit
fi

# check for bad zip file and remove
shopt -s dotglob nullglob globstar
for file in /home/ubuntu/uploads/*.zip; do
    [[ -r $file ]] || continue
    unzip -t "$file" >/dev/null 2>&1 ||  rm -f "$file"
done


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

| improve this answer | |

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.