×

wget Command in Linux/Unix with Examples

The wget command is a command-line utility in Linux that is used to download files from the internet. It supports various protocols including HTTP, HTTPS, and FTP and can be used to download files from a website, a FTP server, or any other location that is accessible via the internet. The wget command is widely used for downloading files, especially when the download needs to be automated or performed from a script.

1) To download a single file from a website, the following command can be used:

wget http://example.com/file.txt

This command will download the file "file.txt" from the website "http://example.com" and save it in the current working directory.

2) To download a file and save it to a specific location, the -P option can be used.

wget -P /path/to/directory http://example.com/file.txt

This command will download the file "file.txt" from the website "http://example.com" and save it in the directory "/path/to/directory".

3) To download a file and save it with a different name, the -O option can be used.

wget -O newfile.txt http://example.com/file.txt

This command will download the file "file.txt" from the website "http://example.com" and save it as "newfile.txt" in the current working directory.

4) To download multiple files at once, the URLs of the files can be specified in a text file, one per line and the following command can be used:

wget -i list.txt

This command will read the URLs from the file "list.txt" and download the corresponding files.

5) To download files from a FTP server, the following command can be used:

wget ftp://ftp.example.com/file.txt

This command will download the file "file.txt" from the FTP server "ftp.example.com". If a username and password are required to access the server, they can be specified with the --ftp-user and --ftp-password options.

wget --ftp-user=username --ftp-password=password ftp://ftp.example.com/file.txt

6) To download files from a website using a proxy server, the -e option can be used.

wget -e use_proxy=yes -e http_proxy=http://proxy.example.com:8080 http://example.com/file.txt

This command will download the file "file.txt" from the website "http://example.com" using the proxy server "proxy.example.com" and port 8080.

7) To limit the download speed, the --limit-rate option can be used.

wget --limit-rate=200k http://example.com/file.txt

This command will download the file "file.txt" from the website "http://example.com" with a maximum download speed of 200 kilobytes per second.

8) To download files that are behind a login page, the --user and --password options can be used.

wget --user=username --password=password http://example.com/file.txt

This command will download the file "file.txt" from the website "http://example.com" using the specified username and password.

9) To download files that are behind a login page using cookies, the --save-cookies and --load-cookies options can be used.

wget --save-cookies cookies.txt --post-data='username=myusername&password=mypassword' http://example.com/login
wget --load-cookies cookies.txt http://example.com/file.txt

This command will first login to the website "http://example.com" by sending the specified post data, and then save the cookies in a file called "cookies.txt". Then the second command will load the cookies from the file "cookies.txt" and download the file "file.txt" from the website.

10) To download files that are behind a login page using a .netrc file, the --netrc option can be used.

wget --netrc http://example.com/file.txt

This command will download the file "file.txt" from the website "http://example.com" using the credentials specified in the .netrc file.

11) To download a webpage and all its linked resources, the -r option can be used.

wget -r -p -k http://example.com/

This command will download the webpage "http://example.com" and all the linked resources, such as images, css, and javascript files. The -p option will download the necessary files to display the webpage offline and the -k option will convert the links in the downloaded files to point to the local copies of the resources.

12) To download files in a specific time schedule, the --limit-rate and --limit-time options can be used.

wget --limit-rate=200k --limit-time=60 http://example.com/file.txt

This command will download the file "file.txt" from the website "http://example.com" with a maximum download speed of 200 kilobytes per second and will only download for 60 seconds. This can be useful if you have a limited data plan or if you want to schedule downloads at specific times.

13) To download files using a specific number of connections, the --limit-rate and --no-clobber options can be used.

wget --limit-rate=200k --no-clobber http://example.com/file.txt

This command will download the file "file.txt" from the website "http://example.com" with a maximum download speed of 200 kilobytes per second and will not overwrite existing files of the same name. This can be useful for resuming incomplete downloads or for downloading multiple files at the same time.

14) To download files using a specific number of connections, the --limit-rate and --no-clobber options can be used.

wget --limit-rate=200k --no-clobber http://example.com/file.txt

This command will download the file "file.txt" from the website "http://example.com" with a maximum download speed of 200 kilobytes per second and will not overwrite existing files of the same name. This can be useful for resuming incomplete downloads or for downloading multiple files at the same time.

