본문 바로가기

Backend/linux

[linux - wc] 글자 수를 세는 명령어

728x90

리눅스에서 특정 키워드에 대해서 글자 수 를 세어야할 때가 있다.

wc 명령을 이용하면 쉽게 해결이 가능하다.

 

wc command

wc 명령어는 글자수를 세는 명령어이다.

일단 man wc로 명세를 확인할 수 있다.

NAME
     wc -- word, line, character, and byte count
     
SYNOPSIS
     wc [-clmw] [file ...]
     
DESCRIPTION
     The wc utility displays the number of lines, words, and bytes contained in each input file, or
     standard input (if no file is specified) to the standard output.  A line is defined as a string
     of characters delimited by a <newline> character.  Characters beyond the final <newline> charac-
     ter will not be included in the line count.
     blah blah blah~~~~~
생략

wc는 글자, 단어, 줄, 바이트 수를 출력하는 명령어다.

파이프라인을 이용해서 다른 명령어의 결과를 입력으로 이용한다.

 

옵션

직관적인 옵션 4가지가 있다.

각각 카운트를 어떤 기준으로 할지 결정하는 것이다.

  • -c : byte, 파일 바이트 수
  • -l : line, 파일 줄(라인) 수
  • -m : character, 파일 글자 수
  • -w : word, 파일 단어 수

사용 방법

wc 명령을 수행하고자 하는 파일을 명령어 후단에 기재해서 이용하거나

wc -w fine_name

다른 명령어의 결과를 파이프라인을 이용해서 wc 명령으로 전달해서 이용할 수 있다.

cat file_name | wc -l

 

사용 사례

  1. 특정 디렉토리의 특정 확장자 파일의 개수 찾기
    ls -al ./ | grep -o *.zip | wc -l
  2. find와 조합해서 하위 디렉토리까지 확장해서 특정 확장자 파일 개수 찾기
    find ./ -name '*.log' | wc -l

리눅스 명령어가 모두 그렇듯 다른 명령어와 잘 조합해서 이용하면 된다.

 

감사합니다.