# Referensi Cepat find

*Pencarian file berdasarkan nama, tipe, ukuran, waktu, izin, dan aksi*

> Source: GNU Findutils (gnu.org/software/findutils) · MIT

## Pencarian Dasar

### Menjalankan find

```
find .                              # list all files recursively
find /var/log                       # search from specific path
find . -name "*.txt"               # find by name
find / -name "config" 2>/dev/null  # suppress permission errors
find dir1 dir2 -name "*.py"        # search multiple directories
```

### Ringkasan Sintaks

| Command | Description |
|---------|-------------|
| `find [path...] [tests] [actions]` | Bentuk umum — path, lalu test, lalu aksi |
| `path` | Direktori awal (default: direktori saat ini) |
| `test` | Kondisi filter file (`-name`, `-type`, dll.) |
| `action` | Yang dilakukan pada hasil (`-print`, `-exec`, dll.) |
| `Aksi default` | `-print` jika tidak ada aksi yang ditentukan |

## Pola Nama

### Pencocokan Nama

```
find . -name "*.log"               # case-sensitive glob
find . -iname "readme*"            # case-insensitive glob
find . -name "*.py" -o -name "*.js"  # OR: Python or JS files
find . -path "*/src/*.ts"          # match against full path
find . -regex '.*\.\(py\|js\)'    # POSIX regex on full path
```

### Test Nama

| Command | Description |
|---------|-------------|
| `-name pattern` | Basename cocok dengan shell glob (case-sensitive) |
| `-iname pattern` | Basename cocok dengan glob (case-insensitive) |
| `-path pattern` | Path lengkap cocok dengan shell glob |
| `-ipath pattern` | Path lengkap cocok dengan glob (case-insensitive) |
| `-regex pattern` | Path lengkap cocok dengan regular expression |
| `-iregex pattern` | Path lengkap cocok dengan regex (case-insensitive) |

## Filter Tipe

### Filter berdasarkan Tipe

```
find . -type f                     # regular files only
find . -type d                     # directories only
find . -type l                     # symbolic links
find . -type f -name "*.sh"        # combine type + name
```

### Tipe File

| Command | Description |
|---------|-------------|
| `-type f` | File biasa |
| `-type d` | Direktori |
| `-type l` | Symbolic link |
| `-type b` | Block device |
| `-type c` | Character device |
| `-type p` | Named pipe (FIFO) |
| `-type s` | Socket |
| `-empty` | File atau direktori kosong |

## Ukuran & Waktu

### Contoh Ukuran & Waktu

```
find . -size +100M                 # larger than 100 MB
find . -size -1k                   # smaller than 1 KB
find . -mtime -7                   # modified in last 7 days
find . -mmin -30                   # modified in last 30 minutes
find . -newer reference.txt        # newer than reference file
```

### Test Ukuran & Waktu

| Command | Description |
|---------|-------------|
| `-size +/-Nc` | Ukuran dalam byte (c), kilobyte (k), megabyte (M), gigabyte (G) |
| `-mtime +/-N` | Dimodifikasi N*24 jam lalu (+lebih lama, -lebih baru) |
| `-atime +/-N` | Diakses N*24 jam lalu |
| `-ctime +/-N` | Status berubah N*24 jam lalu |
| `-mmin +/-N` | Dimodifikasi N menit lalu |
| `-newer file` | Dimodifikasi lebih baru dari file |
| `-newermt date` | Dimodifikasi setelah tanggal string (GNU) |

## Izin

### Contoh Izin

```
find . -perm 644                   # exact permissions: rw-r--r--
find . -perm -u+x                  # user has execute bit set
find . -perm /o+w                  # others have write (any match)
find . -user root                  # owned by root
find . -group www-data -type f     # owned by group
```

### Test Izin

| Command | Description |
|---------|-------------|
| `-perm mode` | Kecocokan izin tepat |
| `-perm -mode` | Semua bit yang ditentukan aktif |
| `-perm /mode` | Salah satu bit yang ditentukan aktif |
| `-user name` | Dimiliki oleh user (nama atau UID) |
| `-group name` | Dimiliki oleh group (nama atau GID) |
| `-nouser` | Tidak ada user yang cocok di /etc/passwd |
| `-nogroup` | Tidak ada group yang cocok di /etc/group |

## Aksi

### Contoh Aksi

