Upload Text File Using Curl One Liner
How to use the curlicue command for uploading and downloading files without user interaction
If you're considering writing a script that requires downloading (or uploading) files over a network, ane of your all-time friends will be the roll command.
The whorl command is quite useful and flexible. The intent of the tool is to transfer data, without user interaction, to or from a server, using ane of the many supported protocols. Information technology is that list of protocols that helps curl manage to be so flexible, as the command supports:
- DICT
- FILE
- FTP
- FTPS
- GOPHER
- HTTP
- HTTPS
- IMAP
- IMAPS
- LDAP
- LDAPS
- POP3
- POP3S
- RTMP
- RTSP
- SCP
- SMB
- SMBS
- SMTP
- SMTPS
- TELNET
- TFTP
That's a salubrious list of possibilities. It also ways coil can transfer just well-nigh whatsoever blazon of data. Curl tin even brandish the source code of a URL. All of this without requiring user interaction (a crucial feature for scripting).
I want to prove yous how to make employ of this command (one you'll frequently find in Linux howtos and scripts). I'll be demonstrating on Elementary OS, simply the control works on nearly every available Linux distribution. Curl is as well available on macOS and Windows.
SEE: 20 quick tips to make Linux networking easier (costless PDF) (TechRepublic)
Installing curl
Your Linux distribution should have curlicue installed past default. If non, the installation is quite simple. On a Debian derivative, the installation control would be:
sudo apt install gyre
You lot tin check the installation past issuing the control curl –version. You should see the version number of the installed awarding (on my Elementary OS auto, it's 7.47.0).
Using curl
Let'due south first see how curl can be used at its simplest. Say yous want to view the source of a web site. Nosotros'll use the curl site as an example. Result the command:
curl https://curlicue.haxx.se | less
Y'all can now ringlet through the HTML for that site (Figure A).
Figure A
This is a great style to figure out how a site has been created or even troubleshooting your own sites.
Of grade, that example is quite limited. Let'south use curl to pull down a file from a site. Let's stick with the same example. Say you desire to download the HTML for the curl site to view afterward. For this, we'll utilise the -o switch like so:
curl https://ringlet.haxx.se -o curlicue.html
The above command would download the HTML code from the gyre site and salve it as curl.html. Of class, curl isn't only capable of downloading source HTML. Say you have a file yous desire to download from a site. Curl can handle this like then:
coil http://SERVER_ADDRESS/FILENAME -o FILENAME
Where SERVER_ADDRESS is the URL of the server and FILENAME is the name of the file to be downloaded. Say for example, you lot want to download the latest release of Ubuntu Server. That tin can be done like and then:
roll http://releases.ubuntu.com/18.04/ubuntu-18.04-live-server-amd64.iso -o ubuntu-server-eighteen.04.iso
If that file is password protected, curlicue can handle that like then:
curl -u USERNAME:Countersign http://SERVER_ADDRESS -o FILENAME
Where:
- USERNAME is the username on the server.
- PASSWORD is the countersign for the user on the server.
- FILENAME is the file to be downloaded.
- SERVER_ADDRESS is the direct link to the file.
Yous can also use curl with an FTP server. Say you demand to download a file from an FTP server that happens to exist password protected. The command for this would be:
scroll ftp://SERVER_ADDRESS/FILENAME -user USERNAME:PASSWORD -o FILENAME
Where:
- SERVER_ADDRESS is the accost of the FTP server.
- FILENAME is the proper noun of the file to be downloaded.
- USERNAME is the username on the FTP server.
- PASSWORD is the password for the user on the FTP server.
To upload a file to an FTP server, the command would exist:
curl -T FILENAME SERVER_ADDRESS -user USERNAME:Countersign
Once again where:
- SERVER_ADDRESS is the address of the FTP server.
- FILENAME is the name of the file to be downloaded.
- USERNAME is the username on the FTP server.
- PASSWORD is the countersign for the user on the FTP server.
At some indicate, the whorl developers removed SFTP support from the libcurl. If you need to download a file, via SSH, you'll need to use the sftp command.
Why use curl
One of the biggest benefits of ringlet is that information technology tin be used without interaction. Because of that, information technology'southward perfectly suited for scripting. You'll meet many instances of Linux installer scripts that make employ of curl. Equally an installer script runs, y'all might well see roll practise its thing by downloading necessary dependencies. So if you're writing a Linux bash (or installer) script, curl will most certainly exist your friend. For more than information on roll (and there's a lot of it), issue the command human whorl and read through the manual page.
Learn more about Linux in TechRepublic Academy!
Also come across:
- How to use the Linux watch control for easier output tracking (TechRepublic)
- How to utilise the Linux screen command to keep your remote processes running (TechRepublic)
- How to set up an SFTP server on Linux (TechRepublic)
- How to monitor a Linux log file in real time (TechRepublic)
- ICANN makes last minute WHOIS changes to accost GDPR requirements (ZDNet)
Source: https://www.techrepublic.com/article/how-to-use-the-curl-command-for-uploading-and-downloading-files-without-user-interaction/