코드코코

[리눅스] curl로 파일 다운로드 받기 본문

기록/리눅스

[리눅스] curl로 파일 다운로드 받기

코드코코 2021. 12. 15. 15:37

curl

 - curl 은 커맨드 라인에서 URL 형태의 리소스를 요청/응답할 수 있는 도구

 - 전체 매뉴얼 : man curl 호출 

 - 표준 출력 : $ curl <url>

root@DESKTOP-RJ31OF5:/home/ubuntu# curl http://example.com/resource
<!doctype html>
<html>
<head>
    <title>Example Domain</title>

    <meta charset="utf-8" />
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <style type="text/css">
    body {
        background-color: #f0f0f2;
        margin: 0;
        padding: 0;
        font-family: -apple-system, system-ui, BlinkMacSystemFont, "Segoe UI", "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;

    }
    div {
        width: 600px;
        margin: 5em auto;
        padding: 2em;
        background-color: #fdfdff;
        border-radius: 0.5em;
        box-shadow: 2px 3px 7px 2px rgba(0,0,0,0.02);
    }
    a:link, a:visited {
        color: #38488f;
        text-decoration: none;
    }
    @media (max-width: 700px) {
        div {
            margin: 0 auto;
            width: auto;
        }
    }
    </style>
</head>

<body>
<div>
    <h1>Example Domain</h1>
    <p>This domain is for use in illustrative examples in documents. You may use this
    domain in literature without prior coordination or asking for permission.</p>
    <p><a href="https://www.iana.org/domains/example">More information...</a></p>
</div>
</body>
</html>

 

 

1. 응답을 파일에 쓰기

 

$ curl http://example.com/resource > foo.txt

 

root@DESKTOP-RJ31OF5:/home/ubuntu# curl http://example.com/resource > foo.txt
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  1256  100  1256    0     0   4301      0 --:--:-- --:--:-- --:--:--  4301
root@DESKTOP-RJ31OF5:/home/ubuntu# ls -la | grep foo
-rw-r--r-- 1 root   root   1256 Dec 15 13:23 foo.txt

 

참고

리눅스 커맨드에서 꺽쇠(>)는 좌측 명령의 결과를 우측 파일명에 저장하게 한다.

이 때 꺽쇠 하나(>)는 파일을 새로 생성하고 꺽쇠 둘(>>)은 파일에 덧붙인다. 

 

2. 파일명으로 저장하기

 

-o 옵션으로 리소스의 파일명을 지정해 다운로드할 수 있다.

$ curl -o foo.txt http://example.com/foo.txt

root@DESKTOP-RJ31OF5:/home/ubuntu# curl -o foo2.txt http://example.com/foo2.txt
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  1256  100  1256    0     0   3925      0 --:--:-- --:--:-- --:--:--  3925
root@DESKTOP-RJ31OF5:/home/ubuntu# ls -la | grep foo2
-rw-r--r-- 1 root   root   1256 Dec 15 13:31 foo2.txt

 

-O 옵션을 사용하는 경우, 리소스의 파일명을 그대로 사용한다.
root@DESKTOP-RJ31OF5:/home/ubuntu# curl -O http://example.com/foo3.txt
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  1256  100  1256    0     0   4025      0 --:--:-- --:--:-- --:--:--  4012
root@DESKTOP-RJ31OF5:/home/ubuntu# ls -la | grep foo3
-rw-r--r-- 1 root   root   1256 Dec 15 13:34 foo3.txt
 

3. 여러 파일 다운로드 받기

 

- bar0.txt 부터 bar9.txt 파일을 다운로드 받는다.

$ curl -O http://example.com/bar[0-9].txt

root@DESKTOP-RJ31OF5:/home/ubuntu# curl -O http://example.com/bar[0-9].txt

[1/10]: http://example.com/bar0.txt --> bar0.txt
--_curl_--http://example.com/bar0.txt
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  1256  100  1256    0     0   4172      0 --:--:-- --:--:-- --:--:--  4172

