웹사이트 검색

초보자를 위한 Linux curl 명령 자습서(예제 5개)


이 페이지에서

  1. Linux 컬 명령
  2. Q1. 컬 명령은 어떻게 작동합니까?\n
  3. Q2. curl이 동일한 다운로드 파일 이름을 사용하도록 만드는 방법은 무엇입니까?\n
  4. Q3. curl을 사용하여 여러 파일을 다운로드하는 방법은 무엇입니까?\n
  5. Q4. 이전된 문제를 해결하는 방법은 무엇입니까?\n
  6. Q5. 중단 지점에서 다운로드를 재개하는 방법은 무엇입니까?\n
  7. 결론

웹 브라우저는 사용자가 인터넷에서 자료를 다운로드하는 데 사용하는 기본 매체이지만 이를 가능하게 하는 일부 Linux 명령도 있습니다. 이러한 도구는 GUI가 없는 헤드리스 시스템에서 유용합니다.

이 자습서에서는 무엇보다도 웹에서 항목을 다운로드할 수 있는 명령 중 하나인 curl에 대해 설명합니다. 이 문서에서 설명하는 예제는 Ubuntu 16.04 LTS에서 테스트되었습니다.

리눅스 컬 명령

curl 명령을 사용하면 Linux의 명령줄을 통해 데이터를 다운로드하고 업로드할 수 있습니다. 구문은 다음과 같습니다.

curl [options] [URL...]

이 명령에 대한 매뉴얼 페이지의 내용은 다음과 같습니다.

 curl is a tool to transfer data from or to a server, using one of the
supported protocols (DICT, FILE, FTP, FTPS, GOPHER, HTTP, HTTPS, IMAP,
IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMTP, SMTPS,
TELNET and TFTP). The command is designed to work without user inter?
action.

curl offers a busload of useful tricks like proxy support, user authen?
tication, FTP upload, HTTP post, SSL connections, cookies, file trans?
fer resume, Metalink, and more. As you will see below, the number of
features will make your head spin!

curl is powered by libcurl for all transfer-related features. See
libcurl(3) for details.

다음은 curl 작동 방식에 대한 더 나은 아이디어를 제공하는 몇 가지 Q&A 스타일의 예입니다.

Q1. 컬 명령은 어떻게 작동합니까?

기본 사용법은 매우 간단합니다. URL을 curl 명령에 대한 입력으로 전달하고 출력을 파일로 리디렉션하면 됩니다.

예를 들어:

curl http://releases.ubuntu.com/18.04/ubuntu-18.04-desktop-amd64.iso.torrent > test.torrent

여기서 -o 옵션을 사용할 수도 있습니다.

-o, --output <file>
Write output to <file> instead of stdout.

예제로 돌아가서 데이터가 내 시스템의 test.torrent 파일에 다운로드되는 동안 명령줄에 다음 출력이 생성되었습니다.

다음은 출력에 표시되는 이 진행률 표시기에 대한 매뉴얼 페이지의 내용입니다.

 curl normally displays a progress meter during operations, indicating
the amount of transferred data, transfer speeds and estimated time
left, etc.

curl displays this data to the terminal by default, so if you invoke
curl to do an operation and it is about to write data to the terminal,
it disables the progress meter as otherwise it would mess up the output
mixing progress meter and response data.

If you want a progress meter for HTTP POST or PUT requests, you need to
redirect the response output to a file, using shell redirect (>), -o
[file] or similar.

It is not the same case for FTP upload as that operation does not spit
out any response data to the terminal.

If you prefer a progress "bar" instead of the regular meter, -# is your
friend.

Q2. 컬이 동일한 다운로드 파일 이름을 사용하도록 만드는 방법은 무엇입니까?

이전 예에서는 다운로드한 파일 이름을 명시적으로 지정해야 했습니다. 그러나 원하는 경우 컬이 다운로드 중인 파일 이름을 로컬 파일 이름으로 사용하도록 강제할 수 있습니다. 이것은 -O 명령줄 옵션을 사용하여 수행할 수 있습니다.

curl -O http://releases.ubuntu.com/18.04/ubuntu-18.04-desktop-amd64.iso.torrent

따라서 이 경우 ubuntu-18.04-desktop-amd64.iso.torrent라는 파일이 내 시스템의 출력에 생성되었습니다.

Q3. curl을 사용하여 여러 파일을 다운로드하는 방법은 무엇입니까?

이것도 복잡하지 않습니다. 다음과 같은 방법으로 URL을 전달하면 됩니다.

curl -O [URL1] -O [URL2] -O [URL3] ...

예를 들어:

curl -O http://releases.ubuntu.com/18.04/ubuntu-18.04-desktop-amd64.iso.torrent -O http://releases.ubuntu.com/18.04/ubuntu-18.04-live-server-amd64.iso.torrent

다음은 위의 명령입니다.

따라서 두 URL의 다운로드 진행률이 출력에 표시된 것을 볼 수 있습니다.

Q4. 이전된 문제를 해결하는 방법은 무엇입니까?

curl 명령에 URL을 전달할 때 "Moved" 또는 "Moved Permanently"와 같은 오류가 발생하는 경우가 있습니다. 이것은 일반적으로 입력 URL이 다른 URL로 리디렉션될 때 발생합니다. 예를 들어 oneplus.com이라는 웹사이트를 열면 해당 웹사이트가 국내 URL(예: oneplus.in)로 리디렉션되므로 다음과 같은 오류가 발생합니다.

curl이 리디렉션을 따르도록 하려면 대신 -L 명령줄 옵션을 사용하십시오.

curl -L http://www.oneplus.com

Q5. 중단 지점에서 다운로드를 재개하는 방법은 무엇입니까?

때때로 다운로드가 중간에 중단됩니다. 그래서 자연스럽게 시간과 데이터를 절약하기 위해 다시 시도할 때,. 중단된 지점부터 시작하기를 원할 수 있습니다. Curl에서는 -C 명령줄 옵션을 사용하여 이 작업을 수행할 수 있습니다.

예를 들어:

 curl -C - -O http://releases.ubuntu.com/18.04/ubuntu-18.04-desktop-amd64.iso

다음 스크린샷은 중단된 후 다운로드를 재개하는 curl 명령을 보여줍니다.

결론

보시다시피 curl 명령은 명령줄을 통해 항목을 다운로드하려는 경우 유용한 유틸리티입니다. 이 도구는 훨씬 더 많은 기능을 제공하므로 여기에서 표면을 긁었습니다. 이 튜토리얼에서 설명한 명령줄 옵션 연습을 마치면 curls 매뉴얼 페이지로 이동하여 자세한 내용을 알아볼 수 있습니다.