×

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

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.

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.

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.

pwd Command in Linux/Unix with Examples

The "pwd" command in Linux (and other Unix-like operating systems) stands for "print working directory." When executed, it simply displays the current directory (or folder) that the user is currently...

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

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.

man Command in Linux/Unix with Examples

The man command in Linux is used to display the manual pages for a given command or system call. These manual pages provide detailed information on the command's usage, options,...

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

cat Command in Linux/Unix with Examples

The cat command in Linux is a command-line utility that is used to display the contents of a text file. It stands for "concatenate" and it is used to view...

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.

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.

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.

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

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.

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.

ssh Command in Linux/Unix with Examples

The ssh (Secure Shell) command in Linux is used to remotely access and control another computer or device over a network. It provides a secure and encrypted connection between the...

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

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.