Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Download files using Shell script

Tags:

linux

shell

unix

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
like image 306
sumit Avatar asked Oct 09 '13 23:10

sumit


People also ask

How do I download a file using shell script?

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.

How do I download a file from Linux shell?

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.

How do I download a file from the command line?

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.


1 Answers

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.

like image 125
Danica Avatar answered Sep 30 '22 09:09

Danica