[UNIX CLI] Command Line(1)

3 minute read


UNIX COMMAND LINE

  • UNIX CLI 정리(1)
  • argument(인자)는 커맨드가 작동할 대상을 지정하기 위해 사용
  • option(옵션)은 커맨드가 구체적으로 어떤 방식으로 동작할지를 지시하기 위해 사용
    • 옵션은 하이픈(-)이 붙음
    • 옵션 중에는 옵션을 적고 한 칸 띄운 다음 옵션에 대한 인자(or value)를 적어야하는 것들이 존재
    • 하이픈 뒤에는 여러 옵션을 연속적으로 사용 가능
      • 값을 줘야 하는 옵션이 있다면 값을 줘야 하는 옵션을 가장 뒤에 써야 명령이 정상적으로 작동
  • man [command]을 통해 명령어에 대한 공식 메뉴얼을 볼 수 있음
  • cd를 통해 directory를 변경할 수 있음
  • ls를 통해 directory 내의 directory/file 볼 수 있음
  • ls -l를 통해 현재 directory 내 파일들의 detail info에 대해 알 수 있음
  • mkdir를 통해 directory 만들 수 있음
  • touch를 통해 새 파일 만들 수 있음
  • rm를 통해 파일 삭제할 수 있음
  • mv를 통해 파일을 옮기거나 파일/directory의 이름을 변경할 수 있음
  • cp를 통해 파일/directory를 복사+붙여놓기 할 수 있음
  • cat를 통해 파일 1개 이상을 이어 볼 수 있음
  • less를 통해 파일 1개 이상을 각각 볼 수 있음
  • head를 통해 파일의 앞 부분만 볼 수 있음
  • tail를 통해 파일의 뒷 부분만 볼 수 있음
  • history를 통해 지금까지 사용한 명령어를 모두 볼 수 있음

argument & option

# '2020' is an argumnet
cal 2020

# '-y' is an option
cal -y
# 2 is give after option -B which stands for before
cal -B 2

# returns a calender from two months before to 3 months after
cal -B 2 -A 3

# returns a calender counting the days in a year from two months before to 3 months after
cal -B 2 -A 3 - j

# same as above
cal -B 2 -jA 3
cal -jB 2 -A 3

man

# official manual of cal
man cal

pwd

# print working directory
pwd

cd

  • tab 키를 통해 자동완성 할 수 있음
  • control + a를 통해 줄의 가장 앞쪽으로 갈 수 있음
  • control + e를 통해 줄의 가장 뒤쪽으로 갈 수 있음
  • clear를 통해 command 창을 모두 지울 수 있음
# change directory
cd Jupyter_Projects/blog

# change to parent directory
cd ../

# change to parent's parent directory
cd ../..

# change to root directory
cd /

# change to home directory
cd ~

# change to previous directory
cd -

ls

  • 가장 처음에 오는 것을 통해 해당 파일의 종류 알 수 있음
  • - : 일반 파일
  • d : 디렉토리
  • c : 캐릭터 장치 파일
  • b : 블록 장치 파일
  • s : 소켓
  • p : 파이프
  • l : 심볼릭 링크
# show list with details
ls -l

# show all file 
ls -a

# show all file as list with details
ls -al # ls -la or ls -l -a

# show file's info or files inside a directory
ls -l [filename/directory_name]

# show directory's info
ls -ld [directory_name]

# show every file and directory in the current level, then everything in each sub-directory, and so on
ls -R

# print a / after the name of every directory and a * after the name of every runnable program
ls -F


mkdir

# make directory
mkdir [directory_name]

rm

  • directory 삭제시 -i 옵션을 사용해 중요한 파일을 지우지 않도록 확인
# delete file
rm [file_name]

# delete directory
rm -r -i [directory_name]

mv

# move file
mv [file_name] [directory_name]

# use -i option to prevent overwriting
mv -i [file_name] [directory_name]

# rename file/directory name
mv [file/directory_name] [new_directory_name]

# move directory and change name of file
mv -i [file_name] [directory_name/new_file_name]

cp

  • -i 옵션을 항상 입력해 overwriting이 일어나지 않도록 방지
# copy & paste file
cp -i [file_name] [duplicated_file_name]

# copy & paste directory
cp -ir [directory_name] [duplicated_directory_name]

cat

  • ⬇️ : 아래로 한줄 이동
  • ⬆️ : 위로 한줄 이동
  • spacebar : 아래로 한 페이지 이동
  • b : 위로 한 페이지 이동
  • G : 가장 마지막으로 이동
  • g : 가장 처음으로 이동
# view photograph_lyric file
cat photograph_lyric

# view photogrpah_lyric & shiver_lyric file
cat photograph_lyric shiver_lyric file

less

  • :n : view next file
  • :p : view previous file
  • ⬇️ : 아래로 한줄 이동
  • ⬆️ : 위로 한줄 이동
  • spacebar : 아래로 한 페이지 이동
  • b : 위로 한 페이지 이동
  • G : 가장 마지막으로 이동
  • g : 가장 처음으로 이동
# view photograph_lyric file
less photograph_lyric

# view photogrpah_lyric & shiver_lyric file
less photograph_lyric shiver_lyric file

head & tail

  • -n 옵션을 통해 출력될 line 수를 지정할 수 있음
# view 20 line of photograph_lyric file
head -n 20 photograph_lyric

# view the last of 10 line of photogrpah_lyric file
tail -n 10 photograph_lyric

history

  • ![number]를 통해 해당 command 불러 올 수 있음
  • ![most_recent_command]를 통해 가장 최근에 해당 command를 불러 올 수 있음
# view history of commands
history

# view the last of 10 line of photogrpah_lyric file
![number]

# view the top 10 line of iris.csv
head iris.csv

# view the last 10 line of iris.csv again
!head