[CS] 리눅스 명령어 정리

박상우·2023년 1월 11일
0

CS

목록 보기
5/12
post-thumbnail

코랩, 서버 등으로 작업할 때 유용한 리눅스 명령어 정리

pwd (print working directory)

pwd
/home/.. 
  • 현재 작업중인 디렉토리 정보 출력

cd (change directory)

cd /home
pwd
/home

cd ..
pwd
/
  • 경로 이동

ls (list)

ls
testfile1  testfile2  testfile3
  • 디렉토리 목록 확인

cp (copy)

cp testfile1 testfile_cp
ls
testdir/  testfile  testfile_cp


cp -r testdir testdir_cp
ls
testdir/  testdir_cp/  testfile  testfile_cp
  • 파일 혹은 디렉토리를 복사
    디렉토리를 복사할 시에는 -r 옵션을 추가

mv (move)

ls
testdir/  testfile


mv testfile testfile_mv
ls
testdir/  testfile_mv


mv testfile_mv testdir/
ls
testdir/


ls testdir/
testfile
  • 위치를 변경하거나, 이름을 변경

mkdir

mkdir testdir
ls
testdir/  testfile
  • 디렉토리 생성

rm (remove)

ls
testdir/  testfile1  testfile2


rm -f testfile1
ls
testdir/  testfile2


rm -rf testdir/
ls
testfile2
  • 파일 혹은 디렉토리를 삭제
    디렉토리를 삭제할 시에는 r 옵션을 추가
    f 옵션을 주면 사용자에게 삭제 여부를 묻지 않음

touch

touch testfile2
  • 파일 혹은 디렉토리의 업데이트 일자를 현재 시간으로 변경

cat

ls
file1  file2  file3


cat file1
1


cat file2
2


cat file3
3


cat file1 file2 > file1_2
$ ls
file1  file1_2  file2  file3


cat file1_2
1
2


cat file1 >> file2
cat file2
2
1


cat > file4
hello
world
(작성이 끝나면 ctrl +d 로 파일 저장)


cat file4
hello
world
  • 파일 작성, 확인, 병합 명령어

find

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

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

  • 특정 파일이나 디렉토리를 검색
  • *을 통해 특정 확장자를 찾을 수 있음
find ./ -name "*.jpg" -exec rm {} \;
ls
dir1/  dir2/  dir3/  dir4/  file1  file2  file3  file4
  • -exec를 통해 특정 확장자만 삭제 가능
find ./ -type d
./
./dir1
./dir2
./dir3
./dir4


find ./ -type f
./file1
./file2
./file3
./file4
  • d, f 옵션으로 파일 혹은 디렉토리만 검색 가능
find ./ -type f | wc -l
4
  • wc -l 옵션을 통해 결과 값이 몇개 존재하는지 숫자로 확인 가능
find ./ -name "*.txt" -exec sed -i 's/hi/hello/g' {} \;
  • txt 파일 안에 있는 'hi'라는 문자열을 'hello'로 바꾸는 명령어
sed -i 's/hi/hello/g' testfile1.txt
  • textfile1.txt의 모든 'hi'라는 문자열을 'hello'로 바꾸는 명령어인 sed를 find와 응용한 것

unzip

unzip compressed.zip 
현재 폴더에 압축 해제 

unzip -l compressed.zip 
압축 해제하지 않고 압축 파일 내 목록 출력 

unzip compressed.zip  -d /path/to/put
특정 폴더에 압축 해제 

for i in *.zip; do unzip $i -d /path/to/put;done
여러 파일 압축 해제 

zip

  • -r : 디렉터리까지 압축
  • -1: 빠른 압축(압축률 ⬇)
  • -9: 높은 압축률 (속도 ⬇)
  • -e: zip 파일에 암호 설정
  • -x: 압축시 파일 제외

unzip

  • -d: 지정한 디렉터리에 압축 해제
  • -l: 압축 파일내 목록 보기

tar

파일과 폴더를 하나의 파일로 묶어주는 archiving utility (압축 x)

  • 아카이빙
tar -cvf target.tar file1 file2 dir1 dir2

단순 아카이빙 
tar -cvzf target.tar.gz file1 file2 dir1 dir2

아카이빙 후 gzip으로 압축 
tar -cvJf target.tar.xz file1 file2 dir1 dir2

아카이빙 후 xz로 압축 
  • 해제
tar -xvf target.tar
현재 폴더에 아카이빙 풀기 
tar -zxvf target.tar.gz
압출된 tar 파일 해제  
압축 방식에 따라 -j(bzip2), -J(xz), -z(gzip) 옵션 추가 
tar -Jxvf target.tar.xz -C /opt
해제 디렉토리를 지정하는 xz 
profile
세상아 덤벼라

0개의 댓글