15) To download files recursively and preserving their original structure, the -r and -np options can be used.

wget -r -np http://example.com/directory/

This command will download all the files and directories within the directory "http://example.com/directory" and preserve the original file and directory structure. The -np option will not download files from parent directories.

In conclusion, the `wget` command is a powerful and versatile tool that can be used for a variety of tasks including downloading files, entire websites, and directories. It supports various protocols, can be used with proxy servers, and can handle login pages and cookies. Additionally, it can also be used to schedule downloads, limit download speed, and retry downloads if they fail. With its range of options and parameters, `wget` can be tailored to suit your specific needs, making it an essential tool for anyone who needs to download files from the internet.


Related Topics

Linux apt-get command

Apt-get is a command-line that helps to handle the packages in Linux. Its major task is to assist in retrieving data and packages from trusted sources for purposes of installation,...

6 minutes read.

grep Command in Linux/Unix with Examples

The grep command in Linux is a command-line utility that searches for patterns in text files. It stands for "global regular expression print" and it is used to search for...

5 minutes read.

adduser Command in Linux with Examples

What is the adduser command? The adduser command creates a new user, and this command may be used to add apre-existing user to a group that already exists. How to use the...

4 minutes read.

mv Command in Linux/Unix with Examples

The 'mv' command in Linux is used to move or rename files and directories. The command stands for "move" and it allows you to move files and directories from one...

3 minutes read.

Linux Distributions

Linux Distributions Linux OS is not like other operating systems. Linux can be modified according to the user’s requirements, and it is very flexible to any change. Linux distributions are often...

4 minutes read.

wget Command in Linux/Unix with Examples

The wget command is a command-line utility in Linux that is used to download files from the internet. It supports various protocols including HTTP, HTTPS, and FTP and can be...

5 minutes read.

telnet Command in Linux/Unix with Examples

The telnet command in Linux is a command-line utility that allows you to connect to a remote host using the Telnet protocol. Telnet is a network protocol that provides a...

3 minutes read.

sed Command in Linux/Unix with Examples

The 'sed' command in Linux is a powerful text processing tool that allows you to perform various operations on text files, such as searching, replacing, and manipulating text. The command...

3 minutes read.

find Command in Linux/Unix with Examples

The find command in Linux is a command-line utility that is used to search for files and directories in a file system. It is a versatile command that can be...

5 minutes read.

cp Command in Linux/Unix with Examples

The cp (copy) command in Linux is a command-line utility that is used to copy files and directories. It is one of the most basic and commonly used commands in...

5 minutes read.

chmod Command in Linux/Unix with Examples

The chmod command in Linux is a command-line utility that is used to change the permissions of files and directories. It stands for "change mode" and it is used to...

4 minutes read.

Linux pwd

The pwd command is used to print or display the name of the working or current directory. Some shells have their own version of the pwd with several features, in...

1 minute read.

Linux ls

The primary use case of the ls command is to list the content available in the directory. The default directory is the current directory of the user. The below screenshot...

5 minutes read.

ls Command in Linux/Unix with Examples

The ls command in Linux is a command-line utility that is used to list the files and directories in a directory. It is one of the most basic and commonly...

4 minutes read.

chown Command in Linux/Unix with Examples

The 'chown' command in Linux is used to change the ownership of a file or directory. The command stands for "change owner" and it allows you to change the user...

3 minutes read.

du Command in Linux/Unix with Examples

The "du" command in Linux (and other Unix-like operating systems) is used to estimate the space used by a file or directory. The du command stands for "disk usage," and...

4 minutes read.

unzip Command in Linux/Unix with Examples

The 'unzip' command in Linux is used to extract files from a ZIP archive file. The command allows you to decompress and extract the files and directories stored in a...

3 minutes read.

addgroup Command in Linux with Examples

What is the addgroup command? It is a command in Linux to create a new group using specified command lined and the system’s default values. How to add groups Adding a user group:...

4 minutes read.

tar Command in Linux/Unix with Examples

The 'tar' command in Linux is used to create, maintain, and extract files from archive files called tarballs. The command stands for "tape archive" and it allows you to combine...

4 minutes read.

Linux Bash

Linux Bash In this tutorial, we are going to learn what the shell is? And how many various shells we have, we have also covered most of the bash shell features....

4 minutes read.