ABOUT ME

Today
Yesterday
Total
  • Docker를 통해 Linux 설치 및 tail,grep 명령어 실습
    linux 2024. 9. 23. 15:18
    반응형

    Docker를 통한 Linux 설치

    docker pull ubuntu

     

    ubuntu 컨테이너 실행

    docker run -it ubuntu /bin/bash

     

    ubuntu 컨테이너 이름 지정 후 생성

    docker run --name my_custom_name -it ubuntu /bin/bash

     

    연습 폴더 생성 및 접근

    ls
    mkdir test_folder
    cd test_folder
    touch file.txt

     

     

    이미 생성되어있는 컨테이너가 있다면 exec를 통해서 cmd창을 사용하여 리눅스환경으로 접속

    docker exec -it my_custom_linux /bin/bash

    파일 생성 및 명령어 연습 방법

    echo "첫 번째 줄입니다." > example.txt
    echo "두 번째 줄입니다." >> example.txt
    echo "세 번째 줄입니다." >> example.txt

     

    1. Tail명령어

    tail example.txt

     

    Tail의 경우 기본적으로 파일의 마지막 10줄을 출력한다. 

     

    2. 특정 줄 수를 보고 싶다면 -n옵션을 사용하면 된다.

    tail -n 2 example.txt

     


    1. grep명령어

    grep "두 번째" example.txt

     

    -i 옵션을 추가하면 대소문자를 구분하지 않고 검색할 수 있습니다.

    grep -i "첫" example.txt

     

     

     

    파일에 데이터 추가하기: 더 많은 데이터를 연습하고 싶다면 for 루프를 사용하여 많은 줄을 추가할 수 있습니다.

    for i in {1..100}; do echo "라인 $i" >> largefile.txt; done

     

     

    tail largefile.txt

    해당 명령어를 입력한다면 아래와같이 출력되어 나온다. 기존에 알아봤듯이 tail의 경우 마지막 10줄을 기본적으로 출력하기 때문이다. 

    라인 91
    라인 92
    라인 93
    라인 94
    라인 95
    라인 96
    라인 97
    라인 98
    라인 99
    라인 100

     

     

    tail -n 20 largefile.txt

    이와같이 -n 옵션을 사용하여 특정 줄 수를 지정해준다면

    라인 81
    라인 82
    라인 83
    라인 84
    라인 85
    라인 86
    라인 87
    라인 88
    라인 89
    라인 90
    라인 91
    라인 92
    라인 93
    라인 94
    라인 95
    라인 96
    라인 97
    라인 98
    라인 99
    라인 100

    이와같이 출력된 결과를 확인 할 수 있다.

     


    실습을 위한 파일 생성 및 추가

    echo "This is the first line of the file." > english_file.txt
    echo "The second line contains some text." >> english_file.txt
    echo "Here is the third line with more text." >> english_file.txt
    echo "This is the fourth line." >> english_file.txt
    echo "Another line with some words." >> english_file.txt
    echo "The final line of the file." >> english_file.txt

     

    tail 명령어로 파일의 마지막 10줄을 조회. 해당 파일의 경우 6줄만 존재하기 때문에 전체가 출력된다. 

    tail english_file.txt

     

    1. 특정 줄 수 출력을 위해 -n 옵션을 사용

    tail -n 3 english_file.txt

     

     

    2. grep명령어를 사용하여 특정 단어가 포함된 줄을 검색

    grep "line" english_file.txt

     

     

    3. 대소문자를 구별하지 않고 검색

    grep -i "this" english_file.txt

     

     

    4. tail과 grep을 조합하여 출력

    tail -n 5 english_file.txt | grep "line"

    tail 명령어의 결과에서 특정 단어를 검색하고 싶을 때는 tail과 grep을 조합해서 사용할 수 있습니다. 예를 들어, 마지막 5줄 중에서 "line"이라는 단어가 포함된 줄을 검색하려면 다음과 같이 사용할 수 있습니다.

     

    5. 줄 번호와 함께 검색 결과 출력

    grep -n "line" english_file.txt

    1:This is the first line of the file.
    2:The second line contains some text.
    3:Here is the third line with more text.
    4:This is the fourth line.
    5:Another line with some words.
    6:The final line of the file.

     

    6. 특정 단어가 포함되지 않은 line을 출력

    grep -v "line" english_file.txt

     

     

    7.단어 단위로 검색

    grep -w "text" english_file.txt

     

    반응형

    댓글

Designed by Tistory.