linux advanced

minseok·2024년 12월 6일
post-thumbnail

pipes

  • '|' 기호
  • send the output of one program, progess, or command directly into the input of another (2개의 프로그램이나 프로세스같은 것을 연결 해줌)
  • '|'기호 기준으로 좌측 ouput을 우측에 input한다고 보면 된다.

ex)

ls | grep hello
ls : 현재 디렉토리의 파일 출력
grep hello : hello 파일명을 가진 파일 출력

ps aux | grep httpd | aws '{print $2}'
ps aux : 프로세스 리스트를 출력
grep httpd : httpd 프로세스를 잡는다.
aws '{print $2}' : 해당 row의 2번째 열을 출력





grep

  • 파일내의 특정 문자열 찾기
  • global regular expression print

ex)

grep "apple" fruits.txt
fruits.txt 파일에서 apple 문자열을 찾음

grep -r apple
현재 디렉토리로부터 apple 문자열 가지고 있는 파일을 찾음

grep -E "appl(e|es)" fruits.txt
fruits.txt 파일에서 "apple"이나 "apples" 문자열 찾음





wc

  • word count
  • -l 옵션을 많이 사용함 (라인 갯수 출력)

ex)

fruits.txt

apple
orange
melon
apples
Apple

입력 -> wc fruits.txt
결과 -> 5(행) 5(단어) 32(문자) fruits.txt(파일명)

입력 -> echo "hello world" | wc
결과 -> 1(행) 2(단어) 12(문자)





head & tail

💬 head

  • head [OPTION]... [FILE]...
  • beginning section of text
  • 기본적으로 10개 라인

💬 tail

  • outputs the end section of text
  • 기본적으로 10개 라인
  • ⭐️ -f 옵션(follow mode), 파일의 끝을 계속 읽는다. 주로 서버 로그 파일에 사용
  • ⭐️ --pid=[PID] 옵션, 프로세스가 존재하는 동안만 읽는다.





diff & cmp

💬 diff

  • 2개 파일의 차이를 보여줌
  • github에서 본 것 같은 느낌
ex)
file : t1.txt
hello
hello world25
world

---
file : t2.txt
hello
hello world25
wrwr
rr

diff -u t1.txt t2.txt

--- t1.txt	2024-12-06 01:47:37.564972621 +0000
+++ t2.txt	2024-12-06 01:47:51.032822226 +0000
@@ -1,3 +1,4 @@
 hello
 hello world25
-world
+wrwr
+rr

💬 cmp

  • 다른 점을 바이트 값을 사용해서 보여줌

cmp t1.txt t2.txt
결과 : t1.txt t2.txt differ: byte 22, line 3

cmp -b t1.txt t2.txt
결과 : t1.txt t2.txt differ: byte 22, line 3 is 157 o 162 r





awk(어크?)

column base text mainpulation

  • pattern matching : sed 커맨드와 유사하다.
  • built in variables : FS(field separator), OF(output field separator), NR(current record number)
  • arthmetic and string operations : 산수, 문자열 기능
  • associative arrays : 배열 지원

ex)

awk '{print $1, $3}' file1.txt
file1.txt 파일의 첫 번째, 세 번째 컬럼($1, $3) 확인

awk '{sum += $2} END {print sum}' file1.txt
file1.txt 파일의 두 번째 컬럼을 모두 더한 후 출력한다.

awk '$2 > 20' file1.txt
file1.txt 파일의 두 번째 컬럼이 20보다 큰 행을 출력

awk '$2 > 20 {print $0}' file1.txt > temp.txt && mv temp.txt file1.txt
file1.txt 파일에서 두 번째 컬럼이 20보다 큰 행을 temp.txt로 입력 한 다음에 temp.txt를 file1.txt로 이동한다.(덮어쓴다.)

profile
즐겁게 개발하기

0개의 댓글