How to use curl command in Linux

29/12/2020
Chưa phân loại
The command curl function is to ease file transference between devices through protocols DICT, FILE, FTP, FTPS, GOPHER, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMB, SMBS, SMTP, SMTPS, TELNET or TFTP.

If you know your file remote location you can download it with a single command order. Curl supports authentication and encryption. This tutorial will explain how to download files using cURL, how to upload files using cURL, how to resume interrupted downloads or to use a proxy when downloading files among other tips.

If curl isn’t installed in your Debian/Ubuntu Linux distribution type:

apt install curl

For CentOS and Fedora run:

yum install curl

Downloading files using cURL:

Once installed we can start using to download a file from any website like wget. Type:

# curl -O http://ftp.us.debian.org/debian/pool/main/n/nano/nano_2.7.4-1_amd64.deb

% Total    % Received % Xferd  Average Speed   Time     Time    Time  Current
Dload  Upload   Total   Spent   Left  Speed
100     473k  100  473k     0    0   293k      0  0:00:01  0:00:01 –:–:–  293k

We can use cURL to download multiple files from multiple sources with a command:

# curl -O http://ftp.us.debian.org/debian/pool/main/n/nano/nano_2.7.4-1_amd64.deb-
http://ftp.us.debian.org/debian/pool/main/s/snort/snort_2.9.7.0-5_amd64.deb -O
https://www.snort.org/downloads/snort/snort-2.9.13-1.f29.x86_64.rpm

% Total          % Received % Xferd  Average Speed   Time     Time    Time  Current
Dload  Upload   Total   Spent   Left  Speed
100  473k  100  473k    0      0  271k        0  0:00:01  0:00:01 –:–:–  271k
100  825k  100  825k      0    0  1429k       0 –:–:– –:–:– –:–:– 1429k
100   474           0   474  0    0   74      0 –:–:–  0:00:06 –:–:–   124
root@LinuxHint:/home/linuxhint#

If when using Curl we don’t want to store the files in the working directory, use a low case -o parameter to specify the directory, you can also change the file’s name using this parameter:

# curl -o /home/linuxhint/w3af/Namewewant.deb http://ftp.us.debian.org/debian/pool/
main/n/nano/nano_2.7.4-1_amd64.deb
 
% Total         % Received % Xferd  Average Speed   Time     Time    Time  Current
Dload  Upload   Total   Spent   Left  Speed
100  473k  100  473k      0       0   313k    0  0:00:01  0:00:01 –:–:–  313k


Additionally to the “-O” parameter, we can resume interrupted downloads using the “-C -” parameter.

You can try downloading nano or any file and interrupt the download pressing ctrl+c:

# curl -O http://ftp.us.debian.org/debian/pool/main/n/nano/nano_2.7.4-1_amd64.deb
 
% Total         % Received % Xferd  Average Speed Time  Time  Time  Current
Dload  Upload   Total   Spent   Left  Speed
3  473k          3 15828 0          0  24479      0  0:00:19 –:–:– 0:00:19 24463^C

Then resume the interrupted download using “curl -C – -O <http://destination/file>”:

# curl -C – -O  http://ftp.us.debian.org/debian/pool/main/n/nano/nano_2.7.4-1_amd64.deb
** Resuming transfer from byte position 151552
% Total          % Received % Xferd  Average Speed   Time     Time    Time  Curren
Dload  Upload   Total   Spent   Left  Speed
100  325k  100  325k      0          0   221k  0  0:00:01  0:00:01 –:–:–  221k

cURL also supports using proxy and authentication, to download a file using a proxy we need the -x parameter:

# curl -x 138.68.40.138:8080 -O  http://ftp.us.debian.org/debian/pool/main/n/nano/
nano_2.7.4-1_amd64.deb
 
% Total          % Received % Xferd  Average Speed  Time  Time Time Current
Dload  Upload   Total   Spent   Left  Speed
100  473k  100  473k      0          0   262k  0  0:00:01  0:00:01 –:–:–  261k

Note: you can find proxy servers for testing at https://free-proxy-list.net/

Uploading files using cURL:

To upload a file using cURL we need to use the –upload-file parameter.

curl –upload-file <file> <http://server>

You can test this command for uploading using https://transfer.sh/ free service to share files from the command line.

#  curl –upload-file nano_2.7.4-1_amd64.deb https://transfer.sh/nano.deb
# curl -O https://transfer.sh/hOlbR/nano.deb
% Total  % Received % Xferd  Average Speed   Time  Time Time Current
Dload  Upload   Total   Spent   Left  Speed
100  473k  100  473k  0  0   140k 0  0:00:03  0:00:03 –:–:–  140k

We just uploaded nano using curl, and downloaded it using cURL through the url provided by https://transfer.sh.

Uploading to an FTP server with authentication:

# curl -u <username>:<password> -T nano_2.7.4-1_amd64.deb ftp://ivanney.com
 
% Total % Received % Xferd  Average Speed   Time  Time Time Current
Dload  Upload   Total   Spent   Left  Speed
100  473k        0   0  100  473k    0 107k  0:00:04  0:00:04 –:–:–  107k

Note: image edited to hide the password.

As you see cURL is a great tool to share files from the command line and it supports many protocols.

I hope you found this tutorial useful to get your data fully removed, should you have any inquiry contact us opening a ticket support at LinuxHint Support. Keep following LinuxHint for more tips and updates on Linux.

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

OpenZFS backed NFS server: Part 1 — Creating a Server

If you are familiar with OpenZFS, you know all about its feature rich interface, flexible architecture, reliable checksums...
29/12/2020

How to install KDE Plasma 5.10.2 on Ubuntu 17.04

KDE Plasma 5.10.2 was recently released as a bugfix update to KDE Plasma 5. KDE Plasma 5.10 was released sometime in May...
28/12/2020

VPS US giá rẻ

DỊCH VỤ THUÊ VPS US Máy Chủ đặt tại Bang Dallas (TX) Hoa Kỳ đường truyền mạng 10Gbs tốc độ...
23/12/2020
Bài Viết

Bài Viết Mới Cập Nhật

Reliable IPv4 and IPv6 Subnet Rental Services: The Perfect Solution for Global Businesses
23/12/2024

Tìm Hiểu Về Thuê Proxy US – Lợi Ích và Cách Sử Dụng Hiệu Quả
11/12/2024

Mua Proxy V6 Nuôi Facebook Spam Hiệu Quả Tại Onetcomvn
03/06/2024

Hướng dẫn cách sử dụng ProxyDroid để duyệt web ẩn danh
03/06/2024

Mua proxy Onet uy tín tại Onet.com.vn
03/06/2024