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

Cách sử dụng dmesg

Tổng quan Với các hệ điều hành Unix-like | Linux, khi máy tính đang khởi động, trong quá trình nhân...
30/12/2020

Hướng dẫn đổi mật khẩu máy chủ ảo VPS Windows 2012

Hiện nay có 2 cách thông dụng để đổi mật khẩu VPS Windows 2012, đó là thông qua giao diện quản trị...

How to Install and Use Write Note Taking App on Ubuntu

Write is a cross platform note taking app from Stylus Labs. Write don’t have a lot of features. It is a really simple...
29/12/2020