CodeLyst

리눅스 기본 명령어 정리-1

#Linux@jihak
thumb nail

Shell (Terminal)

  • 현재 사용중인 shell 보여주기
$ echo $SHELL

output: /bin/bash

  • 사용가능한 shell 보여주기
cat /etc/shells
  • output:
    #/etc/shells: valid login shells
    /bin/sh /bin/bash
    /usr/bin/bash
    /bin/rbash
    /usr/bin/rbash
    /usr/bin/sh
    /bin/dash
    /usr/bin/dash
    /usr/bin/tmux
    /usr/bin/screen

Directories

  • 현재 위치 출력
pwd

output: /home/user

  • cd(change directory)
cd /bin # goto bin directory cd .. # goto prarent directory cd ~ # goto home directory cd / # goto root directory
  • abosolute path: /home/user/bin
  • relative path: ../../etc/
  • mkdir(make directory), rmdir(remove empty directories)
    • options: -m, -p, -v
      • -m: 디렉토리를 생성할 때 권한 설정 default(rwx r-x r-x)
      • -p: 상위 경로도 함께 생성
      • -v: 디렉토리를 생성하고 생성된 디렉토리에 대한 메세지를 출력
mkdir programs # make programs directory ls # programs rmdir programs # remove promgrams directory ls #
  • ls
    • options: -l, -a, -S, -R
      • l: 자세한 내용 출력 (권한, 포함된 파일수, 쇼유자, 그룹, 수정일자, 파일이름)
      • a: 모든 파일 출력 (숨김된 파일도)
      • S: 파일 크기 순으로 정렬하여 출력
      • R: 하위 디렉토리도 출력
  • alias ** 현재 등록되어 있는 별칭 확인
alias
# alias 명령어 별칭 = '명령어' alias la = 'ls -a'

alias는 재부팅하면 다시 초기화 되어서 매번 다시 설정해줘야하는 불편함이 있으니
이러한 문제를 막을려면 alias를 ./bashrc 이나 .bash_aliases파일에 등록해서 사용하면 됩니다.

vi ~./bashrc
# ~/.bashrc alias cc='clear' # Source global definitions if [ -f /etc/bashrc ]; then . /etc/bashrc fi # Uncomment the following line if you don't like systemctl's auto-paging feature: # export SYSTEMD_PAGER= # User specific aliases and functions ~

파일 작성 이후에 source 명령어로 동기화를 해줍니다.

Copy files

# 파일, 폴더 복사 cp [options] src-file des-file cp [options] src-dir des-file
  • options: -f, -i, -r ...
    • f: 복사한 대상이 있으면 강제로 지우고 복사한다.
    • i: 복사한 대상이 있으면 덮어쓰기 여부를 사용자에게 확인한다.
    • r: 원본이 파일이면 복사, 디렉토리면 전체가 복사된다.
    • v: 복사 상태를 보여준다.
    • a: 원본의 파일 속성, 링크 정보들을 그대로 유지해서 복사한다.

Move or Rename files

# src-file을 dest-file로 이름 변경
mv src-file dest-file

# src-file을 dest-dir로 이동 
mv src-file(dir) dest-dir
  • options: f, -i, -n
    • -f: 이동할 파일이 이미 존재하면 강제로 덮어쓰기 한다.
    • -i: 이동할 파일이 존재하면 덮어쓰기 여부를 사용자에게 확인한다.
    • -n : 이동시킬 파일이 이미 존재하면 덮어쓰기 하지 않는다..

Remove files

# filename을 지운다.
rm [option] filename
  • options: -f,-i,-r
    • f: 강제로 제거한다.
    • i: 제거할지 안할지 물어본다.
    • r: recursive하게 서브디렉토리에 있는 파일까지 삭제한다.

View Files

# visual editor 사용 # 파일 없으면 생성 vi filename
# file내용을 screen에 출력 cat filename
# screen 단위로 출력 more filename
less filename # screen 출력의 앞뒤 페이지로 스크롤 가능 # vi 스크롤 명령어 사용 가능
# 파일의 앞부분만 출력 head filename # 파일의 뒷부분만 출력 tail filename
# 파일의 수정시간을 갱신, 파일이 없으면 새로생성 touch filename
# 파일의 줄 수, 단어 수 , 문자수 출력 wc [-lwc] filename
# file1과 file2의차이점과 같은점을 비교하여 보여줌 diff file1 file2
# 파일의 속성을 보여줌 file filename

Find & Grep

  • 파일 찾기
find [option] directory [-name, -type, -user] filename
  • 파일 내에서 string 찾기
grep string filename grep [option] [pattern] [filename]

pattern에서 정규표현식 사용가능

# 특정 파일에서 a,b,c로 시작하는 단어를 모두 찾는다. grep '[a-b]' filename # 특정 파일에서 a로 시작하는 모든 단어를 찾는다. grep 'a*' filename
- options
    - c : 일치하는 행의 수를 출력한다.
    - i : 대소문자를 구별하지 않는다.
    - v : 일치하지 않는 행만 출력한다.
    - n : 포함된 행의 번호를 함께 출력한다.
    - l : 패턴이 포함된 파일의 이름을 출력한다.
    - w : 단어와 일치하는 행만 출력한다.
    - x : 라인과 일치하는 행만 출력한다.
    - r : 하위 디렉토리를 포함한 모든 파일에서 검색한다.
    - m 숫자 : 최대로 표시될 수 있는 결과를 제한한다.
    - E : 찾을 패턴을 정규 표현식으로 찾는다.
    - F : 찾을 패턴을 문자열로 찾는다.