The “curl” Command in Linux [9 Practical Examples]

LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now

The curl command in Linux stands for “Client URL”. It is a file transfer command-line tool powered by libcurl,  a free client-side URL transfer library. curl supports various network protocols and can operate without interacting with the user. The most common uses of the curl command include file downloading, uploading, troubleshooting, etc.

A. Description

The curl command in Linux is a free file transfer tool. This means it allows you to send or receive data over the network. It supports most of the network protocols ( HTTP, FTP, SFTP, SMTP, TFTP, POP3, TELNET, FILE, etc). You can use the curl command to transfer single or multiple files at once.

B. Syntax

The syntax for the curl command in Linux is really simple. The basic curl syntax is given below.

curl [options / URLs]
Note: In the above syntax “ / ” indicates that you can write URLs with or without options using the curl command

C. Options

The curl command in Linux comes with a busload of options. Some of the most useful options are described below. For any further details, you can always look at the man page.

man curl

Useful Options

  • -C – ( Continues interrupted downloads)
  • -I ( Outputs the HTTP header only )
  • –limit-rate <amount> ( Limits bandwidth/speed while downloading)
  • –libcurl <filename> ( output C source code of the URL )
  • -L ( Redirects if the requested page is moved)
  • -o (Downloads file with default different name)
  • -O, Upper case (Downloads file with a different name)
Note: The options in Linux CLI (Command Line Interface) are all case-sensitive, So be cautious while using them

Installing the “curl” Command in Linux

Most versions of Linux have the curl command pre-installed. You can find out whether you have it on your machine or not by typing the following.

curl --version

Your system will print out the version of the installed curl if you already have it installed.Checking curl version.Otherwise, you will get the following text:Checking for curl command in system.In this case, don’t be overwhelmed. You can install the curl package within a minute by following the instructions below.

Steps to Follow >

➊ At first open the Ubuntu Terminal

➋ Type the following in your command prompt:

sudo apt install curl

➌ Type your password

➍ Now, press ENTER

Output >

Upon completion of these four steps, curl will be installed on your machine.Installing curl command.

Practical Examples of the “curl” Command in Linux

Various applications are available using the curl command in Linux. You can control file transfers in many ways with curl. In this article, I will demonstrate the frequent uses of the curl command with practical examples.

Example 1: Displaying HTML Contents of URL on Terminal Using the “curl” Command in Linux

You can view the HTML contents of an URL using the curl command. Simply typing the URL after the curl command will output the HTML contents in your Terminal. In this example, I will show the contents of the homepage of the linuxsimply website on my command prompt. To do so you can follow the steps below.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

curl https://linuxsimply.com/

➌ Now, press the ENTER button.

Output >

In the image below, you can see that I have shown the HTML codes of the homepage of the linuxsimply website in my command prompt using the curl command in Linux.Displaying file content on terminal Using curl command i linux.


Similar Readings


Example 2:  Downloading Files in the Current Directory Under Default Name Using the “curl” Command in Linux

Using the curl command with the option -O (Uppercase), you will be able to download files under their default name. In this example, I will download the 50 Most Used Linux Commands.pdf file from the linuxsimply website to demonstrate the use of this option. You can follow the steps below to do so.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

curl -O https://linuxsimply.com/wp-content/uploads/2022/11/50-Most-Used-Linux-Commands-pdf.pdf

➌ Now, press the ENTER button.

Output >

In the image below, you can see that I have downloaded the desired file using the curl command in Linux. You can view the file by typing the ls command for the current directory.Downloading files under default name using curl command in linux.

Example 3: Downloading Files in the Current Directory Under a Given Name Using the “curl” Command in Linux

Using the curl command in Linux with the option -o (Lowercase), you will be able to download files under a different name. In this example, I will download the homepage of the linuxsimply website under the name “My_Linux.html”. You can follow the steps below to do so.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

curl -o My_Linux.html https://linuxsimply.com/

➌ Now, press the ENTER button.

Output >

In the image below, you can see that I have downloaded the desired file with the name “My_Linux.html”. You can view the file by typing the ls command for the current directory.Downloading file under a given name using curl command in linux.

Example 4: Downloading the C Source Code of the URL Using the “curl” Command in Linux

