[Linux] 명령어 1

zzwon1212·2023년 9월 20일
0

Linux

목록 보기
2/11

Linux를 다른 운영체제에서 이용하는 방법에는 가상 머신(VM ware, VirtualBox), 멀티 부팅, WSL 등이 있다. 나는 Windows 11 운영체제에서 WSL2(Windows Subsystem for Linux 2)를 활용하여 Ubuntu 18.04.6 LTS를 이용하고 있다.

1. path

pwd # print working directory
cd 경로 # change directory. 경로 생략 시 home으로 이동
cd / # root dir
cd ~ # home dir
cd - # previous dir
cd /usr/local/bin # root dir(/)로 시작하는 absolute path
cd ./../tmp # current dir(.)로 시작하는 relative path. ==../tmp
# tab으로 command completion을 활용해보자.

2. ls

ls # list file
ls -l
ls -al
  • ls [-altriRr] [파일명 ...]
    • 출력: filetype, mode, links, owner: user/group, size, modified date(mtime), filename
    • a: all, l: long, t: sort by mtime, r: reverse
    • eg: ls -a -l, ls -al, ls -ltr, ls -i
  • filetype: [-dbclps]
    • -: regular file
    • d: directory
    • l: symbolic link
  • file mode bit: UNIX의 파일 권한을 나타내는 3+9 bit 체계로, 화면에는 9칸만 표시된다.
    • 3+9bit
      • 3bit: SetUID, SetGID, Sticky bit(보안과 관련있는 허가 권한)
      • 9bit: owner, group, others의 접근 권한
    • 표기 방법
      • Symbolic mode: "rwx" symbol로 표기하는 방식
      • Octal mode: bit를 8진수법으로 표기하는 방식으로 실무에서 많이 쓰인다.
      • r=4,w=2,x=1r = 4, w = 2, x = 1이다. 따라서 '750'은 'rwxr-x---'를 의미한다.
      • directory인 경우: r은 directory 내부 목록을 확인할 수 있음을, x는 directory 내부 파일에 접근할 수 있음을 의미한다.
    • 파일 생성 시 기본값: umask 값을 뺀 나머지. umask는 보통 022이다.
      • directory: 777 - 022 = 755가 기본 mode
      • file: 666 - 022 = 644가 기본 mode
      chmod 664 testdir # change mode

3. directory

mkdir -p work/testdir # make directory
rmdir [-p] # remove directory. empty directory인 경우에만 삭제 가능.
rm -rf # file과 directory를 함께 지움. rmdir보다 자주 사용됨.

4. cp, mv, rm

cp ~/.bashrc ~bashrc_example
cd; ls

mv ~/bashrc_example ~/oldbashrc # move, rename(파일명까지 명시할 경우)
ll !$

rm ~/old_bashrc

5. chown, chgrp

ls -l helloworld
chown root helloworld; ls -l helloworld # change owner

6. file, stat

file <file>

stat [option] <file>
  • file: 파일의 타입 확인
    • 파일이 가지고 있는 고유의 표식을 근거로 파일의 종류를 분류한다. 때문에 UNIX 계열에서 파일의 확장자는 중요하지 않다.
    • magic 데이터: /usr/share/file/magic
  • stat: file의 meta data를 출력한다.
    • meta data: 내용이 아닌 수식하는 정보로 파일명, 생성 시간, 권한 등이 있다.
      • Modify: file의 data가 변경된 시간, mtime
      • Change: file의 meta data가 변경된 시간, ctime
      • Birth: file이 생성된 시간

7. touch

touch emptyfile
stat <ALT-.> # == stat emptyfile

touch emptyfile
stat <ALT-.> # == stat emptyfile

  • file의 메타 정보(주로 시간)를 업데이트 한다.
  • file이 존재하지 않는 경우 빈 파일을 생성한다.

8. find

  • 중요한 명령어이다.

    조건설명
    n정확히 n인 경우를 검색
    +nn보다 큰 경우를 검색
    -nn보다 작은 경우를 검색
  • man find을 통해 아래 주요 조건 명령어를 활용해보자.

    • -name filename, -maxdepth level, -mindepth level, -size n, -mtime n, -mmin n, -inum n, -samefile file
    • -name은 패턴을, -size는 +, -를 사용할 수 있다
    mkdir ~/tmp; cd !$
    for i in {8..21}; do dd bs=100000 count=$i if=/dev/zero of=./${i}00k.dat; done
    find . -name '[89]*k.dat' # 8 또는 9
    find . -name '*k.dat' -a -size 1M
    find . -name '*k.dat' -size +1500k -size -1800k
    find . -mtime -1 -size +1M
    • -a: 'AND'(default이므로 생략 가능), -o: 'OR'
  • 검색 후 작업 지시: find ... -exec 명령어 \;, find ... -exec 명령어 \+

    find . '*.tmp' -exec rm {} \;
    find . '*.tmp' -exec rm -rf {} \;
    find . '*.tmp' -exec rm -rf {} \+
    • \;: 명령을 하나씩 실행, \+: 명령을 한 번에 실행

📙강의 - 코인즈월렛 기술이사 김선영

profile
JUST DO IT.

0개의 댓글

관련 채용 정보