Issue
I'm trying to write a bash script that will download the contents of a URL (not recursive) and then analyze the file that was downloaded.
If the downloaded file is a text file (i.e index.html) I want to know the size of the file and count the number of characters within that file.
If the file is an image file I just want to know the file size.
Right now I'm working with wget
and downloading the contents of the input URL, but the problem is that when I do this inside my script I don't know the file name of the file that was downloaded.
So, the two main question are:
- How can I get the filename in my script after using
wget
to perform some analyzing operations on the file? - How can I deterime the file type of the downloaded file?
Solution
I did finally manage to solve it.
#!usr/bin/env bash
URL="$1"
FILENAME=$(date +%y-%m-%d-%T) #Set the current date and time as the filename
wget -O "$FILENAME" "$URL" #Download the content from the URL and set the filename
FILE_INFO=$(file "$FILENAME") #Store the output from the 'file' command
if [[ "$FILE_INFO" == *"text"* ]]
then
echo "It's a text file"
elif [[ "$FILE_INFO" == *"image"* ]]
then
echo "It's an image"
fi
Special thanks to Ben Scott for the help!
Answered By - azemi Answer Checked By - Candace Johnson (WPSolving Volunteer)