[2/10]: http://example.com/bar1.txt --> bar1.txt
--_curl_--http://example.com/bar1.txt
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  1256  100  1256    0     0   7801      0 --:--:-- --:--:-- --:--:--  7801

[3/10]: http://example.com/bar2.txt --> bar2.txt
--_curl_--http://example.com/bar2.txt
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  1256  100  1256    0     0   7753      0 --:--:-- --:--:-- --:--:--  7753

[4/10]: http://example.com/bar3.txt --> bar3.txt
--_curl_--http://example.com/bar3.txt
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  1256  100  1256    0     0   7520      0 --:--:-- --:--:-- --:--:--  7476

[5/10]: http://example.com/bar4.txt --> bar4.txt
--_curl_--http://example.com/bar4.txt
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  1256  100  1256    0     0   7705      0 --:--:-- --:--:-- --:--:--  7705

[6/10]: http://example.com/bar5.txt --> bar5.txt
--_curl_--http://example.com/bar5.txt
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  1256  100  1256    0     0   7658      0 --:--:-- --:--:-- --:--:--  7658

[7/10]: http://example.com/bar6.txt --> bar6.txt
--_curl_--http://example.com/bar6.txt
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  1256  100  1256    0     0   7753      0 --:--:-- --:--:-- --:--:--  7753

[8/10]: http://example.com/bar7.txt --> bar7.txt
--_curl_--http://example.com/bar7.txt
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  1256  100  1256    0     0   7850      0 --:--:-- --:--:-- --:--:--  7850

[9/10]: http://example.com/bar8.txt --> bar8.txt
--_curl_--http://example.com/bar8.txt
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  1256  100  1256    0     0   7801      0 --:--:-- --:--:-- --:--:--  7801

[10/10]: http://example.com/bar9.txt --> bar9.txt
--_curl_--http://example.com/bar9.txt
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  1256  100  1256    0     0   7850      0 --:--:-- --:--:-- --:--:--  7850
root@DESKTOP-RJ31OF5:/home/ubuntu# ls -la | grep bar
-rw-r--r-- 1 root   root   1256 Dec 15 13:36 bar0.txt
-rw-r--r-- 1 root   root   1256 Dec 15 13:36 bar1.txt
-rw-r--r-- 1 root   root   1256 Dec 15 13:36 bar2.txt
-rw-r--r-- 1 root   root   1256 Dec 15 13:36 bar3.txt
-rw-r--r-- 1 root   root   1256 Dec 15 13:36 bar4.txt
-rw-r--r-- 1 root   root   1256 Dec 15 13:36 bar5.txt
-rw-r--r-- 1 root   root   1256 Dec 15 13:36 bar6.txt
-rw-r--r-- 1 root   root   1256 Dec 15 13:36 bar7.txt
-rw-r--r-- 1 root   root   1256 Dec 15 13:36 bar8.txt
-rw-r--r-- 1 root   root   1256 Dec 15 13:36 bar9.txt

- []는 여러 번 조합할 수 있고, 위 코드는 foo-a0.txt 부터 foo-z9.txt 까지의 파일을 다운로드 받는다.

$ curl -O http://example.com/foo-[a-c][0-2].txt

root@DESKTOP-RJ31OF5:/home/ubuntu# curl -O http://example.com/foo-[a-c][0-2].txt

[1/9]: http://example.com/foo-a0.txt --> foo-a0.txt
--_curl_--http://example.com/foo-a0.txt
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  1256  100  1256    0     0   3949      0 --:--:-- --:--:-- --:--:--  3949

[2/9]: http://example.com/foo-a1.txt --> foo-a1.txt
--_curl_--http://example.com/foo-a1.txt
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  1256  100  1256    0     0   6977      0 --:--:-- --:--:-- --:--:--  6977

[3/9]: http://example.com/foo-a2.txt --> foo-a2.txt
--_curl_--http://example.com/foo-a2.txt
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  1256  100  1256    0     0   7566      0 --:--:-- --:--:-- --:--:--  7566

