행위

Curl 파일다운 로드

DB CAFE

thumb_up 추천메뉴 바로가기


1 cURL 로 파일 다운로드 받기[편집]

1.1 URL호출 결과 파일로 쓰기[편집]

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

옵션 없이 호출하는 curl 커맨드는 표준 출력으로 응답을 출력하는데, 위와 같이 출력을 리다이렉트해서 파일에 쓸 수 있다.

1.2 파일명으로 저장하기[편집]

-o 옵션 : 파일명으로 저장

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

-O 옵션 : 원본 파일명으로 저장

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

1.3 여러 파일 다운로드 받기(정규식이용)[편집]

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

위 코드는, foo0.txt 부터 foo9.txt 파일을 다운로드 받는다.

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

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

$curl -O http://example.com/{foo,bar,baz}.txt

중괄호({})를 써서 위와 같이 호출할 수도 있고, foo.txt, bar.txt, baz.txt 를 다운로드 받는다.


1.4 for 문으로 여러 파일 다운로드 받기[편집]

$ files="foo bar baz"
$ for name in files; do
  curl -O "http://example.com/${name}.txt"
done

cURL의 옵션에 대한 상세한 설명은 아래 포스트를 참고하면 된다. http://ohgyun.com/489

참고:

- wget vs curl: http://www.thegeekstuff.com/2012/07/wget-curl/

- Single line command line to download multiple files: http://lists.apple.com/archives/macos-x-server/2004/Nov/msg01211.html

- 15 Practical Linux cURL Command Examples: http://www.thegeekstuff.com/2012/04/curl-examples/