본문 바로가기
Linux

find -exec로 파일 삭제 하는 법

by 졸린하루 2020. 10. 22.

1. 파일의 고유 숫자로 삭제

[11:12:22 root@scripttest:/root] ls -li <- 확인
total 204820
67170755 -rw-------. 1 root root      2305 Oct 21 17:14 anaconda-ks.cfg 
67402916 -rwxr-xr-x. 1 root root     10359 Oct 22 09:02 dbsc.sh
67170759 -rw-r--r--. 1 root root      2336 Oct 21 17:21 initial-setup-ks.cfg 
67458820 -rw-r--r--  1 root root 104857600 Oct 22 11:10 test
67458821 -rw-r--r--  1 root root 104857600 Oct 22 11:11 ㅀ?ㅀ.txt

ls -li 옵션으로 파일의 고유 숫자 값을 확인한다.

여기에서 보면 한글로 써진 깨진 파일도 포함되어 있다는 걸 확인 가능하다. 먼저 test 파일 삭제

[11:12:34 root@scripttest:/root] find . -inum 67458820 -exec rm -f {} \;   <- 삭제 명령어
[11:14:16 root@scripttest:/root] ls -li <- 확인
total 102420
67170755 -rw-------. 1 root root      2305 Oct 21 17:14 anaconda-ks.cfg 
67402916 -rwxr-xr-x. 1 root root     10359 Oct 22 09:02 dbsc.sh 
67170759 -rw-r--r--. 1 root root      2336 Oct 21 17:21 initial-setup-ks.cfg 
67458821 -rw-r--r--  1 root root 104857600 Oct 22 11:11 ㅀ?ㅀ.txt

find -exec 명령어로 삭제한 걸 볼 수 있다. 똑같이 깨진 파일도 삭제 해보자.

[11:14:22 root@scripttest:/root] find . -inum 67458821 -exec rm -f {} \;   <- 삭제 명령어
[11:16:11 root@scripttest:/root] ls -li <- 확인
total 20
67170755 -rw-------. 1 root root  2305 Oct 21 17:14 anaconda-ks.
67402916 -rwxr-xr-x. 1 root root 10359 Oct 22 09:02 dbsc.
67170759 -rw-r--r--. 1 root root  2336 Oct 21 17:21 initial-setup-ks.


깨진 파일도 삭제가 되는 걸 확인 할 수 있다. 

 

2. 파일의 이름으로 삭제


[11:22:50 root@scripttest:/root] ls -al <- 확인
total 7216
dr-xr-x---.  6 root root    4096 Oct 22 11:22 .
dr-xr-xr-x. 19 root root     263 Oct 22 09:18 ..
-rw-------.  1 root root    2305 Oct 21 17:14 anaconda-ks.cfg
-rw-------.  1 root root     537 Oct 22 09:21 .bash_history
-rw-r--r--.  1 root root      18 Dec 29  2013 .bash_logout
-rw-r--r--.  1 root root     176 Dec 29  2013 .bash_profile
-rw-r--r--.  1 root root     176 Dec 29  2013 .bashrc
drwx------.  4 root root      31 Oct 21 17:22 .cache
drwx------.  6 root root      58 Oct 22 09:08 .config
-rw-r--r--.  1 root root     100 Dec 29  2013 .cshrc
-rwxr-xr-x.  1 root root   10359 Oct 22 09:02 dbsc.sh
drwx------.  3 root root      25 Oct 21 17:19 .dbus
-rw-r--r--.  1 root root    2336 Oct 21 17:21 initial-setup-ks.cfg 
drwxr-xr-x.  3 root root      19 Oct 21 17:21 .local
-rw-r--r--.  1 root root     129 Dec 29  2013 .tcshrc
-rw-r--r--   1 root root 1048576 Oct 22 11:22 test
-rw-r--r--   1 root root 1048576 Oct 22 11:22 test1
-rw-r--r--   1 root root 1048576 Oct 22 11:22 test2
-rw-r--r--   1 root root 1048576 Oct 22 11:22 test3
-rw-r--r--   1 root root 1048576 Oct 22 11:22 test4
-rw-r--r--   1 root root 1048576 Oct 22 11:22 test5
-rw-r--r--   1 root root 1048576 Oct 22 11:22 test6

ls 로 삭제할 파일을 확인. 여기에서는 모든 test 파일을 삭제 한다.

 

[11:26:05 root@scripttest:/root] find . -name "test*" -exec rm -f {} \;  <- 삭제 명령어
[11:26:20 root@scripttest:/root] ls -al <- 확인
total 44
dr-xr-x---.  6 root root   234 Oct 22 11:26 .
dr-xr-xr-x. 19 root root   263 Oct 22 09:18 ..
-rw-------.  1 root root  2305 Oct 21 17:14 anaconda-ks.cfg 
-rw-------.  1 root root   537 Oct 22 09:21 .bash_history
-rw-r--r--.  1 root root    18 Dec 29  2013 .bash_logout
-rw-r--r--.  1 root root   176 Dec 29  2013 .bash_profile
-rw-r--r--.  1 root root   176 Dec 29  2013 .bashrc
drwx------.  4 root root    31 Oct 21 17:22 .cache
drwx------.  6 root root    58 Oct 22 09:08 .config
-rw-r--r--.  1 root root   100 Dec 29  2013 .cshrc
-rwxr-xr-x.  1 root root 10359 Oct 22 09:02 dbsc.sh 
drwx------.  3 root root    25 Oct 21 17:19 .dbus
-rw-r--r--.  1 root root  2336 Oct 21 17:21 initial-setup-ks.cfg 
drwxr-xr-x.  3 root root    19 Oct 21 17:21 .local
-rw-r--r--.  1 root root   129 Dec 29  2013 .tcshrc

 

모든 test 파일이 사라진 걸 확인 할 수 있다. 물론 * 모두 선택은 신중하게 해야한다.

'Linux' 카테고리의 다른 글

Linux VIP 설정 방법  (0) 2020.12.04
CVE-2020-25705 RHEL 완화법  (0) 2020.11.30
lvm extend 로 lvm 용량 확장 하는 법  (0) 2020.10.21
User Passwd 변경 스크립트  (0) 2020.08.04
fdisk 스크립트  (0) 2020.08.04