[4/9]: http://example.com/foo-b0.txt --> foo-b0.txt
--_curl_--http://example.com/foo-b0.txt
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  1256  100  1256    0     0   7177      0 --:--:-- --:--:-- --:--:--  7177

[5/9]: http://example.com/foo-b1.txt --> foo-b1.txt
--_curl_--http://example.com/foo-b1.txt
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  1256  100  1256    0     0   7520      0 --:--:-- --:--:-- --:--:--  7520

[6/9]: http://example.com/foo-b2.txt --> foo-b2.txt
--_curl_--http://example.com/foo-b2.txt
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  1256  100  1256    0     0   5869      0 --:--:-- --:--:-- --:--:--  5841

[7/9]: http://example.com/foo-c0.txt --> foo-c0.txt
--_curl_--http://example.com/foo-c0.txt
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  1256  100  1256    0     0   7566      0 --:--:-- --:--:-- --:--:--  7566

[8/9]: http://example.com/foo-c1.txt --> foo-c1.txt
--_curl_--http://example.com/foo-c1.txt
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  1256  100  1256    0     0   6789      0 --:--:-- --:--:-- --:--:--  6789

[9/9]: http://example.com/foo-c2.txt --> foo-c2.txt
--_curl_--http://example.com/foo-c2.txt
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  1256  100  1256    0     0   5869      0 --:--:-- --:--:-- --:--:--  5869
root@DESKTOP-RJ31OF5:/home/ubuntu# ls -la | grep foo
-rw-r--r-- 1 root   root   1256 Dec 15 13:40 foo-a0.txt
-rw-r--r-- 1 root   root   1256 Dec 15 13:40 foo-a1.txt
-rw-r--r-- 1 root   root   1256 Dec 15 13:40 foo-a2.txt
-rw-r--r-- 1 root   root   1256 Dec 15 13:40 foo-b0.txt
-rw-r--r-- 1 root   root   1256 Dec 15 13:40 foo-b1.txt
-rw-r--r-- 1 root   root   1256 Dec 15 13:40 foo-b2.txt
-rw-r--r-- 1 root   root   1256 Dec 15 13:40 foo-c0.txt
-rw-r--r-- 1 root   root   1256 Dec 15 13:40 foo-c1.txt
-rw-r--r-- 1 root   root   1256 Dec 15 13:40 foo-c2.txt

 

4.  for 문으로 여러 파일 다운로드 받기

 

root@DESKTOP-RJ31OF5:/home/ubuntu/test_dir# files="foo bar baz"
root@DESKTOP-RJ31OF5:/home/ubuntu/test_dir# for name in $files; do curl -O "http://example.com/${name}
.txt"; done
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   445  100   445    0     0   1493      0 --:--:-- --:--:-- --:--:--  1493
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  1256  100  1256    0     0   3817      0 --:--:-- --:--:-- --:--:--  3806
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  1256  100  1256    0     0   3783      0 --:--:-- --:--:-- --:--:--  3783
root@DESKTOP-RJ31OF5:/home/ubuntu/test_dir# ls -la
total 8
drwxr-xr-x 1 root   root   4096 Dec 15 14:30 .
drwxr-xr-x 1 ubuntu ubuntu 4096 Dec 15 14:05 ..
-rw-r--r-- 1 root   root   1256 Dec 15 14:30 bar.txt
-rw-r--r-- 1 root   root   1256 Dec 15 14:30 baz.txt
-rw-r--r-- 1 root   root    445 Dec 15 14:30 foo.txt
-rw-r--r-- 1 root   root      0 Dec 15 13:56 test1.txt
-rw-r--r-- 1 root   root      0 Dec 15 13:57 test2.txt
-rw-r--r-- 1 root   root      0 Dec 15 13:57 test3.txt

 

'기록 > 리눅스' 카테고리의 다른 글

[리눅스] 사용자와 그룹 관련 명령어  (0) 2021.12.15
[리눅스] 다중명령어  (0) 2021.12.15
[리눅스] wget 실습  (0) 2021.12.15
[리눅스] 파일 다운로드  (0) 2021.12.15
[리눅스] 리눅스 패키지  (0) 2021.12.15