garbanoti

How to use curl command in Linux

How to use curl command in Linux
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 ”:

# 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

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

Kaip naudoti „GameConqueror Cheat Engine“ sistemoje „Linux“
Straipsnyje pateikiamas „GameConqueror“ apgaulės variklio naudojimo „Linux“ vadove. Daugelis žaidėjų, žaidžiančių sistemoje „Windows“, dažnai naudoja ...
Geriausi „Linux“ žaidimų konsolių emuliatoriai
Šiame straipsnyje bus išvardyta populiari žaidimų konsolių emuliavimo programinė įranga, skirta „Linux“. „Emuliacija“ yra programinės įrangos suderina...
Geriausi „Linux“ žaidimų distristai 2021 m
„Linux“ operacinė sistema nuėjo ilgą kelią nuo originalios, paprastos, serverio išvaizdos. Ši OS pastaraisiais metais nepaprastai patobulėjo ir dabar ...