วิธีใช้ curl เพื่อดาวน์โหลดไฟล์จาก Linux Command Line
curl
คำสั่งLinux สามารถทำได้มากกว่าการดาวน์โหลดไฟล์ ค้นหาสิ่งที่มีความสามารถในและเมื่อคุณควรจะใช้มันแทนcurl
wget
curl vs. wget: อะไรคือความแตกต่าง?
ผู้คนมักจะดิ้นรนเพื่อระบุจุดแข็งที่สัมพันธ์กันของwget
และcurl
คำสั่ง คำสั่งมีการทำงานบางอย่างที่ทับซ้อนกัน พวกเขาแต่ละคนสามารถดึงไฟล์จากสถานที่ห่างไกลได้ แต่นั่นคือจุดสิ้นสุด
wget
เป็นเครื่องมือที่ยอดเยี่ยมสำหรับการดาวน์โหลดเนื้อหาและไฟล์ สามารถดาวน์โหลดไฟล์หน้าเว็บและไดเรกทอรี ประกอบด้วยกิจวัตรอันชาญฉลาดในการสำรวจลิงก์ในหน้าเว็บและดาวน์โหลดเนื้อหาซ้ำ ๆ ทั่วทั้งเว็บไซต์ มันไม่มีใครเทียบได้ในฐานะตัวจัดการการดาวน์โหลดแบบบรรทัดคำสั่ง
curl
ตอบสนองความต้องการที่แตกต่างกันโดยสิ้นเชิง ใช่มันสามารถดึงไฟล์ได้ แต่ไม่สามารถท่องเว็บไซต์ซ้ำเพื่อค้นหาเนื้อหาที่จะดึงข้อมูลได้ สิ่งที่curl
ทำได้จริงคือให้คุณโต้ตอบกับระบบระยะไกลโดยการร้องขอไปยังระบบเหล่านั้นและเรียกดูและแสดงการตอบสนองต่อคุณ คำตอบเหล่านี้อาจเป็นเนื้อหาและไฟล์ของหน้าเว็บ แต่ยังสามารถมีข้อมูลที่ให้ผ่านบริการเว็บหรือ API อันเป็นผลมาจาก "คำถาม" ที่ถามโดยคำขอ curl
และcurl
ไม่ จำกัด เฉพาะเว็บไซต์ curl
รองรับมากกว่า 20 โปรโตคอลรวมถึง HTTP, HTTPS, SCP, SFTP และ FTP และเนื้อหาเนื่องจากการจัดการไปป์ Linux ที่เหนือกว่าcurl
สามารถรวมเข้ากับคำสั่งและสคริปต์อื่น ๆ ได้ง่ายขึ้น
ผู้เขียนcurl
มีหน้าเว็บที่อธิบายถึงความแตกต่างที่เขาเห็นระหว่างและcurl
wget
การติดตั้ง curl
จากคอมพิวเตอร์ที่ใช้ในการค้นคว้าบทความนี้ Fedora 31 และ Manjaro 18.1.0 ได้curl
ติดตั้งแล้ว curl
ต้องติดตั้งบน Ubuntu 18.04 LTS บน Ubuntu ให้เรียกใช้คำสั่งนี้เพื่อติดตั้ง:
sudo apt-get install curl
เวอร์ชัน curl
--version
ทำให้ตัวเลือก curl
รายงานรุ่น นอกจากนี้ยังแสดงรายการโปรโตคอลทั้งหมดที่รองรับ
curl - รุ่น
การเรียกค้นเว็บเพจ
หากเราชี้ไปcurl
ที่หน้าเว็บก็จะดึงข้อมูลมาให้เรา
curl //www.bbc.com
แต่การดำเนินการเริ่มต้นคือการถ่ายโอนข้อมูลไปยังหน้าต่างเทอร์มินัลเป็นซอร์สโค้ด
ระวัง : ถ้าคุณไม่ได้บอกcurl
คุณต้องการบางสิ่งบางอย่างที่เก็บไว้เป็นไฟล์ก็จะมักจะถ่ายโอนข้อมูลไปยังหน้าต่าง terminal หากไฟล์ที่ดึงมาเป็นไฟล์ไบนารีผลลัพธ์อาจไม่สามารถคาดเดาได้ เชลล์อาจพยายามตีความค่าไบต์บางค่าในไฟล์ไบนารีเป็นอักขระควบคุมหรือลำดับการหลีกเลี่ยง
การบันทึกข้อมูลลงในไฟล์
บอก curl ให้เปลี่ยนทิศทางผลลัพธ์ไปยังไฟล์:
curl //www.bbc.com> bbc.html
คราวนี้เราไม่เห็นข้อมูลที่ดึงมามันถูกส่งตรงไปยังไฟล์ให้เรา เนื่องจากไม่มีเอาต์พุตหน้าต่างเทอร์มินัลให้แสดงcurl
เอาต์พุตชุดข้อมูลความคืบหน้า
ไม่ได้ทำเช่นนี้ในตัวอย่างก่อนหน้านี้เนื่องจากข้อมูลความคืบหน้าจะกระจัดกระจายไปทั่วซอร์สโค้ดของหน้าเว็บดังนั้นจึงcurl
ระงับโดยอัตโนมัติ
ในตัวอย่างนี้ curl
ตรวจพบว่าเอาต์พุตกำลังถูกเปลี่ยนเส้นทางไปยังไฟล์และปลอดภัยในการสร้างข้อมูลความคืบหน้า
ข้อมูลที่ให้ไว้คือ:
- % ทั้งหมด : จำนวนเงินทั้งหมดที่จะเรียกคืน
- % ที่ได้รับ : เปอร์เซ็นต์และค่าจริงของข้อมูลที่ดึงมาจนถึงตอนนี้
- % Xferd : เปอร์เซ็นต์และส่งจริงหากมีการอัปโหลดข้อมูล
- Average Speed Dload : ความเร็วในการดาวน์โหลดเฉลี่ย
- การอัปโหลดความเร็วเฉลี่ย : ความเร็วในการอัปโหลดโดยเฉลี่ย
- รวมเวลา : ระยะเวลาทั้งหมดโดยประมาณของการโอน
- เวลาที่ใช้ : เวลาที่ผ่านไปสำหรับการถ่ายโอนนี้
- เวลาที่เหลือ : เวลาที่เหลือโดยประมาณเพื่อให้การโอนเสร็จสมบูรณ์
- ความเร็วปัจจุบัน : ความเร็วในการถ่ายโอนปัจจุบันสำหรับการถ่ายโอนนี้
เนื่องจากเราเปลี่ยนเส้นทางผลลัพธ์จากcurl
ไปยังไฟล์ตอนนี้เราจึงมีไฟล์ชื่อ“ bbc.html”
ดับเบิลคลิกที่ไฟล์นั้นจะเปิดเบราว์เซอร์เริ่มต้นของคุณเพื่อให้แสดงเว็บเพจที่ดึงมา
โปรดทราบว่าที่อยู่ในแถบที่อยู่ของเบราว์เซอร์เป็นไฟล์ในเครื่องบนคอมพิวเตอร์เครื่องนี้ไม่ใช่เว็บไซต์ระยะไกล
เราไม่ต้องเปลี่ยนเส้นทางผลลัพธ์เพื่อสร้างไฟล์ เราสามารถสร้างไฟล์โดยใช้-o
ตัวเลือก (เอาต์พุต) และบอกcurl
ให้สร้างไฟล์ ที่นี่เรากำลังใช้-o
ตัวเลือกและระบุชื่อไฟล์ที่เราต้องการสร้าง“ bbc.html”
curl -o bbc.html //www.bbc.com
การใช้แถบความคืบหน้าเพื่อตรวจสอบการดาวน์โหลด
หากต้องการให้ข้อมูลการดาวน์โหลดแบบข้อความถูกแทนที่ด้วยแถบความคืบหน้าอย่างง่ายให้ใช้-#
ตัวเลือก (แถบความคืบหน้า)
curl -x -o bbc.html //www.bbc.com
การรีสตาร์ทการดาวน์โหลดที่ถูกขัดจังหวะ
ง่ายต่อการเริ่มการดาวน์โหลดที่ถูกยกเลิกหรือถูกขัดจังหวะ มาเริ่มดาวน์โหลดไฟล์ขนาดใหญ่ เราจะใช้เวอร์ชัน Long Term Support ล่าสุดของ Ubuntu 18.04 เรากำลังใช้--output
ตัวเลือกเพื่อระบุชื่อไฟล์ที่เราต้องการบันทึกลงใน:“ ubuntu180403.iso”
curl - ออก ubuntu18043.iso //releases.ubuntu.com/18.04.3/ubuntu-18.04.3-desktop-amd64.iso
การดาวน์โหลดจะเริ่มต้นและดำเนินไปจนเสร็จสิ้น
หากเราบังคับให้ขัดขวางการดาวน์โหลดCtrl+C
เราจะกลับไปที่พรอมต์คำสั่งและการดาวน์โหลดจะถูกยกเลิก
To restart the download, use the -C
(continue at) option. This causes curl
to restart the download at a specified point or offset within the target file. If you use a hyphen -
as the offset, curl
will look at the already downloaded portion of the file and determine the correct offset to use for itself.
curl -C - --output ubuntu18043.iso //releases.ubuntu.com/18.04.3/ubuntu-18.04.3-desktop-amd64.iso
The download is restarted. curl
reports the offset at which it is restarting.
Retrieving HTTP headers
With the -I
(head) option, you can retrieve the HTTP headers only. This is the same as sending the HTTP HEAD command to a web server.
curl -I www.twitter.com
This command retrieves information only; it does not download any web pages or files.
Downloading Multiple URLs
Using xargs
we can download multiple URLs at once. Perhaps we want to download a series of web pages that make up a single article or tutorial.
Copy these URLs to an editor and save it to a file called “urls-to-download.txt.” We can use xargs
to treat the content of each line of the text file as a parameter which it will feed to curl
, in turn.
//tutorials.ubuntu.com/tutorial/tutorial-create-a-usb-stick-on-ubuntu#0 //tutorials.ubuntu.com/tutorial/tutorial-create-a-usb-stick-on-ubuntu#1 //tutorials.ubuntu.com/tutorial/tutorial-create-a-usb-stick-on-ubuntu#2 //tutorials.ubuntu.com/tutorial/tutorial-create-a-usb-stick-on-ubuntu#3 //tutorials.ubuntu.com/tutorial/tutorial-create-a-usb-stick-on-ubuntu#4 //tutorials.ubuntu.com/tutorial/tutorial-create-a-usb-stick-on-ubuntu#5
This is the command we need to use to have xargs
pass these URLs to curl
one at a time:
xargs -n 1 curl -O < urls-to-download.txt
Note that this command uses the -O
(remote file) output command, which uses an uppercase “O.” This option causes curl
to save the retrieved file with the same name that the file has on the remote server.
The -n 1
option tells xargs
to treat each line of the text file as a single parameter.
When you run the command, you’ll see multiple downloads start and finish, one after the other.
Checking in the file browser shows the multiple files have been downloaded. Each one bears the name it had on the remote server.
RELATED:How to Use the xargs Command on Linux
Downloading Files From an FTP Server
Using curl
with a File Transfer Protocol (FTP) server is easy, even if you have to authenticate with a username and password. To pass a username and password with curl
use the -u
(user) option, and type the username, a colon “:”, and the password. Don’t put a space before or after the colon.
This is a free-for-testing FTP server hosted by Rebex. The test FTP site has a pre-set username of “demo”, and the password is “password.” Don’t use this type of weak username and password on a production or “real” FTP server.
curl -u demo:password ftp://test.rebex.net
curl
figures out that we’re pointing it at an FTP server, and returns a list of the files that are present on the server.
The only file on this server is a “readme.txt” file, of 403 bytes in length. Let’s retrieve it. Use the same command as a moment ago, with the filename appended to it:
curl -u demo:password ftp://test.rebex.net/readme.txt
The file is retrieved and curl
displays its contents in the terminal window.
In almost all cases, it is going to be more convenient to have the retrieved file saved to disk for us, rather than displayed in the terminal window. Once more we can use the -O
(remote file) output command to have the file saved to disk, with the same filename that it has on the remote server.
curl -O -u demo:password ftp://test.rebex.net/readme.txt
The file is retrieved and saved to disk. We can use ls
to check the file details. It has the same name as the file on the FTP server, and it is the same length, 403 bytes.
ls -hl readme.txt
RELATED:How to Use the FTP Command on Linux
Sending Parameters to Remote Servers
Some remote servers will accept parameters in requests that are sent to them. The parameters might be used to format the returned data, for example, or they may be used to select the exact data that the user wishes to retrieve. It is often possible to interact with web application programming interfaces (APIs) using curl
.
As a simple example, the ipify website has an API can be queried to ascertain your external IP address.
curl //api.ipify.org
By adding the format
parameter to the command, with the value of “json” we can again request our external IP address, but this time the returned data will be encoded in the JSON format.
curl //api.ipify.org?format=json
Here’s another example that makes use of a Google API. It returns a JSON object describing a book. The parameter you must provide is the International Standard Book Number (ISBN) number of a book. You can find these on the back cover of most books, usually below a barcode. The parameter we’ll use here is “0131103628.”
curl //www.googleapis.com/books/v1/volumes?q=isbn:0131103628
The returned data is comprehensive:
Sometimes curl, Sometimes wget
If I wanted to download content from a website and have the tree-structure of the website searched recursively for that content, I’d use wget
.
If I wanted to interact with a remote server or API, and possibly download some files or web pages, I’d use curl
. Especially if the protocol was one of the many not supported by wget
.