본문 바로가기
SPRING BOOT

[Linux]리눅스 기본 명령어

by Red서카 2022. 11. 20.

리눅스 기본 명령어들, 자주 쓰는 명령어들을 정리해보았습니다.

모든 명령어는 명령어 뒤에 --help 옵션을 주면 자세한 사용 방법이 나오니

기본적인 사용법만 기억하시고 필요에 따라서 --help를 이용해주세요.

 

웹에서 실제로 리눅스를 설치하지 않고도 명령어 입력이 가능한 사이트입니다.

아래의 커맨드를 직접 입력해서 연습해보세요.

https://bellard.org/jslinux/

 

pwd

현재 작업중인 디렉토리 정보 출력

localhost:~# pwd
/root

 

ls 

디렉토리 목록 확인

localhost:~# ls
bench.py    hello.c     hello.js    readme.txt

localhost:~# ls -l
total 16
-rw-r--r--    1 root     root           114 Jul  6  2020 bench.py
-rw-r--r--    1 root     root            76 Jul  3  2020 hello.c
-rw-r--r--    1 root     root            22 Jun 26  2020 hello.js
-rw-r--r--    1 root     root           151 Jul  6  2020 readme.txt

localhost:~# ls -al
total 40
drwxr-xr-x    5 root     root           237 Jan  9  2021 .
drwxrwxrwx   21 root     root           461 Nov 20 15:49 ..
-rw-------    1 root     root            47 Nov 20 15:49 .ash_history
drwx------    3 root     root            61 Jul  6  2020 .cache
drwx------    5 root     root           124 Jul  6  2020 .mozilla
drwxr-xr-x    4 root     root           202 Jul  6  2020 .wine
-rw-r--r--    1 root     root           114 Jul  6  2020 bench.py
-rw-r--r--    1 root     root            76 Jul  3  2020 hello.c
-rw-r--r--    1 root     root            22 Jun 26  2020 hello.js
-rw-r--r--    1 root     root           151 Jul  6  2020 readme.txt

 

mkdir

디렉토리 생성

localhost:~# ls
bench.py    hello.c     hello.js    readme.txt

localhost:~# mkdir testdir
localhost:~# ls
bench.py    hello.c     hello.js    readme.txt  testdir

 

cd

경로 이동

절대 경로와 상대 경로로 이동 가능하다.

절대경로란 최상위 디렉토리 (/)부터 시작해서 목표 디렉토리까지 가는 경로를 전부 기술하는 방식이다.

절대경로로 경로를 기술할 때에는 항상 맨 앞에 최상위 디렉토리 (/)가 붙는다는 것을 명심하자.

 

상대경로는 ‘현재 자신이 있는 위치를 기준으로’ 이동을 하는 것이다.

그리고 현재 자신이 있는 위치는 .(마침표) 로 표기한다.

이전(상위) 디렉토리는 .. 으로 표기한다.

localhost:~# ls -l
total 20
-rw-r--r--    1 root     root           114 Jul  6  2020 bench.py
-rw-r--r--    1 root     root            76 Jul  3  2020 hello.c
-rw-r--r--    1 root     root            22 Jun 26  2020 hello.js
-rw-r--r--    1 root     root           151 Jul  6  2020 readme.txt
drwxr-xr-x    2 root     root            37 Nov 20 15:50 testdir

localhost:~# cd testdir/
localhost:~/testdir# pwd
/root/testdir

localhost:~/testdir# cd ..
localhost:~# pwd
/root

 

cp 

파일 혹은 디렉토리를 복사

디렉토리를 복사할때는 -r 옵션을 주어야함

localhost:~# ls
bench.py    hello.c     hello.js    readme.txt  testdir

localhost:~# cp hello.js hellocp.js
localhost:~# ls
bench.py    hello.c     hello.js    hellocp.js  readme.txt  testdir

localhost:~# cp -r testdir/ testcpdir
localhost:~# ls
bench.py    hello.js    readme.txt  testdir
hello.c     hellocp.js  testcpdir

 

mv

파일 혹은 디렉토리 이동

실제로 원하는 위치로 이동할때도 사용하지만, 이름을 변경하는 용도로도 사용한다.

cp와는 달리 디렉토리를 이동할때도 별다른 옵션이 필요 없다.

localhost:~# ls
bench.py    hello.js    readme.txt  testdir
hello.c     hellocp.js  testcpdir

localhost:~# mv hellocp.js helloworld.js
localhost:~# ls
bench.py       hello.js       readme.txt     testdir
hello.c        helloworld.js  testcpdir

localhost:~# mv testcpdir/ testworlddir
localhost:~# ls
bench.py       hello.js       readme.txt     testworlddir
hello.c        helloworld.js  testdir
localhost:~#

 

rm 

파일이나 디렉토리를 삭제

디렉토리를 삭제할때는 r 옵션을 주어야 한다.

-f 옵션을 주면 사용자에게 삭제 여부를 묻지 않고 바로 삭제한다.

디렉토리를 삭제할 때에는 하위 디렉토리까지 모두 삭제되므로 유의하자.

localhost:~# ls
bench.py       hello.js       readme.txt     testworlddir
hello.c        helloworld.js  testdir

localhost:~# rm helloworld.js
localhost:~# ls
bench.py      hello.js      testdir
hello.c       readme.txt    testworlddir

localhost:~# rm -rf testworlddir/
localhost:~# ls
bench.py    hello.c     hello.js    readme.txt  testdir

 

