Move Script

moveFile.sh

#! /bin/bash
move_config=/home/tyson/Desktop/MoveScript/script/move.config
echo Move Script executed on `date`

file_type=$1
if [ -z $file_type ]; then
    echo Please provide a file type to be moved
    exit 1 
fi

file_move_config=$(grep -w ^$file_type= $move_config)
if [ -z $file_move_config ]; then
    echo IncorrectFile Type provided
    echo Kindly refere $move_config file to correct the input
    exit 1
fi

source=$(echo $file_move_config | awk -F'=' '{print $3}')
destination=$(echo $file_move_config | awk -F'=' '{print $2}')
archive=$(echo $file_move_config | awk -F'=' '{print $4}')
echo ---------------------------------------------
echo Source      : $source
echo Destination : $destination
echo Archive     : $archive

if [[ ! -d $source || ! -d $destination || ! -d $archive ]]; then
    echo Once of the source, destination or archive directory is missing hence cannot move the files
    echo Source      : $source
    echo Destination : $destination
    echo Archive     : $archive
    exit 1
fi

echo ---------------------------------------------

for entry in "$source"/* ; do
    if [ -f $entry ] ; then
        echo $entry
        copy_command="cp $entry $destination/$(basename $entry)"
        move_command="mv $entry $archive/$(basename $entry)_$(date '+%Y%m%d-%H%M%S')"
        echo $copy_command
        echo $move_command
        # [[ $copy_command ]] && [[ $move_command ]] || echo "Failed to move"
        ($copy_command) && ($move_command) || echo "Failed to move"
    fi
done

move.config

#Synatx: Name=Destination=Source=Archive
A=/home/tyson/Desktop/MoveScript/CommonDir/A=/home/tyson/Desktop/MoveScript/Files/A/Sent=/home/tyson/Desktop/MoveScript/Files/A/Archive
B=/home/tyson/Desktop/MoveScript/CommonDir/B=/home/tyson/Desktop/MoveScript/Files/B/Sent=/home/tyson/Desktop/MoveScript/Files/B/Archive
C=/home/tyson/Desktop/MoveScript/CommonDir/C=/home/tyson/Desktop/MoveScript/Files/C/Sent=/home/tyson/Desktop/MoveScript/Files/C/Archive

Leave a Comment