전자책 출간 알림 [마이크로서비스패턴 쉽게 개발하기]

티스토리 뷰

File system 정보 보기

파일시스템 유형에 따라 파일 디바이스명은 아래와 같습니다.

/dev/sdX : SATA(Seial Advanced Technology Adaptor: 직렬버스방식의 HDD). 예) sda, sdb, sdc

/dev/vdX : Virtual Disk. 예) vda, vdb, vdc

/dev/nvmeX: Non-Volatile Memory Express(비휘발성 기억장치) 예) nvme0, nvme1, nvme2

df -h명령으로 보면 File system 정보를 볼 수 있습니다.

[root@infra ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
devtmpfs                 7.8G     0  7.8G   0% /dev
tmpfs                    7.8G     0  7.8G   0% /dev/shm
tmpfs                    7.8G   18M  7.8G   1% /run
tmpfs                    7.8G     0  7.8G   0% /sys/fs/cgroup
/dev/mapper/centos-root  481G  4.7G  477G   1% /
/dev/sda1               1014M  183M  832M  19% /boot
/dev/mapper/centos-home   10G   33M   10G   1% /home
tmpfs                    1.6G     0  1.6G   0% /run/user/0
overlay                  481G  4.7G  477G   1% /var/lib/docker/overlay2/6e59d824245cd3cd4de0d7bf7c41721859eb315615aef04d47a3fa55fa047479/merged
overlay                  481G  4.7G  477G   1% /var/lib/docker/overlay2/761801df046f90c9f731651ce35a369d714a9ae91cf70b43c9d69b6ed6a536d1/merged
overlay                  481G  4.7G  477G   1% /var/lib/docker/overlay2/9f49224b2a580a890d818b0f19b5e7ae380858d312afe53ca5d47bb06bf66d58/merged

위에서 /dev/sda1의 의미는 첫번째 SATA 디바이스(sda)의 1번째 파티션이라는 의미입니다.

 

blkid <File system>

file system의 UUID와 TYPE을 제공합니다.

[root@infra ~]# blkid /dev/sda1
/dev/sda1: UUID="45b12cb1-9108-42b8-ade1-0305a10d9761" TYPE="xfs"

blkid로 안 나오는 경우 lsblk --fs를 이용하십시오.

 

findmnt

findmnt는 mount된 트리 구조를 제공합니다,

[root@infra ~]# findmnt
TARGET                                SOURCE     FSTYPE     OPTIONS
/                                     /dev/mapper/centos-root
                                                 xfs        rw,relatime,seclabel,attr2,inode64,noquota
|-/sys                                sysfs      sysfs      rw,nosuid,nodev,noexec,relatime,seclabel
| |-/sys/kernel/security              securityfs securityfs rw,nosuid,nodev,noexec,relatime
| |-/sys/fs/cgroup                    tmpfs      tmpfs      ro,nosuid,nodev,noexec,seclabel,mode=755
| | |-/sys/fs/cgroup/systemd          cgroup     cgroup     rw,nosuid,nodev,noexec,relatime,seclabel,xattr,release_agent=/usr/lib/systemd/syste
| | |-/sys/fs/cgroup/net_cls,net_prio cgroup     cgroup     rw,nosuid,nodev,noexec,relatime,seclabel,net_prio,net_cls
| | |-/sys/fs/cgroup/cpu,cpuacct      cgroup     cgroup     rw,nosuid,nodev,noexec,relatime,seclabel,cpuacct,cpu
...

 

lsblk

block device의 목록을 제공합니다. list block의 약자인듯합니다.

[root@infra ~]# lsblk
NAME            MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda               8:0    0   500G  0 disk
|-sda1            8:1    0     1G  0 part /boot
`-sda2            8:2    0   499G  0 part
  |-centos-root 253:0    0 481.1G  0 lvm  /
  |-centos-swap 253:1    0   7.9G  0 lvm  [SWAP]
  `-centos-home 253:2    0    10G  0 lvm  /home
sr0              11:0    1  1024M  0 rom

lsblk --fs를 하면 device의 UUID를 구할 수 있습니다.

 

실제로 block device가 위치한 file system을 보면 맨 앞에가 'b'로 표시됩니다. 즉, block file system이라는 의미입니다.

[root@infra ~]# ls -l /dev/sda
brw-rw----. 1 root disk 8, 0 Jun 17 13:11 /dev/sda
[root@infra ~]# ls -l /dev/sda1
brw-rw----. 1 root disk 8, 1 Jun 17 13:11 /dev/sda1
[root@infra ~]# ls -l /dev/sda2
brw-rw----. 1 root disk 8, 2 Jun 17 13:11 /dev/sda2

 

du

du는 Disk Usage를 제공하는 명령으로, -s(summay)와 -h(human readable)을 함께 사용하면 쉽게 특정 디렉토리의 사용량을 알 수 있습니다.

[root@infra ~]# du -sh /var/log
4.2M	/var/log
[root@infra ~]# du -sh /etc
36M	/etc

du와 df는 측정방식이 달라 결과값에 차이가 있는데 정확한 것은 du입니다. 

[root@infra ~]# du -sh /boot
151M	/boot
[root@infra ~]# df -h /boot
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1      1014M  183M  832M  19% /boot

Mount/Unmount file system

block file system을 mount하고 un-mount하는 방법에 대해 설명합니다.

blkid명령으로 file system의 UUID를 먼저 구합니다.

mount를 할 때 UUID를 이용하는것이 좋습니다. 왜냐하면 /dev/vda1, /dev/vdb1등 file system은 부팅시마다 Mount Point(mount되는 디렉토리. 위 그림 참조)가 바뀔 수 있기 때문입니다. 아래와 같이 UUID를 이용하여 mount를 하면 재부팅시에도 엉뚱한 file system으로 mount되는것을 방지할 수 있습니다.

df -h 명령으로 mount된 결과를 볼 수 있습니다.

(echo $? 명령은 바로 전 명령의 결과를 보는 방법입니다. 0이면 정상적으로 처리된것입니다.)

 

NFS와 같은 network file system을 mount할때는 조금 다릅니다.

아래와 같이 -t 뒤에 type인 nfs를 지정하고 source에는 node명 또는 IP:디렉토리를 정의합니다.

[root@bastion tmp]# mount -t nfs infra:/data/vol3 ~/tmp/vol3
[root@bastion tmp]# df -h
Filesystem        Size  Used Avail Use% Mounted on
devtmpfs          3.9G     0  3.9G   0% /dev
tmpfs             3.9G   84K  3.9G   1% /dev/shm
tmpfs             3.9G  932K  3.9G   1% /run
tmpfs             3.9G     0  3.9G   0% /sys/fs/cgroup
/dev/xvda2         98G   11G   82G  12% /
/dev/xvda1        976M  196M  730M  22% /boot
shm                63M     0   63M   0% /var/lib/containers/storage/overlay-containers/085af8176e549e373ec9a9ab3d17fd7762209e062baf829bdb091558476fff24/userdata/shm
overlay            98G   11G   82G  12% /var/lib/containers/storage/overlay/8dde267195ba5a698ff194a57646635ac2e82845f9c818c98b6f70a80fd7a550/merged
tmpfs             782M     0  782M   0% /run/user/0
infra:/data/vol1  481G  4.4G  477G   1% /root/tmp/vol1
infra:/data/vol2  481G  4.4G  477G   1% /root/tmp/vol2
infra:/data/vol3  481G  4.4G  477G   1% /root/tmp/vol3

 

un-mount은 umount라는 명령을 사용합니다.

$ umount /common/docs

$ umount /root/tmp/vol3

mount된 디렉토리에 열려 있는 파일이 있으면 un-mount가 안됩니다.

이런 경우는 lsof라는 명령(list open file)으로 그 디렉토리에 열려있는 파일 목록을 보고, 닫은 후 un-mount하면 됩니다.

lsof를 설치합니다. 
[root@bastion tmp]# yum install -y lsof

Loading mirror speeds from cached hostfile
 * epel: ftp.riken.jp
Package lsof-4.87-6.el7.x86_64 already installed and latest version
Nothing to do
[root@bastion tmp]# cd vol1

현재 디렉토리가 ~/tmp/vol1이기 때문에 lsof로 보면 bash파일이 열려 있습니다. 
[root@bastion vol1]# lsof .
COMMAND   PID USER   FD   TYPE DEVICE SIZE/OFF      NODE NAME
lsof     5801 root  cwd    DIR   0,52        6 539160800 .
lsof     5802 root  cwd    DIR   0,52        6 539160800 .
bash    25708 root  cwd    DIR   0,52        6 539160800 .
[root@bastion vol1]# df -h
Filesystem        Size  Used Avail Use% Mounted on
...
infra:/data/vol1  481G  4.4G  477G   1% /root/tmp/vol1
infra:/data/vol2  481G  4.4G  477G   1% /root/tmp/vol2
infra:/data/vol3  481G  4.4G  477G   1% /root/tmp/vol3

bash파일이 열려 있으므로 un-mount가 안됩니다. 
[root@bastion vol1]# umount /root/tmp/vol1
umount.nfs4: /root/tmp/vol1: device is busy

상위 디렉토리로 옮겨 lsof를 해보면 열려있는 파일이 없는걸로 나옵니다. 
[root@bastion vol1]# cd ..
[root@bastion tmp]# lsof ./vol1

이제 un-mount가 정상적으로 됩니다. 
[root@bastion tmp]# umount /root/tmp/vol1
[root@bastion tmp]# umount /root/tmp/vol2
[root@bastion tmp]# umount /root/tmp/vol3
[root@bastion tmp]# df -h
Filesystem      Size  Used Avail Use% Mounted on
devtmpfs        3.9G     0  3.9G   0% /dev
tmpfs           3.9G   84K  3.9G   1% /dev/shm
tmpfs           3.9G  800K  3.9G   1% /run
tmpfs           3.9G     0  3.9G   0% /sys/fs/cgroup
/dev/xvda2       98G   11G   82G  12% /
/dev/xvda1      976M  196M  730M  22% /boot
shm              63M     0   63M   0% /var/lib/containers/storage/overlay-containers/085af8176e549e373ec9a9ab3d17fd7762209e062baf829bdb091558476fff24/userdata/shm
overlay          98G   11G   82G  12% /var/lib/containers/storage/overlay/8dde267195ba5a698ff194a57646635ac2e82845f9c818c98b6f70a80fd7a550/merged
tmpfs           782M     0  782M   0% /run/user/0

File/Directory 찾기

File과 Directory를 찾는 명령에는 find와 locate가 있습니다.

Find

기본형

Find <디렉토리> -name <파일명> 형식이 가장 기본적인 문법입니다.

[hklee@bastion ~]$ find /usr -name sshd
/usr/sbin/sshd
find: ‘/usr/share/polkit-1/rules.d’: Permission denied
find: ‘/usr/libexec/initscripts/legacy-actions/auditd’: Permission denied

위에서 에러는 별도 파일로 보내고 결과에는 표시하고 싶지 않으면 "2> <에러파일>"을 추가합니다.

여기서 2라는 것은 standard error를 의미합니다.

[hklee@bastion ~]$ find /usr -name sshd 2> finderr
/usr/sbin/sshd
[hklee@bastion ~]$ cat finderr
find: ‘/usr/share/polkit-1/rules.d’: Permission denied
find: ‘/usr/libexec/initscripts/legacy-actions/auditd’: Permission denied

에러 파일도 안 만들고, 결과에도 표시하기 싫으면 "2> /dev/null"을 추가합니다.

[hklee@bastion ~]$ find /usr -name sshd 2> /dev/null
/usr/sbin/sshd

 

대소문자 구별 안하고 찾

-name 대신에 -iname을 사용합니다.

[hklee@bastion tmp]$ ls -al
total 8
drwxrwxr-x. 2 hklee hklee 4096 Jun 18 00:40 .
drwx------. 4 hklee hklee 4096 Jun 18 00:39 ..
-rw-rw-r--. 1 hklee hklee    0 Jun 18 00:40 file1
-rw-rw-r--. 1 hklee hklee    0 Jun 18 00:40 File1
[hklee@bastion tmp]$ find . -name file1
./file1
[hklee@bastion tmp]$ find . -iname file1
./file1
./File1

 

wilecard 사용하기

-name 또는 -iname 뒤에 파일명을 큰따옴표로 감싼후에 wild카드를 사용할 수 있습니다.

[hklee@bastion tmp]$ ls -l
total 0
-rw-rw-r--. 1 hklee hklee 0 Jun 18 00:40 file1
-rw-rw-r--. 1 hklee hklee 0 Jun 18 00:46 file2
-rw-rw-r--. 1 hklee hklee 0 Jun 18 00:46 file3
[hklee@bastion tmp]$ find . -name file
[hklee@bastion tmp]$ find . -name "file*"
./file3
./file2
./file1

 

file owner로 찾기

[root@bastion tmp]# ls -l
total 0
-rw-rw-r--. 1 hklee hklee 0 Jun 18 00:40 file1
-rw-rw-r--. 1 hklee hklee 0 Jun 18 00:46 file2
-rw-rw-r--. 1 hklee hklee 0 Jun 18 00:46 file3
-rw-rw-r--. 1 root  root  0 Jun 18 00:47 rootfile
[root@bastion tmp]# find . -user hklee
.
./file3
./file2
./file1
[root@bastion tmp]# find . -user root
./rootfile

 

type으로 찾기

file만 대상으로 찾으려면 -type f, 디렉토리만 대상으로 찾으려면 -type d 로 지정하고 찾으십시오.

[root@bastion tmp]# ls -l
total 24
---x--x--x. 1 root root    0 Jun 18 00:09 file1
-r-xr-xr-x. 1 root root    0 Jun 18 00:09 file2
-rw-r--r--. 1 root root    0 Jun 18 00:09 file3
drwxr-xr-x. 2 root root 4096 Jun 18 00:06 scptest
drwxr-xr-x. 5 root root 4096 Jun 15 11:24 scratch
drwxr-xr-x. 2 root root 4096 Jun  9 17:17 test
drwxr-xr-x. 2 root root 4096 Jun 17 20:27 vol1
drwxr-xr-x. 2 root root 4096 Jun 17 20:29 vol2
drwxr-xr-x. 2 root root 4096 Jun 17 20:29 vol3
[root@bastion tmp]#
[root@bastion tmp]#
[root@bastion tmp]# find . -type f -name "file*"
./file3
./file2
./file1
[root@bastion tmp]# find . -type d -name "vol*"
./vol3
./vol1
./vol2

 

size로 찾기

[root@bastion tmp]# find / -size +200M -name "*.tar"
/root/backup/install.tar
/root/backup/www.tar

 

아래와 같이 최소값과 최대값을 지정하여 찾을수도 있습니다. (-exec에 대해서는 조금 뒤에 설명되어 있습니다.)

[root@bastion ~]# find / -size +100M -name "*.tar" -exec ls -lh {} \;
-rw-r--r--. 1 root root 338M Jun  5 11:52 /root/backup/install.tar
-rw-r--r--. 1 root root 811M Jun  5 11:53 /root/backup/www.tar
[root@bastion ~]# find / -size +200M -size -500M -name "*.tar" -exec ls -lh {} \;
-rw-r--r--. 1 root root 338M Jun  5 11:52 /root/backup/install.tar


수정시간으로 찾기

-mmin을 이용하면 modified된 분을 기준으로 파일을 찾을 수 있습니다.

아래 예는 최근 10분동안 생성/수정된 파일을 /var디렉토리 하위에서 찾습니다.

[root@bastion tmp]# find /var -mmin -10
/var/named/data/named.run
/var/log/secure
/var/log/journal/657ef9edb6454d36878fa53c9ac15a57/system.journal
/var/log/sshd_err.log
/var/log/btmp
/var/log/messages
/var/log/audit/audit.log
/var/lib/rsyslog
/var/lib/rsyslog/imjournal.state

만약 반대로 생성/수정된지 10분 이상인 파일을 찾는다면 -mmin -10을 -mmin +10으로 바꾸면 됩니다.

 

Permission으로 찾기

-perm을 이용하면 permission(r:4, w:2, x:1)으로 파일을 찾을 수 있습니다.

아래 예는 permission이 111인 파일을 찾아 줍니다.

[root@bastion tmp]# find / -perm 111 2> /dev/null
/root/tmp/file1
/usr/bin/sudoreplay
[root@bastion tmp]# ls -l /root/tmp/file1
---x--x--x. 1 root root 0 Jun 18 00:09 /root/tmp/file1

permission이 111을 포함하는 파일들을 찾으려면 -perm /111과 같이 '/'을 사용합니다.

[root@bastion tmp]# ls -l file*
---x--x--x. 1 root root 0 Jun 18 00:09 file1
-r-xr-xr-x. 1 root root 0 Jun 18 00:09 file2
-rw-r--r--. 1 root root 0 Jun 18 00:09 file3
[root@bastion tmp]# find . -name "file*" -perm 111
./file1
[root@bastion tmp]# find . -name "file*" -perm /111
./file2
./file1

 

검색결과 가공하기

find 결과를 이용하여 어떤 명령을 실행할 수 있습니다.

-exec를 활용하십시오.

아래 예제는 검색 결과를 ls -ah로 보기 좋게 가공하는 방법입니다.

'{}'에 find결과가 오게되고 '\;'로 행간 구분을 합니다.

[root@bastion tmp]# find / -type f -size +200M -exec ls -lh {} \; 2> /dev/null
-rw-r--r--. 1 root root 841M May  4 23:56 /root/image/rhcos-4.4.3-x86_64-vmware.x86_64.ova
-rwxr-xr-x. 1 root root 338M Jun  2 04:41 /root/image/openshift-install
-rw-r--r--. 1 root root 338M Jun  5 11:52 /root/backup/install.tar
-rw-r--r--. 1 root root 811M Jun  5 11:53 /root/backup/www.tar
-r--------. 1 root root 128T Jun 18 01:07 /proc/kcore
-rwxr-xr-x. 1 root root 318M Mar  2 17:44 /usr/bin/openshift-install

 

find 결과 파일들을 모두 삭제할 수도 있습니다.

[root@bastion tmp]# ls -al
total 32
drwxr-xr-x.  8 root root 4096 Jun 18 00:54 .
dr-xr-x---. 21 root root 4096 Jun 18 00:01 ..
---x--x--x.  1 root root    0 Jun 18 00:09 file1
-r-xr-xr-x.  1 root root    0 Jun 18 00:09 file2
-rw-r--r--.  1 root root    0 Jun 18 00:09 file3
drwxr-xr-x.  2 root root 4096 Jun 18 00:06 scptest
drwxr-xr-x.  5 root root 4096 Jun 15 11:24 scratch
drwxr-xr-x.  2 root root 4096 Jun  9 17:17 test
drwxr-xr-x.  2 root root 4096 Jun 17 20:27 vol1
drwxr-xr-x.  2 root root 4096 Jun 17 20:29 vol2
drwxr-xr-x.  2 root root 4096 Jun 17 20:29 vol3
[root@bastion tmp]# find . -name "file*" -exec rm -i {} \;
rm: remove regular empty file './file3'? y
rm: remove regular empty file './file2'? y
rm: remove regular empty file './file1'? n
[root@bastion tmp]# ls -l file*
---x--x--x. 1 root root 0 Jun 18 00:09 file1

find 결과 파일을 삭제하는 옵션에는 -delete도 있습니다.

[root@bastion tmp]# ls -l file*
---x--x--x. 1 root root 0 Jun 18 00:09 file1
-rw-r--r--. 1 root root 0 Jun 18 01:10 file2
-rw-r--r--. 1 root root 0 Jun 18 01:10 file3
[root@bastion tmp]# find . -name "file*" -delete
[root@bastion tmp]# ls -l file*
ls: cannot access file*: No such file or directory

 

locate

locate명령은 file위치를 db로 indexing하여 그 위치를 리턴해주는 명령입니다.

사전에 mlocate가 설치되어 있어야 하고, updatedb로 db를 indexing하여야 합니다.

[root@bastion tmp]# yum install -y mlocate

Loading mirror speeds from cached hostfile
 * epel: ftp.riken.jp
Package mlocate-0.26-8.el7.x86_64 already installed and latest version
Nothing to do
[root@bastion tmp]# updatedb
[root@bastion tmp]# locate file1
/home/hklee/tmp/file1
/nogada/nogada_file1
/root/rsynctest/file1

 

아래 예제에서 locate lee 는 locate "*lee*"와 동일한 결과입니다.

[root@bastion tmp]# touch hklee
[root@bastion tmp]# touch mslee
[root@bastion tmp]# touch shlee

아직 indexing이 안되었으므로 못 찾음
[root@bastion tmp]# locate shlee

updatedb로 index갱신하면 찾아줌
[root@bastion tmp]# updatedb
[root@bastion tmp]# locate shlee
/root/tmp/shlee

아래와 같이 wildcard로 찾을 수 있음
[root@bastion tmp]# locate "*lee"
/home/hklee
/root/tmp/hklee
/root/tmp/mslee
/root/tmp/shlee
/var/spool/mail/hklee

아래와 같이 디렉토리까지 지정하여 특정 디렉토리 하위에서만 찾을 수도 있습니다. 
[root@bastion tmp]# locate "/root/tmp/*lee"
/root/tmp/hklee
/root/tmp/mslee
/root/tmp/shlee

 

대소문자 구별없이 검색할때는 -i 옵션을 추가합니다.

[root@bastion tmp]# ls -l *lee
-rw-r--r--. 1 root root 0 Jun 18 01:26 HKlee
-rw-r--r--. 1 root root 0 Jun 18 01:20 hklee
-rw-r--r--. 1 root root 0 Jun 18 01:20 mslee
-rw-r--r--. 1 root root 0 Jun 18 01:21 shlee
[root@bastion tmp]# locate "/root/tmp/hklee"
/root/tmp/hklee
[root@bastion tmp]# locate -i "/root/tmp/hklee"
/root/tmp/HKlee
/root/tmp/hklee

 

댓글

전자책 출간 알림 [마이크로서비스패턴 쉽게 개발하기]