كيفية استخدام curl لتنزيل الملفات من سطر أوامر Linux

curlيمكن لأمر Linux القيام بأكثر من مجرد تنزيل الملفات. اكتشف ما curlهو قادر ومتى يجب عليك استخدامه بدلاً من wget.

curl مقابل wget: ما هو الفرق؟

كثيرا ما يعاني الناس للتعرف على نقاط القوة النسبية لل wgetو curlالأوامر. الأوامر لديها بعض التداخل الوظيفي. يمكن لكل منهم استرداد الملفات من مواقع بعيدة ، ولكن هذا هو المكان الذي ينتهي فيه التشابه.

wgetهي أداة رائعة لتنزيل المحتوى والملفات. يمكنه تنزيل الملفات وصفحات الويب والأدلة. يحتوي على إجراءات ذكية لاجتياز الروابط في صفحات الويب وتنزيل المحتوى بشكل متكرر عبر موقع ويب بأكمله. إنه غير مسبوق كمدير تنزيل لسطر الأوامر.

curlيلبي حاجة مختلفة تمامًا. نعم ، يمكنه استرداد الملفات ، لكن لا يمكنه التنقل بشكل متكرر في موقع ويب يبحث عن محتوى لاسترداده. ما curlيفعله في الواقع هو السماح لك بالتفاعل مع الأنظمة البعيدة عن طريق تقديم طلبات إلى تلك الأنظمة واسترداد ردودها وعرضها عليك. قد تكون هذه الردود عبارة عن محتوى صفحة ويب وملفات ، ولكنها يمكن أن تحتوي أيضًا على بيانات مقدمة عبر خدمة ويب أو واجهة برمجة تطبيقات نتيجة "السؤال" الذي طرحه طلب curl.

و curlلا يقتصر على مواقع الانترنت. curlيدعم أكثر من 20 بروتوكولًا ، بما في ذلك HTTP و HTTPS و SCP و SFTP و FTP. ويمكن القول ، نظرًا لمعالجتها الفائقة لأنابيب Linux ، curlأن تتكامل بسهولة أكبر مع الأوامر والبرامج النصية الأخرى.

مؤلف curlلديه صفحة ويب تصف الاختلافات التي يراها بين curlو wget.

تركيب الضفيرة

من بين أجهزة الكمبيوتر المستخدمة للبحث في هذه المقالة ، curl تم تثبيت Fedora 31 و Manjaro 18.1.0 بالفعل. curlيجب تثبيته على Ubuntu 18.04 LTS. على أوبونتو ، قم بتشغيل هذا الأمر لتثبيته:

sudo apt-get install curl

نسخة الضفيرة

و --versionيجعل خيار  curlتقرير نسخته. كما يسرد جميع البروتوكولات التي يدعمها.

curl - الإصدار

استرجاع صفحة ويب

إذا أشرنا curlإلى صفحة ويب ، فسوف تسترجعها لنا.

حليقة //www.bbc.com

لكن الإجراء الافتراضي هو تفريغه في نافذة المحطة ككود مصدر.

احذر : إذا لم تخبر curlأنك تريد تخزين شيء ما كملف ، فسيتم تفريغه دائمًا في نافذة المحطة الطرفية. إذا كان الملف الذي يتم استرداده عبارة عن ملف ثنائي ، فقد تكون النتيجة غير متوقعة. قد يحاول الغلاف تفسير بعض قيم البايت في الملف الثنائي كأحرف تحكم أو تسلسلات هروب.

حفظ البيانات في ملف

دعنا نقول curl لإعادة توجيه الإخراج إلى ملف:

curl //www.bbc.com> bbc.html

هذه المرة لا نرى المعلومات المستردة ، يتم إرسالها مباشرة إلى الملف من أجلنا. نظرًا لعدم وجود مخرج نافذة طرفية لعرضه ، يقوم curlبإخراج مجموعة من معلومات التقدم.

لم يفعل ذلك في المثال السابق لأن معلومات التقدم كانت مبعثرة في جميع أنحاء الكود المصدري لصفحة الويب ، لذلك تم curlمنعها تلقائيًا.

في هذا المثال ،  curlيكتشف أنه تتم إعادة توجيه الإخراج إلى ملف وأنه من الآمن إنشاء معلومات التقدم.

المعلومات المقدمة هي:

  • ٪ الإجمالي : المبلغ الإجمالي الذي سيتم استرداده.
  • النسبة المئوية المتلقاة : النسبة المئوية والقيم الفعلية للبيانات المسترجعة حتى الآن.
  • ٪ Xferd : النسبة المئوية والفعلية المرسلة ، إذا تم تحميل البيانات.
  • متوسط ​​سرعة التحميل : متوسط ​​سرعة التنزيل.
  • متوسط ​​سرعة التحميل : متوسط ​​سرعة الرفع.
  • إجمالي الوقت : المدة الإجمالية المقدرة للتحويل.
  • الوقت المنقضي: الوقت المنقضي حتى الآن لعملية النقل هذه.
  • الوقت المتبقي : الوقت المقدر المتبقي لإكمال التحويل
  • السرعة الحالية : سرعة النقل الحالية لهذا النقل.

نظرًا لأننا أعدنا توجيه الإخراج من curl إلى ملف ، لدينا الآن ملف يسمى "bbc.html".

سيؤدي النقر المزدوج فوق هذا الملف إلى فتح المستعرض الافتراضي الخاص بك بحيث يعرض صفحة الويب المستردة.

لاحظ أن العنوان الموجود في شريط عنوان المتصفح هو ملف محلي على هذا الكمبيوتر ، وليس موقع ويب بعيد.

لا يتعين علينا إعادة توجيه الإخراج لإنشاء ملف. يمكننا إنشاء ملف باستخدام -oخيار (الإخراج) ، وإخبارنا curlبإنشاء الملف. نحن هنا نستخدم -oالخيار ونقدم اسم الملف الذي نرغب في إنشاء "bbc.html".

curl -o bbc.html //www.bbc.com

استخدام شريط التقدم لمراقبة التنزيلات

لاستبدال معلومات التنزيل المستندة إلى النص بشريط تقدم بسيط ، استخدم خيار -#(شريط التقدم).

curl -x -o bbc.html //www.bbc.com

إعادة تشغيل تنزيل متقطع

من السهل إعادة تشغيل تنزيل تم إنهاؤه أو مقاطعته. لنبدأ تنزيل ملف كبير الحجم. سنستخدم أحدث إصدار من الدعم طويل الأمد لـ 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.