I want to download a number of files which are as follows:
http://example.com/directory/file1.txt
http://example.com/directory/file2.txt
http://example.com/directory/file3.txt
http://example.com/directory/file4.txt
.
.
http://example.com/directory/file199.txt
http://example.com/directory/file200.txt
Can anyone help me with it using shell scripting? Here is what I'm using but it is downloading only the first file.
for i in {1..200}
do
exec wget http://example.com/directory/file$i.txt;
done
Before writing a shell script, we will see how to download a file directly using commands, then we will extend it to a script. The FTP commands for downloading files are “get” and “mget” which are used for downloading single or multiple files respectively. These commands should be entered inside an FTP prompt.
wget and curl are just two of the most popular commands for downloading files in Linux. There are more such command line tools. Terminal based web-browsers like elinks, w3m etc can also be used for downloading files in command line. Personally, for a simple download, I prefer using wget over curl.
In Windows, to download a file from the internet through the CLI, you must first install a tool that enables this functionality. There are several tools and commands for this purpose, including bitsadmin , wget , Invoke-WebRequest , and curl . We will be using curl in our example, due to its ease of use.
wget http://example.com/directory/file{1..200}.txt
should do it. That expands to wget http://example.com/directory/file1.txt http://example.com/directory/file2.txt ...
.
Alternatively, your current code should work fine if you remove the call to exec
, which is unnecessary and doesn't do what you seem to think it does.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With