본문 바로가기
PC_Tips/Linux 사용 Tip

자주 사용하는 명령어

by ciwhiz 2022. 3. 16.

* (남은 용량 확인)
root@:/# df -h -T
(예)
Filesystem    Type    Size  Used Avail Use% Mounted on
/dev/sda1     ext3    7.6G  435M  6.8G   6% /
varrun       tmpfs     62M   36K   62M   1% /var/run
varlock      tmpfs     62M  4.0K   62M   1% /var/lock
udev         tmpfs     62M   80K   62M   1% /dev
devshm       tmpfs     62M     0   62M   0% /dev/shm

* 내 현재 용량 체크
quota -s

===계정별 용량체크===========================================
 du -sh /home/* | sort -nr
======================================================

===폴더별 용량체크=======================================
du -h --max-depth=1
======================================================


* (파일탐색)
명령어 : find 파일 찾을 위치 지정 -name 찾을 파일 이름
    ex  : find ./ -name aaa.txt

위에 ex를 설명해드리면 ./(현재 폴더 부터, 하위 폴더 포함) aaa.txt 파일을 찾아줘

* (파일 내부 문자열을 검색)
명령어 : grep -r "찾을 문자열" ./*
     ex : grep -r "aaa" ./*
        
위의 ex를 설명드리면 ./(현재 폴더 아래 모든 파일에서) aaa라는 문자열이 있는지 찾아라!! 단 하위 디렉토리 모두에서(-r)

* uname
시스템과 커널의 정보를 확인할 수 있습니다.
저는 모든 정보를 확인하기 위하여 -a 옵션을 사용하였습니다.

보여지는 순서는
Linux . localhost . 3.13.0-24-generic . #47-Ubuntu SMP Fri May 2 23:30:00 UTC 2014 . x8664 . x8664 . x86_64 . GNU/Linux
[커널명] [호스트명] [릴리즈정보] [커널버전] [머신하드웨어이름] [프로세서종료] [하드웨어플랫폼] [운영체제]
입니다.

* netstat 
현재 접속하고 있는 사용자 수 확인
netstat -an  | grep :80 | grep ESTB | wc -l

*  불필요한 상대방 세션을 종료 시키는 방법에 대해 알아보겠습니다.

1. 사용방법

1) 현재 접속자 확인
w : 접속한 사용자의 정보 및 어떤 작업을 하는지 확인
who [옵션] : 접속한 사용자 정보 확인 (옵션 : -a / -q / -m)
finger : 접속한 사용자의 정보 확인

2) 사용자 세션 종료
ps -ef | grep ssh로 확인
# kill -9 해당하는 TTY의 pid  ex) kill -9 2076


폴더권한 관련

#ls -al 로 봤는데, 어떤 폴더가 
drwxrwxrwx+ ...... 로, 맨 뒤에 '+'가 붙어있으면 뭔가 다른 access control list에 추가가 되어있어서,
당연히 777로 r/w가 될것같지만 안된다
이럴 때는, 아래 명령어로 acl 에서 삭제한 후.
#setfacl -b 폴더명 
#chmod 777 폴더명
으로 다시 rw 권한을 주면 r/w가 잘 될거임.

* 비밀번호 변경
passwd (계정명)

<<유용한 Bash 명령어>>
- 바로 전 명령어 실행하기:
!!

- 특정 문자로 시작하는 이전 명령어 실행하기. 예:
!s

- 편집했던 파일을 복사하거나 백업하기. 예로, nginx.conf 복사:
cp nginx.conf{,.bak}

- 현재 디렉토르와 최근 디렉토리 사이를 왔다갔다 하기
cd -

- 부모 디렉토리로 가기. 공백 주위!
cd ..

- 홈 디렉토리로 가기
cd ~

- 홈 디렉토리로 가기
cd $HOME

- 홈 디렉토리로 가기
cd

- 755 권한 설정. (owner-group-other) 순으로 (-rwx-r-x-r-x) 권한을 준다.
chmod 755 파일명

- 모든 유저에게 실행권한 추가하기
chmod a+x 파일명

- 파일이나 . 디렉토리의 소유권 변경
chown 유저명

- file.backup으로 백업 복사본 만들기
cp file file.backup

- file1을 복사하여 file2 만들기
cp file1 file2

- directory1의 모든 컨텐트를 directory2로 복사하기 (recursively)
cp -r directory1 directory2/

- 날짜 보기
date

- Zero the sdb drive. You may want to use GParted to format the drive afterward. You need elevated permissions to run this (sudo).
dd if=/dev/zero of=/dev/sdb

- 디스크 사용량 보기
df -h

- OS의 상세한 메시지를 텍스트 파일에 넣기
dmesg>dmesg.txt

- Display a LOT of system information. I usually pipe output to less. You need elevated permissions to run this (sudo).
dmidecode

- Display BIOS information. You need elevated permissions to run this (sudo).
dmidecode -t 0

- Display CPU information. You need elevated permissions to run this (sudo).
dmidecode -t 4

- Search for installed packages related to Apache
dpkg –get-selections | grep apache

- Shows you where in the filesystem the package components were installed
dpkg -L package_name

- 각 서브디렉토리의 상세한 디스크 사용을 보기
du / -bh | less

- 환경변수 PATH 값을 보기
echo $PATH

- USER, LANG, SHELL, PATH, TERM과 같은 환경변수 보기
env

- Opens a picture with the Eye of Gnome Image Viewer
eog picture_name

- 터미널 끝내기 (혹은 sudo su로 하고 있었다면 수퍼유저 포기하기)
exit

- Display memory usage
free

- Easy way to view all the system logs.
gnome-system-log

- 파일을 찾아 string이 매칭된 라인 보기
grep string filename

- Get the number of seconds since the OS was started
grep btime /proc/stat | grep -Eo “[[:digit:]]+”

- 이전 명령어 1000개 보기
history | less

- 로컬 호스트 이름 보기
hostname

- Display time.
hwclock –show

- 사용자 id (uid) 와 그룹 id (gid) 보기
id

- 로컬 IP 주소와 netmask 보기
ifconfig

- Wireless network interface
iwconfig

- Display wireless network information
iwlist

- Kill process by name. You need elevated permissions to run this (sudo).
killall process

- Get the date and time of the last system shutdown
last -x | grep shutdown | head -1 | grep -Eo “[A-Z][a-z]{2} [[:digit:] ][[:digit:]] [[:digit:]]{2}:[[:digit:]]{2}”

- 쉘 세션 끝내기 (버추얼 콘솔의 하나로 로그인한 쉘만)
logout

- 현재 디렉토리의 숨겨지지 않은 파일과 서브폴더 목록 보기. -R은 recursive이고 -a는 숨긴 파일을 포함한다.
ls

- 현재 디렉토리의 모든 파일의 파일 접근 권한 보기. 권한의 포맷은 drwxrwxrwx이고, 순서는 owner-group-other, 숫자값은 read=4, write=2, execute=1이다.
ls -l filename

- 가능한 모든 어플리케이션의 목록 보기
ls /usr/bin | less

- Display more networking information
lshw -C network

- Display kernel modules currently loaded
lsmod

- Display sound, video, and networking hardware
lspci -nv | less

-Display usb-connected hardware
lsusb

- 명령어 man 페이지 읽기 (manual)
man command

- 새 디렉토리 만들기
mkdir dirname

- 특정 디렉토리에 파일 옮기기
mv file dir

- file1을 file2로 이름바꾸기
mv file1 file2

- 라우팅 테이블 보기
netstat -rn

- 환경 변수 보기
printenv

- 사용자가 현재 실행하고 프로세스 목록 보기. 유용한 옵션이 많으니 ps –help 로 보라.
ps -Af

- 작업 디렉토리 보기
pwd

- 파일 지우기
rm filename

- 디렉토리와 디렉토리의 모든 컨텐트 지우기
rm -rf dir

- 현재 디렉토리의 txt로 끝나는 모든 파일 지우기
rm *.txt

- 디렉토리 지우기 (비어있지 않을 때에만 동작한다)
rmdir dir

- Display your default gateway listed under “default”
route

- Completely destroy all traces of the file. This takes a while. -n 7 means seven overwrites, -z means zero the bits afterward to hide shredding, -u means delete the file when done, and -v means verbose.
=> shred -zuv -n 7 file

- 지금 컴퓨터 끄기
shutdown -h now

- 지금 컴퓨터 재시작
shutdown -r now

- 원격 컴퓨터에 로그인하기
ssh IP address

- 루트 쉘을 열고 exit할 때까지 수퍼유저 권한을 갖는다. sudo su와 달리 사용자 환경변수에 상관없이 루트 쉘을 시작한다.
sudo -i

- sudo -i와 같이 루트 쉘을 연다. 그러나 이 방법은 사용자 환경변수 유지한다. exit으로 일반 쉘로 돌아간다.
sudo su

- 특정 디렉토리와 그 안의 모든 파일의 압축 파일을 만들기
tar czf dirname.tgz dirname

- 현재 디렉토리에 압축 파일을 풀기
tar zxvf archive

- cpu 사용량 기준으로 현재 프로세스 목록 보기. 끝내려면 q를, 도움말은 h를 누른다.
top

- 빈 파일 만들기. 단, 파일이 없어야 한다.
touch filename

- 현재 터미널 이름 보기
tty

- 리눅스 커널 보기
uname -a

- 컴퓨터의 프로세스 아키텍처 보기
uname -m

- 명령어의 man 페이지의 한줄 요약 보기
whatis command

- 프로그램의 파일 시스템 위치 보기
whereis command

- 어플리케이션의 경로 보기
which command

- 컴퓨터에 로그인한 사용자 보기
who

- 내 로그인 이름 보기
whoami

- This will display the output of test.log as it is being written to by another program
tail –follow test.log

- 디렉토리 쉘을 오가면서 디렉토리의 파일이나 어플리케이션을 열고 싶다면 파일명 앞에 이 명령어를 붙여라. 예.
./filename.txt

- 이스케이프 연산자. 이름에 공백이 있는 파일을 열 때 공백 바로 전에 사용하라.
\

- 물결표는 홈 디렉토리를 말한다.
~

- Run any command when the system load is low
batch command

- Display cpu info
cat /proc/cpuinfo

- Display memory usage
cat /proc/meminfo

- Display networking devices
cat /proc/net/dev

- Display performance information
cat /proc/uptime

- Display kernel version
cat /proc/version

- 파일 내용 보기
cat filename

- 파티션 테이블 목록 보기
fdisk -l

- Show the properties/compression of a file or package
file package_name

- 파일 찾기
find / -name filename

- *.gz로 압축 파일 만들기
gzip test.txt

- *.gz 파일 압축 풀기
gzip -d test.txt.gz

- 압축 파일의 압축비 보기
gzip -l *.gz

- 파일 상태 보기
stat filename.txt

- 인터넷에서 파일 받기
wget http://remote_file_url

- Show list of last 10 logged in users.
`last -n 10`

- Display a tree of processes
`pstree`

추가 명령어
현재 디렉토리 파일 개
`ls -1 | wc -l`


ps -efT | grep sometext

 

 

[압축관련]

압축하기

tar

$ tar -cvf [파일명.tar] [폴더명]
# abc라는 폴더를 aaa.tar로 압축 예시
$ tar -cvf aaa.tar abc

gz

$ tar -zcvf [파일명.tar.gz] [폴더명]
# abc라는 폴더를 aaa.tar.gz로 압축 예시
$ tar -zcvf aaa.tar.gz abc

zip

$ zip [파일명.zip] [폴더명]
# 현재폴더 전체를 aaa.zip으로 압축 예시
$ zip aaa.zip ./*
# aaa.zip으로 압축하고 현재 폴더의 모든 것과 현재 폴더의 하위 폴더들도 모두 압축 예시
$ zip aaa.zip -r ./*
# 위 명령어를 스크립트에서 실행할 때, 파일 경로가 전부 나올 수 있기 때문에 해당 폴더로 이동한 후 작업하는 것을 권장

 

압축풀기

tar

$ tar -xvf [파일명.tar]
# aaa.tar라는 tar파일 압축해제 예시
$ tar -xvf aaa.tar

gz

$ tar -zxvf [파일명.tar.gz]
#  aaa.tar.gz라는 tar.gz파일 압축 해제
$ tar -zxvf aaa.tar.gz

zip

$ unzip [파일명.zip]
# aaa.zip 압축 해제 예시
$ unzip aaa.zip 
# 특정 폴더에 압축해제 예시
$ unzip aaa.zip -d ./target

tar 의 옵션 설명:

  •  -c : 파일 tar로 압축
  •  -p : 파일 권한 저장
  •  -v : 화면에 과정 출력
  •  -f : 파일 이름 지정
  •  -C : 경로 지정
  •  -x : tar 압축해제
  •  -z : gzip으로 압축/해제