If you are a developer, the curl command in Linux will be very useful for you. You can download the C source code of a URL in your system with the option –libcurl. In this example, I will download the source code of the Glossary page from the linuxsimply website and save it in a file named “code.c”.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

curl https://linuxsimply.com/glossary/ –libcurl code.c

➌ Now, press the ENTER button.

Output >

In the image below, you can see that I have downloaded the source code of the Glossary page from the linuxsimply website with the name “code.c”. You can view the contents of the file by typing the cat command along with the filename.

Downloading C source code of a webpage using curl command in linux.

Example 5: Verifying HTTP Header Using the “curl” Command in Linux

Using the curl command in Linux with the option -I, you can view the information of the HTTP Header. The option also outputs an HTTP code that indicates if the server is Up or Down. In this example, I will display the Header information of the homepage of the linuxsimply website on my command prompt. To do so you can follow the steps below.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

curl -I https://linuxsimply.com/

➌ Now, press the ENTER button.

Output >

In the image below, you can see that I have shown the HTTP Header information of the homepage of the linuxsimply website in my command prompt using the curl command in Linux.Getting HTTP header Information using curl command in linux.

Example 6: Redirecting Automatically to Available Page If the webpage is Removed Using the “curl” Command in Linux

The curl command in Linux allows you to automatically redirect to an available page if it finds that the URL provided has been removed. You can do so by using the -L option. In this example, I will illustrate the usefulness of this command. You may use the command following the given process.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

curl -L https://linuxsimply.com/sample-page/

➌ Now, press the ENTER button.

Output >

In the image below, at first, you can see that the sample page of the linuxsimply website has been removed. But when I tried to fetch the removed URL with the option -L, it redirected me to the homepage of the linuxsimply website.Automatically redirecting from removed page using curl command in linux.


Similar Readings


Example 7: Limiting Bandwidth While Downloading Files Using the “curl” Command in Linux

Using the curl command with the option — limit-rate you can limit the downloading speed. In this example, I will download the “Untitled design.mp4” file from the linuxsimply website with a limited bandwidth of 50KBs. To do so you can go through the following steps.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

curl -- limit-rate 50k -O https://linuxsimply.com/wp-content/uploads/2022/12/Untitled-design.mp4

➌ Now, press the ENTER button.

Output >

In this image, you can see that I am downloading the mentioned file with an average bandwidth of almost 50KB/s using the curl command.Limiting download speed using curl command in linux.

Example 8: Resuming Interrupted Downloads Using the “curl” Command in Linux

You can resume your unfinished downloads with option -C – of the curl command in Linux. In this example, I retrieved the partially downloaded “file_exapmle.mp4” file. At first, I interrupted my ongoing download by pressing CTRL+C. Then, I resumed downloading using the -C – option. To do so you can go through the following steps.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

curl --limit-rate 50K -C - -O  https://file-examples.com/storage/fe8a7837bf63ad8783d6a5d/2017/04/file_example_MP4_1280_10MG.mp4

➌ Now, press the ENTER button.

Output >

In this image, you can see that the first downloading attempt was interrupted. Then I retrieved the partially downloaded file using the curl command.Resuming interrupted downloads using curl command in linux.

Example 9: Finding Word Meanings with Dict Protocol Using the “curl” Command in Linux

Using the curl command in Linux you can find the meaning or definition of any word from your command line. To get this facility you will have to use the DICT protocol along with the command. In this example, I will find the meaning of the word “search” from my command line. To do so you can follow the steps below.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

curl dict://dict.org/d:search

➌ Now, press the ENTER button.

Output >

In this image, you can see that I am viewing the meaning of the word “search”. The curl command also provides sentences with the word along with definitions from several sources.Searching meaning of a word from command line.

Conclusion

In this article, I have tried to show you the frequent uses of the curl command. You can always combine one or more options demonstrated here to get a full experience. I hope these practical examples will help you through your journey with the command line and make you a power user of Linux.


Similar Readings

Rate this post
Anonnya Ghosh

Hello there! I am Anonnya Ghosh, a Computer Science and Engineering graduate from Ahsanullah University of Science and Technology (AUST). Currently, I am working as a Linux Content Developer Executive at SOFTEKO. The strong bond between Linux and cybersecurity drives me to explore this world of open-source architecture. I aspire to learn new things further and contribute to the field of CS with my experience. Read Full Bio

Leave a Comment