×

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

Difference Between Linux and Windows

Linux vs. Windows Here in this tutorial, we are going to discuss several features of Linux and Windows. These features or characteristics help us to come to a conclusion about which...

5 minutes read.

cd Command in Linux/Unix with Examples

The 'cd' command in Linux is used to change the current working directory. The command stands for "change directory" and it is used to navigate the file system on a...

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.

Linux cd

Linux cd: As in GUI operating systems like Windows 10, we use the mouse to travel to various folders back and forth; similarly, we have the “cd” command in Linux...

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.

cut Command in Linux/Unix with Examples

The "cut" command in Linux (and other Unix-like operating systems) is used to remove sections from each line of a file or stream of data. The cut command is often...

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.

top Command in Linux/Unix with Examples

The 'top' command in Linux is a real-time system monitoring tool that displays information about the running processes and system resource usage. The command provides a dynamic, scrolling view of...

4 minutes read.

mkdir Command in Linux/Unix with Examples

The "mkdir" command in Linux (and other Unix-like operating systems) is used to create new directories. The "mkdir" command stands for "make directory" and it is used to create a...

3 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.

Linux Commands with Examples

Linux Commands with Examples All the Linux commands are executed on the terminal provided by the Linux system. This terminal is a command-line interface to implement and display command outputs. Linux...

10 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.

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.

Linux SCP Command

SCP stands for secure copy protocol. It is a command-line application for copying files and directories safely between two places. Command-line utilities are programs that operate on a computer's command...

3 minutes read.

ping Command in Linux/Unix with Examples

The "ping" command in Linux (and other Unix-like operating systems) is used to test the connectivity between two networked devices. The "ping" command sends a small packet of data, called...

4 minutes read.

sudo Command in Linux/Unix with Examples

The sudo command in Linux is used to run a command or set of commands with the privileges of another user, typically the root user. This allows users to perform...

3 minutes read.

How to change the ApacheHTTP port in Linux

What is an Apache HTTP server in Linux? When we talk about the most commonly used web servers, one of the web servers that cannot be missed is Apache. With the...

4 minutes read.

Rename Folder in Linux

Rename Folder in Linux In the Linux system, there is not any single dedicated command for renaming the files and directories. Mostly, the mv command is used extensively for the purpose...

3 minutes read.

wc Command in Linux/Unix with Examples

The wc command in Linux is a command-line utility that is used to count the number of lines, words, and characters in a text file or standard input. The name...

4 minutes read.