본문 바로가기

Language&Configuration/[Liunx]

[Linux] 리눅스 기본 명령어 모음 및 사용법 (12개)

유닉스 계열인 리눅스에서는 대소문자를 구분합니다.
명령어는 모두 소문자이지만, 일부 옵션에서는 대문자를 사용하기도 합니다.

또한, 모든 명령어는 명령어 뒤에 --help 옵션을 주거나 man 명령어를 사용하면 
옵션이나 사용법이 나오기 때문에 잘 모르는 명령어는 꼭 보시면 익히면 좋습니다.
예시) pwd --help / man pwd 


pwd  - print working directory
현재 작업중인 디렉토리 정보 출력

$ pwd
/home/infoofit

 

cd - change directory 
경로 이동 - 절대 경로와 상대 경로로 이동 가능

$ cd /home/infoofit/mydir
$ pwd
/home/infoofit/mydir

$ cd ..
$ pwd
/home/infoofit

 

ls - list
디렉토리 목록 확인

$ ls
test1  test2  test3

$ ls -l
total 0
-rw-r--r-- 1 infoofit 197121 0 5월  2 12:11 test1
-rw-r--r-- 1 infoofit 197121 0 5월  2 12:11 test2
-rw-r--r-- 1 infoofit 197121 0 5월  2 12:11 test3

$ ls -a
./  ../  test1  test2  test3

$ ls -al
total 4
drwxr-xr-x 1 infoofit 197121 0 5월  2 12:11 ./
drwxr-xr-x 1 infoofit 197121 0 5월  2 12:11 ../
-rw-r--r-- 1 infoofit 197121 0 5월  2 12:11 test1
-rw-r--r-- 1 infoofit 197121 0 5월  2 12:11 test2
-rw-r--r-- 1 infoofit 197121 0 5월  2 12:11 test3

 

cp - copy 
파일 복사/디렉토리 복사( -r 옵션)

$ ls
testdir/  testfile

$ cp testfile1 testfile_cp
$ ls
testdir/  testfile  testfile_cp

$ cp -r testdir testdir_cp
$ ls
testdir/  testdir_cp/  testfile  testfile_cp

 

mv - move
파일/디렉토리 이동, 이름 변경
* 디렉토리 이동 시, 옵션 불필요(cp 명령어와 차이점)

$ ls
testdir/  testfile

$ mv testfile testfile_mv
$ ls
testdir/  testfile_mv

$ mv testfile_mv testdir/
$ ls
testdir/

$ ls testdir/
testfile

 

mkdir - make directory 
디렉토리 생성 
* -p 옵션 : 하위 디렉토리까지 한 번에 생성 가능

$ ls
testfile

$ mkdir testdir
$ ls
testdir/  testfile

$ mkdir -p a/b/c
$ ls -R a/
a/:
b/

a/b:
c/

 

rm - remove
파일/디렉토리 삭제(-r 옵션)
* -f 옵션 : 삭제 여부를 묻지않고 바로 삭제한다.
* 디렉토리 삭제시, 하위 디렉토리까지 모두 삭제된다

$ ls
testdir/  testfile1  testfile2

$ rm -f testfile1
$ ls
testdir/  testfile2

$ rm -rf testdir/
$ ls
testfile2

 

touch
파일/디렉토리 최근 업데이트 일자를 현재 시간으로 변경
* 파일이나 디렉토리가 존재하지 않으면 빈 파일을 만든다.

$ ls -l
total 0
-rw-r--r-- 1 infoofit 197121 0 11월  6 22:08 testfile1

$ touch testfile1
$ ls -l
total 0
-rw-r--r-- 1 infoofit 197121 0 11월  6 22:43 testfile1

$ touch testfile2
$ ls -l
total 0
-rw-r--r-- 1 infoofit 197121 0 11월  6 22:43 testfile1
-rw-r--r-- 1 infoofit 197121 0 11월  6 22:43 testfile2

 

cat - concatenate
파일 내용 출력, 파일 합병,다른 파일에 내용 붙이기, 새로운 파일 생성

$ ls
file1  file2

$ cat file1
1

$ cat file2
2

$ cat file1 file2 > file1_2
$ ls
file1  file1_2  file2

$ cat file1_2
1
2

$ cat file1 >> file2
$ cat file2
2
1

 

head
파일 앞부분 출력
* Default : 상위 10줄

$ cat testfile
1
2
3
4
5
6
7
8
9
10
11

$ head -3 testfile
1
2
3


$ head testfile
1
2
3
4
5
6
7
8
9
10

 

tail
파일 뒷부분 출력
* Default : 상위 10줄
* -F 옵션 : 뒷부분 파일을 화면 출력하며, 새롭게 업데이트된 내용을 실시간 갱신 
                   -> 실시간 로그파일을 모니터링할때 유용하게 사용

$ cat testfile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

$ tail -3 testfile
13
14
15

$ tail -F testfile
6
7
8
9
10
11
12
13
14
15
(명령어가 종료되지 않고 계속 해당 화면을 출력하며, 파일 내용 변경시 자동으로 갱신해준다)

 

 

find
특정 파일/디렉토리 검색

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


파일명 입력 시, 특정 조건으로 검색 가능(ex.확장자)

$ ls
dir1/  dir3/  file1  file3  picture1.jpg  picture3.jpg
dir2/  dir4/  file2  file4  picture2.jpg  picture4.jpg


$ find ./ -name 'file1'
./file1


$ find ./ -name "*.jpg"
./picture1.jpg
./picture2.jpg
./picture3.jpg
./picture4.jpg

 

- exec 옵션 :  rm {} \; 을 같이 사용하여 바로 삭제 가능

$ find ./ -name "*.jpg" -exec rm {} \;
$ ls
dir1/  dir2/  dir3/  dir4/  file1  file2  file3  file4

 

-type 옵션 :  디렉토리/파일 지정 검색

 

$ find ./ -type d
./
./dir1
./dir2
./dir3
./dir4


$ find ./ -type f
./file1
./file2
./file3
./file4

wc -l 옵션과 같이 사용 시,
특정 디렉토리에 find 조건에 맞는 결과 갯수 확인

$ find ./ -type f | wc -l
4

특정 조건에 해당하는 파일 내용을 찾아 전체 수정
ex) 10만개의 파일 중, .txt 확장자인 파일을 찾아, txt 파일 안에 있는 ‘hi’ 라는 문자열을 ‘hello’로 바꾸기

find ./ -name "*.txt" -exec sed -i 's/hi/hello/g' {} \;

* sed 명령어 : 파일 testfile1.txt 의 모든 ‘hi’ 라는 문자열을 ‘hello’로 변경

sed -i 's/hi/hello/g' testfile1.txt