Check that file is fully downloaded by FTP I have a task in which I want to handle error occurred in FTP downloading, For which I used the wget command to download a file via FTP and once the download is complete, compare downloaded filesize with FTP file size.

For that, I have the code as follows. My question: is this the correct method?

# fetch file details from the server and extract only number from the return value

FileSizeOnServer=curl --max-time 10 -u $FTP_USER:$FTP_PASSWORD --head $FTP_Path 2&gt;&amp;1 | grep "Content-Length:"
echo "Present File Size Details:" $FileSizeOnServer

FTP_File_Size=${FileSizeOnServer#*: }
echo "FTP_File_Size:$FTP_File_Size"

# fetch only size of local file size

DownloadedFileSize=`stat -c %s /home/pi/Desktop/ABCD.txt`
echo "DownloadedFileSize:$DownloadedFileSize"

# compare local and ftp file size

if [ "$DownloadedFileSize" -eq "$FTP_File_Size" ]; then
    echo "Size matched"
else
    echo "File Size doesn't match Start Downloading again"
fi
You definitely need more quoting of parameter expansions.  Notably, $FTP_PASSWORD may well contain characters that are meaningful to the shell:

FileSizeOnServer=$(curl --max-time 10 -u "$FTP_USER:$FTP_PASSWORD" \
                   --head "$FTP_Path" 2&gt;&amp;1 | grep "Content-Length:")


I'm not sure it's a good idea to redirect standard error there - you want errors to be visible, not piped into grep.

I'm not sure there's much value to the size comparison, though - curl should exit with non-zero status if it didn't transfer the whole file, so we should just use it directly:

if ! curl -u "$FTP_USER:$FTP_PASSWORD" "$FTP_Path"
then
    echo "Failed to download" &gt;&amp;2
    exit 1
fi


Or even simply

set -e
curl -u "$FTP_USER:$FTP_PASSWORD" "$FTP_Path"