linux

20210816 (월 ) find

gusalstm 2021. 8. 17. 09:31
반응형

find 명령어는 경로 지정, 타입, 시간 등 다양한 조합으로 파일을 찾을 수 있다.

Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]

찾는것 뿐만 아니라 exec 명령어를 통해


-type f: 파일
-type d: 디렉터리
-name : 정확히 일치하는 이름
-iname : 대문자 포함
-mmin -10 : modified ten minutes ago
[root@localhost ~]# touch new
[root@localhost ~]# find . -type f -mmin -10
./new
[root@localhost ~]# find . -type f -mmin +1 -mmin -5 : more than 1minute ago and less than 5minute ago
[root@localhost ~]# find . -type f -mtime -20 : less than 20 days ago (최신)
[root@localhost ~]# find . -type f -mtime -20 : less than 20 days ago (20일이상 오래된)
[user@localhost ~]$ find . -size +5M (5메가 이상 큰 파일)
[user@localhost ~]$ find . -perm 777 -type d
./work
[user@docker-host0 ~]$ ls
20210331  centos7-d2.data  Documents  harbor                               host_data.txt  Pictures  Templates  work
20210414  Desktop          Downloads  harbor-offline-installer-v2.3.1.tgz  Music          Public    Videos
[user@localhost ~]$ ls -ld work/
drwxrwxrwx. 4 root root 81 Aug 13 17:20 work/
[user@localhost ~]$ sudo find 20210331 -exec chown root:user {} +
[sudo] password for user:
[user@localhost ~]$ cd 20210331/
[user@localhost 20210331]$ ls -l
total 4
-rw-rw-r--. 1 root user 7 Mar 31 17:16 file01
[user@localhost ~]$ sudo find . -type d -exec chown root:user {} +
[user@localhost ~]$ find . -type f -name "*.jpg" -maxdepth 1  : 하위디렉터리 제외하고 일반파일 jpg 검색
[user@localhost ~]$ find . -type f -name "*.jpg" -maxdepth 1 -exec rm {} + : jpg 삭제

[root@localhost ~]# find / -type f -user "hera" -exec rm {} \; : 검색된 파일을 모두 삭제한다

 

[root@localhost hera]# find / -type f -user "hera" -exec cp {} . \; : 검색된 파일을 현재 디렉터리로 복사한다.

[root@localhost hera]# find / -type f -user "hera" -exec cp {} -a . \;

 -a 옵션은 소유자명을 바뀌지 않게 해준다.

 -a 옵션이 없으면 현재 사용자가 cp명령어를 실행하게 되므로 모든 복사파일의 소유자는 현재사용자로 바뀐다.

 

-user -uid 등을 사용가능하다.

 -user : file is owned by user uname (numeric uesr ID allowed).

 

       -type c
              File is of type c:
              b      block (buffered) special
              c      character (unbuffered) special
              d      directory
              p      named pipe (FIFO)
              f      regular file
              l      symbolic link; this is never true if the -L option or the -follow option is  in  effect,  unless
                     the symbolic link is broken.  If you want to search for symbolic links when -L is in effect, use
                     -xtype.
              s      socket
              D      door (Solaris)

728x90