{"id":19666,"date":"2023-06-22T09:34:29","date_gmt":"2023-06-22T09:34:29","guid":{"rendered":"https:\/\/linuxways.net\/?p=19666"},"modified":"2023-06-22T09:35:59","modified_gmt":"2023-06-22T09:35:59","slug":"curl-command-on-linux-for-beginners","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/linux-commands\/curl-command-on-linux-for-beginners\/","title":{"rendered":"curl Command on Linux for Beginners"},"content":{"rendered":"<p>A command-line utility that allows you to transfer data using various network protocols, with which you can interact with websites, APIs and even communicate with different devices on your network \u2013 all from the comfort of your terminal window. That&#8217;s what the curl command can do for you. Isn&#8217;t it amazing?<\/p>\n<p>If you have recently stumbled upon it or have been scratching your head over it for a while, this article is just for you. We are set to delve into the curl command in Linux, with a particular emphasis on guiding beginners. We will uncover its nature and learn the methods to utilize it to its full potential.<\/p>\n<h2><strong>What is Curl?<\/strong><\/h2>\n<p>Curl is an acronym for Client URL. It is a tool that can be used from the command line to transfer data between your machine and a network server, employing any of the protocols it supports. These protocols include HTTP, HTTPS, FTP, FTPS, SCP, SFTP, DICT, and others. Basically, if there&#8217;s a common protocol, Curl probably supports it!<\/p>\n<p>Curl is especially popular for working with HTTP and HTTPS &#8211; which are the protocols web browsers use to access web pages. However, unlike a web browser, Curl doesn&#8217;t have a graphical user interface. Everything is done through the command line, which makes it very powerful for automation scripts and testing.<\/p>\n<h2><strong>Why Use Curl?<\/strong><\/h2>\n<ul>\n<li><strong>Simplicity and Flexibility<\/strong>: Curl is simple to use. Even with just the <strong>curl<\/strong> command followed by a URL, you can recover information. It also has a multitude of options that cater to various requirements, making it highly flexible.<\/li>\n<li><strong>Support for Multiple Protocols<\/strong>: As mentioned, Curl supports a wide array of protocols. This means that you can use it not just for web pages but for interacting with FTP servers, sending emails (SMTP), and much more.<\/li>\n<li><strong>Data Transfer<\/strong>: You can use Curl to download and upload files. Whether it&#8217;s a single file or content from an entire web page, Curl has got you covered.<\/li>\n<\/ul>\n<h2><strong>Installing Curl<\/strong><\/h2>\n<p>Now that we understand what Curl is and why it&#8217;s an essential tool for handling web data, it&#8217;s time to get it installed on our Linux systems. Curl is widely supported and can be installed on various Linux distributions.<\/p>\n<h3><strong>On Debian-based Systems<\/strong><\/h3>\n<p>If you are operating on a Debian-based Linux distribution, like Ubuntu or Debian itself, the curl installation can be done using the <strong>apt-get<\/strong> command. Install curl by executing:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">sudo<\/span> <span class=\"kw2\">apt-get install<\/span> curl<\/div><\/div>\n<p><strong><em><img loading=\"lazy\" decoding=\"async\" width=\"717\" height=\"218\" class=\"wp-image-19667\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/06\/word-image-19666-1.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/06\/word-image-19666-1.png 717w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/06\/word-image-19666-1-300x91.png 300w\" sizes=\"auto, (max-width: 717px) 100vw, 717px\" \/><\/em><\/strong><\/p>\n<h2><strong>On Red Hat-based Systems<\/strong><\/h2>\n<p>In case you are using a Red Hat-based Linux distribution like CentOS or Fedora, the installation process is equally simple but uses a different package manager called <strong>yum<\/strong>. Install curl by executing:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">sudo<\/span> <span class=\"kw2\">yum install<\/span> curl<\/div><\/div>\n<h3><strong>Verify Installation<\/strong><\/h3>\n<p>In both cases, after installation is complete, you can check the Curl version to confirm it was installed correctly. The code is:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\">curl <span class=\"re5\">--version<\/span><\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"727\" height=\"184\" class=\"wp-image-19668\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/06\/word-image-19666-2.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/06\/word-image-19666-2.png 727w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/06\/word-image-19666-2-300x76.png 300w\" sizes=\"auto, (max-width: 727px) 100vw, 727px\" \/><\/p>\n<h2><strong>Basic Curl Commands<\/strong><\/h2>\n<p>Alright, let\u2019s now start with some of the basic commands that will get you up and running.<\/p>\n<h3><strong>1: Retrieve a Web Page<\/strong><\/h3>\n<p>Curl&#8217;s most basic application is to fetch a web page. Instead of entering a website address into your browser, you can use Curl to directly retrieve the same page from your terminal. Here\u2019s how you do it:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\">curl http:<span class=\"sy0\">\/\/<\/span>example.com<\/div><\/div>\n<p>This will display the HTML content of the page in your terminal. If you just want to check if the page is accessible and don&#8217;t want to see all the HTML code, you can use the <strong>-I<\/strong> option to fetch just the headers.<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\">curl <span class=\"re5\">-I<\/span> http:<span class=\"sy0\">\/\/<\/span>google.com<\/div><\/div>\n<p><strong><em><img loading=\"lazy\" decoding=\"async\" width=\"721\" height=\"127\" class=\"wp-image-19669\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/06\/word-image-19666-3.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/06\/word-image-19666-3.png 721w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/06\/word-image-19666-3-300x53.png 300w\" sizes=\"auto, (max-width: 721px) 100vw, 721px\" \/><\/em><\/strong><\/p>\n<h3><strong>2: Download Files<\/strong><\/h3>\n<p>You can also use this command to download a file directly from the terminal. Use the <strong>-O<\/strong> option and specify the URL of the file.<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\">curl <span class=\"re5\">-O<\/span> http:<span class=\"sy0\">\/\/<\/span>example.com<span class=\"sy0\">\/<\/span>file.zip.<\/div><\/div>\n<p><em><img loading=\"lazy\" decoding=\"async\" width=\"726\" height=\"94\" class=\"wp-image-19670\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/06\/word-image-19666-4.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/06\/word-image-19666-4.png 726w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/06\/word-image-19666-4-300x39.png 300w\" sizes=\"auto, (max-width: 726px) 100vw, 726px\" \/><\/em><\/p>\n<h3><strong>3: Save Output to a File<\/strong><\/h3>\n<p>When you use Curl to fetch a web page, it displays the content in the terminal. If you want to save this content to a file, use the <strong>-o<\/strong> option.<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\">curl <span class=\"re5\">-o<\/span> output.html http:<span class=\"sy0\">\/\/<\/span>example.com<\/div><\/div>\n<p><em><img loading=\"lazy\" decoding=\"async\" width=\"723\" height=\"88\" class=\"wp-image-19671\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/06\/word-image-19666-5.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/06\/word-image-19666-5.png 723w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/06\/word-image-19666-5-300x37.png 300w\" sizes=\"auto, (max-width: 723px) 100vw, 723px\" \/><\/em><\/p>\n<p>This command will save the HTML content of the web page to a file called output.html.<\/p>\n<h3><strong>4: Limit the Transfer Rate<\/strong><\/h3>\n<p>There may be situations where you don&#8217;t want Curl to use all your bandwidth. Maybe you are doing something else that&#8217;s more important. Use <strong>&#8211;limit-rate<\/strong> to limit the transfer rate.<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\">curl <span class=\"re5\">--limit-rate<\/span> 100K <span class=\"re5\">-O<\/span> http:<span class=\"sy0\">\/\/<\/span>example.com<span class=\"sy0\">\/<\/span>largefile.zip<\/div><\/div>\n<p><em><img loading=\"lazy\" decoding=\"async\" width=\"726\" height=\"111\" class=\"wp-image-19672\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/06\/word-image-19666-6.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/06\/word-image-19666-6.png 726w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/06\/word-image-19666-6-300x46.png 300w\" sizes=\"auto, (max-width: 726px) 100vw, 726px\" \/><\/em><\/p>\n<p>This limits the download rate to 100 KB per second.<\/p>\n<h3><strong>5: Use a Proxy<\/strong><\/h3>\n<p>If, for some reason, you need to use a proxy, Curl can handle that too. Use the <strong>-x<\/strong> option followed by the proxy details.<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\">curl <span class=\"re5\">-x<\/span> http:<span class=\"sy0\">\/\/<\/span>proxyserver:port http:<span class=\"sy0\">\/\/<\/span>example.com<\/div><\/div>\n<p><em><img loading=\"lazy\" decoding=\"async\" width=\"725\" height=\"43\" class=\"wp-image-19673\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/06\/word-image-19666-7.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/06\/word-image-19666-7.png 725w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/06\/word-image-19666-7-300x18.png 300w\" sizes=\"auto, (max-width: 725px) 100vw, 725px\" \/><\/em><\/p>\n<h3><strong>6: Measure Time Taken<\/strong><\/h3>\n<p>Wondering how long a request takes? Curl can give you this information with the <strong>-w<\/strong> option.<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\">curl <span class=\"re5\">-o<\/span> <span class=\"sy0\">\/<\/span>dev<span class=\"sy0\">\/<\/span>null <span class=\"re5\">-s<\/span> <span class=\"re5\">-w<\/span> <span class=\"st_h\">'Total time: %{time_total}s\\n'<\/span> http:<span class=\"sy0\">\/\/<\/span>example.com<\/div><\/div>\n<p><em><img loading=\"lazy\" decoding=\"async\" width=\"729\" height=\"74\" class=\"wp-image-19674\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/06\/word-image-19666-8.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/06\/word-image-19666-8.png 729w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/06\/word-image-19666-8-300x30.png 300w\" sizes=\"auto, (max-width: 729px) 100vw, 729px\" \/><\/em><\/p>\n<h2><strong>Conclusion<\/strong><\/h2>\n<p>Curl is a powerful tool for working with web data. With its abundance of options, the curl command offers versatility and can be used in numerous distinct ways. It\u2019s especially valuable for developers and system administrators for tasks like downloading files, interacting with APIs, and troubleshooting network issues.<\/p>","protected":false},"excerpt":{"rendered":"<p>A command-line utility that allows you to transfer data using various network protocols, with which you can interact with websites, APIs and even communicate with different devices on&hellip;<\/p>","protected":false},"author":110,"featured_media":19675,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1049],"tags":[],"class_list":["post-19666","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux-commands"],"_links":{"self":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/19666","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/users\/110"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/comments?post=19666"}],"version-history":[{"count":0,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/19666\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media\/19675"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=19666"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=19666"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=19666"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}