```
find . -name "*.log" -print        # print paths (default)
find . -name "*.tmp" -delete       # delete matching files
find . -type f -ls                 # detailed listing
find . -name "*.txt" -print0       # null-delimited output
find . -type f -printf "%p %s\n"   # custom format (GNU)
```

### Referensi Aksi

| Command | Description |
|---------|-------------|
| `-print` | Cetak path (dipisah newline) |
| `-print0` | Cetak path (dipisah null, aman untuk xargs) |
| `-ls` | Cetak detail file (seperti `ls -dils`) |
| `-delete` | Hapus file yang cocok (mengimplikasikan `-depth`) |
| `-printf format` | Format output kustom (GNU): `%p` path, `%s` ukuran, `%t` waktu |
| `-fprint file` | Tulis path ke file |
| `-quit` | Keluar setelah kecocokan pertama |

## Menggabungkan Test

### Operator Logika

```
find . -name "*.py" -type f         # implicit AND
find . -name "*.py" -a -size +10k   # explicit AND
find . -name "*.py" -o -name "*.js" # OR
find . ! -name "*.log"              # NOT
find . \( -name "*.py" -o -name "*.js" \) -type f
```

### Referensi Operator

| Command | Description |
|---------|-------------|
| `expr1 expr2 / expr1 -a expr2` | AND — keduanya harus true (default) |
| `expr1 -o expr2` | OR — salah satu harus true |
| `! expr / -not expr` | NOT — negasikan ekspresi |
| `\( expr \)` | Kelompokkan ekspresi (escape tanda kurung di shell) |
| `Urutan evaluasi` | Kiri ke kanan; `-a` lebih kuat dari `-o` |

## Exec & Delete

### Contoh Exec

```
find . -name "*.sh" -exec chmod +x {} \;
find . -name "*.log" -exec rm {} +
find . -type f -exec grep -l "TODO" {} +
find . -name "*.bak" -ok rm {} \;   # prompt before each
find . -name "*.tmp" -print0 | xargs -0 rm
```

### Referensi Exec

| Command | Description |
|---------|-------------|
| `-exec cmd {} \;` | Jalankan cmd sekali per file (`{}` = path file) |
| `-exec cmd {} +` | Jalankan cmd dengan beberapa file sekaligus (lebih cepat) |
| `-ok cmd {} \;` | Seperti `-exec` tapi minta konfirmasi |
| `-execdir cmd {} \;` | Jalankan cmd dari direktori file |
| `xargs -0` | Dipasangkan dengan `-print0` untuk pemrosesan batch yang aman |
| `-delete` | Hapus file; proses dari terdalam terlebih dahulu |

## Kedalaman & Pruning

### Contoh Kedalaman & Prune

```
find . -maxdepth 1 -type f          # current dir only
find . -mindepth 2 -name "*.py"     # skip top-level
find . -name ".git" -prune -o -print # skip .git dirs
find . -depth -name "*.tmp" -delete  # process children first
```

### Opsi Kedalaman

| Command | Description |
|---------|-------------|
| `-maxdepth N` | Turun maksimal N level (0 = hanya path awal) |
| `-mindepth N` | Jangan terapkan test pada level kurang dari N |
| `-depth` | Proses isi direktori sebelum direktori itu sendiri |
| `-prune` | Jangan masuk ke direktori yang cocok |
| `-mount / -xdev` | Jangan melewati batas filesystem |
| `-follow / -L` | Ikuti symbolic link |

## Pola Umum

### One-Liner

```
find . -name "*.pyc" -delete             # clean Python bytecode
find . -type f -size 0 -delete            # remove empty files
find . -mtime +30 -name "*.log" -delete   # purge old logs
find . -type f -name "*.md" | wc -l       # count Markdown files
find . -type d -empty -delete             # remove empty dirs
```

### Resep

| Command | Description |
|---------|-------------|
| `Cari file terbesar` | `find . -type f -printf '%s %p\n' \| sort -rn \| head` |
| `Cari duplikat berdasarkan nama` | `find . -type f \| awk -F/ '{print $NF}' \| sort \| uniq -d` |
| `Ganti nama ekstensi` | `find . -name '*.txt' -exec rename 's/.txt/.md/' {} +` |
| `Cari broken symlink` | `find . -xtype l` |
| `Arsip file terbaru` | `find . -mtime -7 -print0 \| tar czf recent.tar.gz --null -T -` |
| `Cari dalam file kode` | `find . -name '*.py' -exec grep -l 'pattern' {} +` |
