본문 바로가기

Linux

Linux 자주 사용되는 명령어

728x90

명령어의 종류

'type 명령어'를 통해 명령어의 종류 확인 가능.

 

1. 실행 파일

type rm
rm is hashed (/bin/rm)

 

2. 셸 내장 명령어

type cd
cd is a shell builtin

 

3. 사용자 정의(alias) 명령어

type ls
ls is aliased to `ls --color=auto'

 


명령어 매뉴얼

# 쉘 내장명령어 매뉴얼
help 명령어

# 실행 파일 매뉴얼
명령어 -h / --help
man 명령어

command [a] [b] : command / command a / command b / command a b /

command  [a|b] : command / command a / command b

 

man (1/2/3) 명령어

  • 1: 사용자 명령어(실행 파일 명령어) → 생략 가능
  • 2: 리눅스 시스템콜 API
  • 3: C언어의 라이브러리 API

사용자 지정 명령어

alias now='date;cal'
now

Fri Mar 15 00:10:02 KST 2024
     March 2024
Su Mo Tu We Th Fr Sa
                1  2
 3  4  5  6  7  8  9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31

 

alias 사용자 정의 이름='명령어1;명령어2;명령어3'

*주의 : 등호 양옆에 공백이 없어야함.

 

<<
alias
>>
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l='ls -CF'
alias la='ls -A'
alias ll='ls -alF'
alias ls='ls --color=auto'
alias now='date;cal'

 

alias 명령어를 사용하면 현재 정의된 사용자 명령어 확인 가능.

 

unalias now

사용자 정의 명령어 해제 가능.


자주 사용하는 명령어 정리

clear

 

현재 터미널 출력된 내용 비우기.

* CTRL + l 도 같은 기능 수행.

date
>> Thu Mar 14 21:30:57 KST 2024

 

현재 날짜 출력

cal

     March 2024
Su Mo Tu We Th Fr Sa
                1  2
 3  4  5  6  7  8  9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31

현재 달의 달력 출력

 

 

clear;date;cal

세미콜론(;)을 통해 한 줄에 다수의 명령어 수행 가능

 

history

    1  bash
    2  $SHELL
    3  ls -R
    4  ls
    5  cd /root
    6  su
    7  su
    8  cd /
    9  ls -R
   10  vim
   11  ls -R
   12  cd /
   13  ls -R
   14  ps
   15  ps -ef
   16  top
   17  date
   18  cal
   19  clear
   20  clear; date; cal
   21  history

지금까지 사용한 명령어 기록 조회

* !16 입력 시 명령어 top 수행

 

exit

셸 종료

 

cat names.txt -n -b

     1  Names
     2  James

     3  Tom
     4  Dale
     5  Linux

 

catenate의 약자로 여러 파일을 연결할 때 사용하는 명령어.

정규 파일의 내용을 확인할 때도 사용된다.

  • -n : 파일의 행 번호도 출력
  • -b : 비어 있지 않은 행에 대해서만 행번호를 매긴다.
cat names.txt number.txt

Names
James

Tom
Dale
Linux
Number
01011234123
01011111111
01012222222

두 파일(names.txt와 number.txt)의 내용을 catenate하여 출력한다.

 

more bible.txt

The Holy Bible, Berean Standard Bible, BSB is produced in cooperation with Bible Hub, Discovery Bible, OpenBible.com, an
d the Berean Bible Translation Committee.
This text of God's Word has been dedicated to the public domain. Free resources and databases are available at BereanBib
le.com.
Verse   Berean Standard Bible
Genesis 1:1     In the beginning God created the heavens and the earth.
Genesis 1:2     Now the earth was formless and void, and darkness was over the surface of the deep. And the Spirit of Go
d was hovering over the surface of the waters.
Genesis 1:3     And God said, “Let there be light,” and there was light.
Genesis 1:4     And God saw that the light was good, and He separated the light from the darkness.
Genesis 1:5     God called the light “day,” and the darkness He called “night.” And there was evening, and there was mor
ning—the first day.
Genesis 1:6     And God said, “Let there be an expanse between the waters, to separate the waters from the waters.”
Genesis 1:7     So God made the expanse and separated the waters beneath it from the waters above. And it was so.
Genesis 1:8     God called the expanse “sky.” And there was evening, and there was morning—the second day.
Genesis 1:9     And God said, “Let the waters under the sky be gathered into one place, so that the dry land may appear.
” And it was so.

more 명령어는 정규 파일의 내용을 페이지 단위로 출력한다.

  • 스페이스바 : 다음 페이지 출력
  • b : 이전 페이지 출력
  • Enter : 다음 줄 이동
  • q : 명령어 종료
less bible.txt

The Holy Bible, Berean Standard Bible, BSB is produced in cooperation with Bible Hub, Discovery Bible, OpenBible.com, and the Berean Bible Translation Committee.
This text of God's Word has been dedicated to the public domain. Free resources and databases are available at BereanBible.com.
Verse   Berean Standard Bible
Genesis 1:1     In the beginning God created the heavens and the earth.
Genesis 1:2     Now the earth was formless and void, and darkness was over the surface of the deep. And the Spirit of God was hovering over the surface of the waters.
Genesis 1:3     And God said, “Let there be light,” and there was light.
Genesis 1:4     And God saw that the light was good, and He separated the light from the darkness.
Genesis 1:5     God called the light “day,” and the darkness He called “night.” And there was evening, and there was morning—the first day.
Genesis 1:6     And God said, “Let there be an expanse between the waters, to separate the waters from the waters.”
Genesis 1:7     So God made the expanse and separated the waters beneath it from the waters above. And it was so.
Genesis 1:8     God called the expanse “sky.” And there was evening, and there was morning—the second day.
Genesis 1:9     And God said, “Let the waters under the sky be gathered into one place, so that the dry land may appear.” And it was so.

 

less는 more 명령어의 확장판이다.

less 명령어가 더 편리한 인터페이스를 제공한다.

 

  • 스페이스바, Page Down, f : 다음 페이지 출력
  • b, Page Up : 이전 페이지 출력
  • Enter : 다음 줄 이동
  • 위/아래 방향키, j/k : 한줄씩 이동
  • g : 파일의 최상단으로 이동
  • G : 파일의 최하단으로 이동
  • q : 명령어 종료
head bible.txt -n 3
The Holy Bible, Berean Standard Bible, BSB is produced in cooperation with Bible Hub, Discovery Bible, OpenBible.com, and the Berean Bible Translation Committee.
This text of God's Word has been dedicated to the public domain. Free resources and databases are available at BereanBible.com.
Verse   Berean Standard Bible

 

  • head : 파일의 첫 부분만 출력하는 명령어
  • -n : 출력할 줄의 수(default = 10)
tail bible.txt -n 3
1 Thessalonians 5:9     For God has not appointed us to suffer wrath, but to obtain salvation through our Lord Jesus Christ.
1 Thessalonians 5:10    He died for us so that, whether we are awake or asleep, we may live together with Him.
1 Thessalonians 5:11    Therefore encourage and build one anoth
  • tail: 파일의 끝 부분만 출력하는 명령어
  • -n : 출력할 줄의 수(default = 10)
> log.txt

 

리다이렉션을 통해 log.txt 생성

리다이렉션의 input이 아무것도 없으므로 빈 파일 log.txt만 생성됨.

 

rm -i names.txt
rm: remove regular file 'names.txt'?
  • rm : names.txt 삭제 명령어
  • -i : interactive. 삭제 시 사용자에게 재확인함.
<< file names.txt
>> names.txt: ASCII text

<< file subdir
>> subdir: directory

 

file 명령어 : 파일의 종류를 확인한다.

 

ls -l -F
total 4352
-rwxr-xr-x 1 hello hello 4124858 Mar 14 21:54 bible.txt*
-rwxr-xr-x 1 hello hello      56 Mar 14 14:30 hello.sh*
-rwxr--r-- 1 hello hello     143 Mar 14 16:15 if.sh*
-rwxr--r-- 1 hello hello     141 Mar 14 16:26 if2.sh*
-rw-r--r-- 1 hello hello      28 Mar 14 21:48 names.txt
-rw-r--r-- 1 hello hello      28 Mar 14 22:03 names2.txt
-rw-r--r-- 1 hello hello      43 Mar 14 21:49 number.txt
-rwxr--r-- 1 hello hello      91 Mar 14 15:32 read.sh*
-rwxr--r-- 1 hello hello      40 Mar 14 15:33 read_int.sh*
drwxr-xr-x 1 hello hello    4096 Mar 14 22:05 subdir/
-rwxr--r-- 1 hello hello      59 Mar 14 14:52 variable.sh*
  • ls 명령어 : 해당 디렉터리에 존재하는 파일 출력
  • -l : 자세하게 출력
    • -로 시작 : 파일이 정규 파일임을 의미
    • d로 시작 : 파일이 디렉터리임을 의미
  • -a : 숨긴 파일까지 모든 파일 출력(*숨긴 파일은 파일명이 .으로 시작한다. ex: .hidden)
  • -F : 파일의 포맷  출력
    • 끝에 /가 붙는 경우 : 파일이 디렉터리임을 의미
    • 끝에 *이 붙는 경우 : 파일이 실행 파일임을 의미
  • -d : 디렉터리 자체를 출력
    • ls -ld subdir
cd
bithub@DESKTOP-AAAAAA:~$

 

cd만 단독 사용시 홈(~)으로 이동.

 

rm -r subdir

 

-r : 디렉터리를 recursive하게 삭제. 폴더가 비어있지 않아도 삭제 가능.

 

<< wc file.txt
>> 5  5 29 names

 

명령어 wc는 파일의 행의 갯수, 단어의 갯수, 문자의 갯수를 출력한다.

  • -l : 행의 갯수 출력
  • -w : 단어의 갯수 출력
  • -c : 문자의 갯수 출력

* 주의 : 문자의 갯수 출력 시 공백과 EOF도 포함한다.

 

sleep 5 # 단위:초

 

5초간 sleep