본문 바로가기
개발 지식/인프라

[Linux/리눅스] 파일 관련 기본 명령어 정리 및 예시 (cd, pwd, ls, mkdir, touch, mv, cp, rm, cat)

by 머그워트 2023. 6. 19.
728x90

 

출처: https://en.wikipedia.org/wiki/Linux

 

 

cd

  • change directory
  • 지정한 경로로 작업 경로를 이동한다.

 

예)

cd /usr/home/tomcat

 

결과: /usr/tomcat/bin 경로로 이동한다.

 

 

pwd

  • print working directory
  • 현재 경로를 출력한다.

 

예) 현재 경로가 /usr/tomcat/bin인 경우

[mugwart@localhost /usr/tomcat/bin] pwd
/usr/tomcat/bin

 

결과: /usr/tomcat/bin을 출력한다.

 

 

ls

  • list
  • 현재 경로의 파일명 목록을 출력한다.
  • ls 명령어만 단독으로 사용하면 파일명만 공백 구분으로 쭈우욱 출력되기 때문에 옵션과 함께 쓴다.
  • 옵션에 대해서는 추후 정리 예정.

 

예)

[mugwart@localhost /usr/tomcat/bin] ls
tomcat.exe file1.txt file2.txt

 

결과: 현재 경로 내의 파일 목록을 출력한다.

 

 

mkdir

  • make directory
  • 지정한 이름의 디렉토리를 현재 경로에 생성한다.

 

예) testdir 디렉토리를 생성하고, 해당 디렉토리로 이동 후 pwd로 현재 경로를 출력한다.

[mugwart@localhost /usr/tomcat/bin] mkdir testdir
[mugwart@localhost /usr/tomcat/bin] cd testdir
[mugwart@localhost /usr/tomcat/bin/testdir] pwd
/usr/tomcat/bin/testdir

 

 

touch

  • 지정한 이름의 빈 파일 생성, 또는 파일의 타임스탬프 관련 정보 수정

 

예 1) 현재 경로의 파일 목록 출력 후 file3.txt 파일 생성하고 다시 파일 목록 출력

[mugwart@localhost /usr/tomcat/bin] ls
testdir tomcat.exe file1.txt file2.txt
[mugwart@localhost /usr/tomcat/bin] touch file3.txt
[mugwart@localhost /usr/tomcat/bin] ls
testdir tomcat.ext file1.txt file2.txt file3.txt

 

예 2) file3.txt의 날짜를 2023년 6월 19일 10시 00분 00초로 변경

[mugwart@localhost /usr/tomcat/bin] touch -t 20230619100000 file3.txt

 

mv

  • move
  • 파일이 저장된 경로 이동 또는 파일명 변경

 

예 1)  file2.txt를 /usr/tomcat/bin에서 /usr/tomcat/bin/testdir로 이동한 후 testdir의 파일 목록 출력

[mugwart@localhost /usr/tomcat/bin] ls
testdir tomcat.exe file1.txt file2.txt file3.txt
[mugwart@localhost /usr/tomcat/bin] mv file2.txt /testdir
[mugwartj@localhost /usr/tomcat/bin] cd testdir
[mugwart@localhost /usr/tomcat/bin/testdir] ls
file2.txt

 

예 2) file2.txt의 파일명을 testfile.txt로 변경

[mugwart@localhost /usr/tomcat/bin/testdir] mv file2.txt testfile.txt
[mugwart@localhost /usr/tomcat/bin/testdir] ls
testfile.txt

 

 

cp

  • copy
  • 파일 또는 디렉토리를 복사
  • 디렉토리 복사 시 -r 옵션 사용 (recursive)

 

예 1) testfile.txt를 testfile2.txt라는 이름으로 복사

[mugwart@localhost /usr/tomcat/bin/testdir] cp testfile.txt testfile2.txt
[mugwart@localhost /usr/tomcat/bin/testdir] ls
testfile.txt testfile2.txt

 

예2) bin 폴더에 testdir2 경로 생성 후 testdir 디렉토리 하위의 모든 파일을 testdir2로 복사

[mugwart@localhost /usr/tomcat/bin] mkdir testdir2
[mugwart@localhost /usr/tomcat/bin] cp -r testdir testdir2
[mugwart@localhost /usr/tomcat/bin] cd testdir2
[mugwart@localhost /usr/tomcat/bin/testdir2] ls
testfile.txt testfile2.txt

 

 

rm

  • remove
  • 파일 또는 디렉토리를 삭제
  • 디렉토리 삭제 시 -r 사용 (recursive)

 

예 1) testfile2.txt 파일 삭제

[mugwart@localhost /usr/tomcat/bin/testdir2] ls
testfile.txt testfile2.txt
[mugwart@localhost /usr/tomcat/bin/testdir2] rm testfile2.txt
[mugwart@localhost /usr/tomcat/bin/testdir2] ls
testfile.txt

 

예 2) testdir2 경로 삭제

[mugwart@localhost /usr/tomcat/bin] ls
testdir testdir2 tomcat.exe file1.txt file2.txt file3.txt
[mugwart@localhost /usr/tomcat/bin] rm -r  testdir2
[mugwart@localhost /usr/tomcat/bin] ls
testdir tomcat.exe file1.txt file2.txt file3.txt

 

 

cat

  • catenate
  • 파일의 내용을 화면에 출력
  • 여러 파일을 공백으로 구분하여 입력 시 여러 파일 내용 동시 출력 가능

 

예) file1.txt의 내용을 화면에 출력

[mugwart@localhost /usr/tomcat/bin] cat file1.txt
Hello world!
My name is
Mugwart

 

728x90

댓글