How to download a file from the Internet using Bash?
Downloading files from the Internet is a common and essential task in programming and server administration environments. In this article, you will learn step by step how to download using Bash, specifically with the wget
command. This process is essential when generating installation scripts that need to obtain resources from the network.
What is wget
and how is it used?
wget
is a popular line command in Linux that allows you to download files from the Internet. This command is extremely useful in Bash scripts, as it makes it easy to automate downloads without user intervention.
To get started with wget
:
-
Check your environment: make sure you are in the terminal of a Linux-based operating system, preferably Ubuntu. In our case, the work will be done on an Ubuntu server.
-
Create a Bash script: You will need to create a .sh
file. In this case, we will name it 10-for-download.sh.
-
Start the script:
- Use
nano
or your favorite text editor.
- Press the
I
key to enter insert mode.
- Add the initial line
#!/bin/bash
so that the system recognizes the script as a Bash executable file.
-
Add a descriptive comment: write a comment explaining the purpose of the script. For example:
-
Print an informative title: This helps to maintain a clear structure in your script:
echo "Download information from the internet"
-
Type the wget
command: use wget
followed by the URL of the file you want to download. Example:
wgethttp://www.duess.apache.org/this.tomcat/apache-t omcat-8.5.35.zip
How to set up and run the script?
Once you have written the script, it is crucial to set the permissions and run the file:
-
Save the file: Press ESC
, followed by :wq
to save and exit.
-
Modify permissions: Use chmod
to make the script executable.
chmod +x 10-to-download.sh
-
Run the script: Simply run the script in the terminal to start the download:
./10-for-download.sh
How to verify the download and internet connection?
Once the script is executed, if everything is correct:
-
Internet connection: The wget
needs connectivity, which you can verify with a simple ping
to a trusted address, for example, ping google.com
.
-
Stop a process: If you need to stop the process, use Ctrl + C
.
-
Verify the downloaded file: Use the ls
command to list the files in the directory and confirm that the file was downloaded correctly.
ls
Additional considerations
-
Correct URL: Make sure that the URL of the file you want to download is correct. You can obtain it by right-clicking on the link in a browser and selecting "Copy URL" or "Copy Link".
-
Console Versatility: Unlike graphical interfaces, in the console there is a need to manually enter the URL.
With these steps, you now have the basis for using wget
in your Bash scripts to manage downloads efficiently. Keep exploring and improving your Bash scripting skills.
Keep practicing and discover more ways to automate your daily tasks with Bash!
Want to see more contributions, questions and answers from the community?