[Linux Command] 기타 실무 주요 사용 커맨드 정리

YunKwang-You·2022년 1월 13일
1

Linux

목록 보기
1/5

pwd (print working directory)

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

$ pwd
/home/downloads

cd (change directory)

cd : 경로이동
절대경로와 상대경로 둘 다 이동 가능

$ cd /home/downloads/mydir
$ pwd
/home/downloads/mydir
$ cd ..
$ pwd 
/home/downloads

cp (copy)

cp : 파일 혹은 디렉토리 복사
디렉토리 복사 시 -r 옵션 사용해야함

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

mv (move)

mv : 파일 혹은 디렉토리 이동
원하는 위치로 이동할 때 뿐만 아니라, 이름을 변경하는 용도로도 사용된다. cp와는 달리 디렉토리를 이동시킬 때도 별다른 옵션이 필요 없다.

$ ls
testdir/	testfile1
$ mv testfile testfile_mv
$ ls
testdir/	testfile_mv
$ mv testfile_mv testdir/
$ ls
testdir/
$ ls testdir/
testfile_mv

mkdir (make directory)

mkdir : 디렉토리 생성
-p 옵션을 통해 하위 디렉토리까지 한 번에 생성 가능

$ ls
testfile
$ mkdir testdir
$ ls
testdir/  testfile
$ mkdir -p a/b/c/d/e/
$ ls -R a/
a/:
b/
a/b:
c/
a/b/c:
d/
a/b/c/d:
e/
a/b/c/d/e:

rm (remove)

rm : 파일이나 디렉토리 삭제
디렉토리를 삭제할 때는 -r 옵션 사용
-f 옵션을 사용하면 삭제 여부를 묻지 않고 바로 삭제
디렉토리를 삭제하면 하위 디렉토리까지 모두 삭제된다.

$ ls
testdir/	testfile1	testfile2
$ rm -f testfile1
$ ls
testdir/	testfile2
$ rm -rf testdir/
$ ls
testfile2

touch

touch : 파일이나 디렉토리의 최근 업데이트 일자를 현재 시간으로 변경
최근 업데이트 일자는 ls -l 명령을 통해 확인 가능하다.
파일이나 디렉토리가 존재하지 않으면 빈 파일을 만든다.

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

cat (concatenate)

cat :

  • 파일 내용 출력기능
  • 파일 여러개를 합쳐 하나의 파일로 만드는 기능
  • 기존의 한 파일의 내용을 다른 파일에 덧붙이는 기능
  • 새로운 파일을 만드는기능
$ ls
file1	file2	file3

(1) 파일 내용 출력 기능

$ cat file1
1
$ cat file2
2
$ cat file3
3

(2) 파일 여러개를 합쳐 하나의 파일로 만드는 기능

$ cat file1 file2 > file1_2
$ ls
file1	file1_2	file2	file 3
$ cat file1_2
1
2

(3) 기존의 한 파일의 내용을 다른 파일에 덧붙이는 기능

$ cat file1 >> file2
$ cat file2
2
1

(4) 새로운 파일을 만드는 기능 ( 작성이 끝나면 ctrl+d 로 파일 저장 )

$ cat > file4
hello
world
$ cat file4
hello
world

파일의 앞부분을 보고싶은 줄 수만큼 보여준다.
옵션을 지정하지 않으면 상위 10줄을 보여준다.

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

tail

파일의 뒷 부분을 보고싶은 줄 수만큼 보여준다.
옵션을 지정하지 않으면 하위 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 testfile
6
7
8
9
10
11
12
13
14
15
$ tail -F testfile
6
7
8
9
10
11
12
13
14
15

(명령어가 종료되지 않고 계속 해당 화면을 출력하며, 파일 내용 변경시 자동으로 갱신해준다)


find

특정 파일이나 디렉토리를 검색한다.
사용 방법은 이렇다.

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

'*'를 통해 특정 확장자명을 찾을 수 있다.

$ 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 옵션을 이용하면 된다.

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

또, -type d 와 -type f 옵션을 통해 디렉토리나 파일만 지정해서 검색할 수 있다.

$ find ./ -type d
./
./dir1
./dir2
./dir3
./dir4
$ find ./ -type f
./file1
./file2
./file3
./file4

다음과 같이 wc -l 옵션과 같이 사용하면,
특정 디렉토리에 find 조건에 맞는 결과 값이 몇개 존재하는지 숫자로 간편히 알아볼 수 있다.

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

이는 파일 개수가 욜라리 많을 때 아주 유용하다.

마지막으로 특정 조건에 해당하는 파일들의 내용을 전부 찾아 바꿔주는 커맨드를 소개한다.

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

위에 포함되어있는 sed 명령어는 testfilel1.txt 이라는 파일의 모든 'hi' 문자열을 'hello'로 바꾸는 역할을 한다.

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

find문에 결합된 sed는 조건에맞는 파일들을 찾고, 찾은 파일들에대해 해당 명령어(sed)를 수행할 수 있도록 한 것이다.


위의 커맨드들!! 정말 실무에서 쓸 일이 많을까 궁금하다. 계속해서 공부해보자😄

profile
Network Infra Engineer

0개의 댓글