touch

파일이나 디렉토리의 최근 업데이트 일자를 현재 시간으로 변경한다.

최근 업데이트 일자는 ls -l 명령을 통해 확인할 수 있다.

파일이나 디렉토리가 존재하지 않으면 빈 파일을 만든다.

localhost:~# ls -l
total 20
-rw-r--r--    1 root     root           114 Jul  6  2020 bench.py
-rw-r--r--    1 root     root            76 Jul  3  2020 hello.c
-rw-r--r--    1 root     root            22 Jun 26  2020 hello.js
-rw-r--r--    1 root     root           151 Jul  6  2020 readme.txt
drwxr-xr-x    2 root     root            37 Nov 20 15:50 testdir

localhost:~# touch readme.txt

localhost:~# ls
bench.py    hello.c     hello.js    readme.txt  testdir

localhost:~# ls -l
total 20
-rw-r--r--    1 root     root           114 Jul  6  2020 bench.py
-rw-r--r--    1 root     root            76 Jul  3  2020 hello.c
-rw-r--r--    1 root     root            22 Jun 26  2020 hello.js
-rw-r--r--    1 root     root           151 Nov 20 15:57 readme.txt
drwxr-xr-x    2 root     root            37 Nov 20 15:50 testdir

localhost:~# touch testtxt.txt
localhost:~# ls
bench.py     hello.c      hello.js     readme.txt   testdir      testtxt.txt

 

파일의 앞부분을 보고싶은 줄 수만큼 보여준다.

옵션을 지정하지 않으면 파일 상위 10줄을 보여준다.

localhost:~# ls
1            file1        hello.js     testdir
bench.py     hello.c      readme.txt   testtxt.txt

localhost:~# cat readme.txt
Some tests:
 
- Compile hello.c with gcc (or tcc):
 
  gcc hello.c -o hello
  ./hello
 
- Run QuickJS:
 
  qjs hello.js
 
- Run python:
 
  python3 bench.py
  
localhost:~# head -3 readme.txt
Some tests:
 
- Compile hello.c with gcc (or tcc):

 

tail

파일의 뒷부분을 보고싶은 줄 수만큼 보여준다.

옵션을 지정하지 않으면 파일 하위 10줄을 보여준다.

참고로 -F 옵션을 주고 실행하면,

파일 내용을 화면에 계속 띄워주고 파일이 변하게되면 새로운 업데이트된 내용을 갱신해준다.

주로 실시간으로 내용이 추가되는 로그파일을 모니터링할때 유용하게 사용한다.

localhost:~# ls
1            file1        hello.js     testdir
bench.py     hello.c      readme.txt   testtxt.txt

localhost:~# cat readme.txt
Some tests:
 
- Compile hello.c with gcc (or tcc):
 
  gcc hello.c -o hello
  ./hello
 
- Run QuickJS:
 
  qjs hello.js
 
- Run python:
 
  python3 bench.py
  
localhost:~# tail -3 readme.txt
- Run python:
 
  python3 bench.py

 

find

특정 파일이나 디렉토리를 검색한다

사용법이 앞의 명령어들에비해 살짝 복잡하므로, 기본 사용법을 언급하자면 다음과 같다.

find [검색경로] -name [파일명]

파일명은 직접 풀 네임을 입력해도 되지만, 특정 조건을 적용해 검색할수도 있다.

localhost:~# ls
1            file1        hello.js     testdir
bench.py     hello.c      readme.txt   testtxt.txt

localhost:~# find ./ -name 'hello.js'
./hello.js

localhost:~# find ./ -name '*.txt'
./readme.txt
./.mozilla/firefox/j4k7jsk5.default-default/SecurityPreloadState.txt
./.mozilla/firefox/j4k7jsk5.default-default/AlternateServices.txt
./.mozilla/firefox/j4k7jsk5.default-default/enumerate_devices.txt
./.mozilla/firefox/j4k7jsk5.default-default/pkcs11.txt
./.mozilla/firefox/j4k7jsk5.default-default/TRRBlacklist.txt
./.mozilla/firefox/j4k7jsk5.default-default/SiteSecurityServiceState.txt
./testtxt.txt

그리고 다음과 같이 -type 옵션을 주면, 디렉토리나 파일만 지정해서 검색할수도 있다.

localhost:~# find ./ -type d
./.wine/drive_c/Program Files/Common Files
./.wine/drive_c/Program Files/Common Files/System
./.wine/drive_c/Program Files/Common Files/System/OLE DB
./.wine/drive_c/Program Files/Common Files/Microsoft Shared
./.wine/drive_c/Program Files/Common Files/Microsoft Shared/TextConv
./.wine/drive_c/Program Files/Windows Media Player
./.wine/drive_c/Program Files/Internet Explorer
./.wine/drive_c/Program Files/Windows NT
./.wine/drive_c/Program Files/Windows NT/Accessories
./testdir

localhost:~# find ./ -type f
./hello.js
./hello.c
./readme.txt
./bench.py
./.ash_history
./testtxt.txt
./file1

'SPRING BOOT' 카테고리의 다른 글

[Github] Git 명령어 모음  (0) 2022.11.20
자주 사용되는 어노테이션  (0) 2022.11.12
MVC패턴 기초  (0) 2022.11.12
#0 SPRINGBOOT 환경 설정(for WINDOWS)  (0) 2022.10.10