레이블이 명령어인 게시물을 표시합니다. 모든 게시물 표시
레이블이 명령어인 게시물을 표시합니다. 모든 게시물 표시

2013년 4월 11일 목요일

curl 사용법

기본 사용
$ curl www.google.com
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>302 Moved</TITLE></HEAD><BODY>
<H1>302 Moved</H1>
The document has moved
<A HREF="http://www.google.co.kr/">here</A>.
</BODY></HTML>


 - 302 는 redirection 코드

자세한 내용 표시 : -v 옵션
$ curl -v www.google.com
* About to connect() to www.google.com port 80 (#0)
*   Trying 74.125.128.147...
* connected
* Connected to www.google.com (74.125.128.147) port 80 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.24.0 (x86_64-apple-darwin12.0) libcurl/7.24.0 OpenSSL/0.9.8r zlib/1.2.5
> Host: www.google.com
> Accept: */*
< HTTP/1.1 302 Found
< Location: http://www.google.co.kr/
< Cache-Control: private
< Content-Type: text/html; charset=UTF-8
< Set-Cookie: PREF=ID=772acf0ed672750a:FF=0:TM=1365725569:LM=1365725569:S=oN2I30J2yduSpsDA; expires=Sun, 12-Apr-2015 00:12:49 GMT; path=/; domain=.google.com
< Set-Cookie: NID=67=n0oJCc3IUnhapZm7IaK47FB9JpM81dAjqLA1ifC5yYPWBg6-g4K_ZcPJaZOD4t23yVK4EzPB7wpe0zsEEHT3hdTQbi9Q-s21LORDQEZRkuWdS2YUzaOw2OjzzWV76b6P; expires=Sat, 12-Oct-2013 00:12:49 GMT; path=/; domain=.google.com; HttpOnly
< P3P: CP="This is not a P3P policy! See http://www.google.com/support/accounts/bin/answer.py?hl=en&answer=151657 for more info."
< Date: Fri, 12 Apr 2013 00:12:49 GMT
< Server: gws
< Content-Length: 221
< X-XSS-Protection: 1; mode=block
< X-Frame-Options: SAMEORIGIN
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>302 Moved</TITLE></HEAD><BODY>
<H1>302 Moved</H1>
The document has moved
<A HREF="http://www.google.co.kr/">here</A>.
</BODY></HTML>
* Connection #0 to host www.google.com left intact
* Closing connection #0

 - curl  이 작업하는 과정과 서버로 전송하는 내용 등을 자세하게 표시해 준다.

redirect 따라가기 : -L 옵션
$ curl -L www.google.com
<!doctype html><html itemscope="itemscope" itemtype="http://schema.org/WebPage"><head><meta itemprop="image" content="/images/google_favicon_128.png"><title>Google</title><script>(function(){
window.google={kEI:"jEhnUYS0HJCPiAf5jIGYCw",getEI:function(a){for(var b;a&&(!a.getAttribute||!(b=a.getAttribute("eid")));)a=a.parentNode;return b||google.kEI},https:function()
 -- 생략 --

파일 다운로드 : -O (대문자 영문자 오) 옵션
$ curl -O -L www.google.com/index.html
$ ls
index.html

 - 파일 이름 지정 : -o (소문자 영문자 오) 옵션
$ curl -L www.google.com/index.html -o google.html

HTTPS 사이트 접속
$ curl -L https://www.google.com:443
만약 실패한다면 클라이언트 PC 에 인증서가 없거나 해당 사이트의 인증서가 잘못된 것일 수 있다.
 - 인증을 무시하고 접근하려면 -k 옵션을 사용
$ curl -k -L https://www.google.com:443
 - 공개 인증서 다운로드 : http://curl.haxx.se/ca/ 에서 cacert.pem 파일을 다운로드
 - 다운로드 받은 인증서를 매개변수로 추가
$ curl --cacert cacert.pem https://www.google.com:443

데이터 전송 (post 방식)
옵션 종류 (모두 나열하지 않음)
  • --data : 입력한 매개변수 내용 그대로 전송
  • --data-binary : 입력한 매개변수 내용 그대로 전송 (binary)
  • --form : form 형식으로 전송
--data 와 --data-binary 매개변수
 - 매개변수에 "hello" 라고 넣어주면 문자그대로 hello 를 데이터 인식하고 전달한다.
 - 매개변수에 @ 를 앞에 붙이면 파일명으로 인식하고 파일을 찾아 파일 내용을 전달한다.
   > $ curl --data-binary @hello.txt www.google.com
   > 내용이 form 같은 형식 등으로 가공되지 않고 그대로 보내진다.

--form
$ curl --form greeting=hello www.google.com
 ==> form 형식으로 전송

curl --form myfile=@filepath www.google.com
 ==> form 형식으로 파일 전송 (multipart/form-data)

팁: 마지막 받은 위치부터 받기
!) 서버에서 HTTP Range spec 을 지원해야 함
$ curl -L -O -C - $DOWNLOAD_URL
 - '-C -' 옵션을 추가

2012년 5월 26일 토요일

mkdir 사용

폴더 생성 시 mkdir 명령어를 이용한다.

$ mkdir hello
hello 폴더를 생성한다.

$ mkdir a/b/c
만약 a 나 b 폴더가 없는 경우 에러를 표시한다.
이런 경우 -p 옵션을 이용한다.

$ mkdir -p a/b/c
만약 a 나 b 폴더가 없는 경우 자동으로 생성한다.

2012년 3월 23일 금요일

특정 크기의 파일 만들기

dd(Unix) - http://en.wikipedia.org/wiki/Dd_(Unix)

dd 명령어를 이용하여 특정 크기의 파일을 생성할 수 있다.
참고: http://darkrang.tistory.com/191
$ dd if=/dev/zero of=[생성할 파일 경로] bs=[block size] count=[개수]
예) 100 MB 크기의 testfile 이라는 이름의 파일을 생성한다.
$ dd if=/dev/zero of=/home/test-user/testfile bs=100M count=1

wiki 인용: block size 에 사용할 수 있는 단위
w means 2
b means 512
k means 1024
M specifies multiplication by 10242
G specifies multiplication by 10243

추가) 단순 파일 생성은 touch 명령어를 이용한다.
$ touch [생성할 파일 path]
예) 0 byte 크기의 testfile 이라는 이름의 파일을 생성한다.
$ touch testfile

추가) 윈도에서는 fsutil 을 이용한다. (윈도 XP 이상 지원)
참고: http://ntfaq.co.kr/2070
> fsutil file createnew [파일 이름] [파일 크기]
예) 1기가 크기의 파일 생성 (1024 x 1024 x 1024 bytes)
> fsutil file createnew c:\file1 1073741824

폴더 내 파일들의 특정 단어가 포함된 줄 나열

폴더 내 파일들에 포함된 특정 단어가 포함된 줄 나열하기

find 명령어 와 xargs 명령어, grep 명령어의 조합을 이용해 보았다.

예) 현재 폴더에 하위 폴더를 포함한 모든 파일에 #include 라는 단어가 포함된 라인을 출력한다.
$ find . | xargs grep --no-filename -s "#include"

위 명령어는 아래 명령어들이 복합되어 있다.
  • find 명령어: 현재 폴더부터 하위 폴더까지 모든 파일을 탐색한다.
  • xargs 명령어: find . 명령어로 찾은 파일명을 grep 명령어의 인자로 넣어준다.
  • grep 명령어: #include 라는 단어가 포함된 줄을 출력한다.

추가) 결과물의 정렬 및 중복된 줄 제거
$ find . | xargs grep --no-filename -s "#include" | sort | uniq -u

위 명령어는 아래 명령어들이 복합되어 있다.
  • sort: 결과물을 정렬한다.
    • -r 옵션을 주면 내림차순으로 정렬한다.
  • uniq: 중복된 내용 처리를 한다.
    • -u 옵션을 주면 중복된 항목 없이 모든 항목을 나열한다.
    • -d 옵션을 주면 중복된 항목들만 나열한다.