Menemukan fail
Pola: find [nama-direktori] -name [nama-file]
Contoh
pc060066:~$ find . -name tes.txt
./tes.txt
Tanda titik setelah find menunjukkan current directory (dalam hal ini /home/$USER).
Menemukan dan menghapus fail
Pola: find [nama-direktori] -name [nama-file-yang-dihapus] -delete
Contoh
pc060066:~$ cp tes.txt tes-del.txt
pc060066:~$ find . -name tes-del.txt
./tes-del.txt
pc060066:~$ find . -name tes-del.txt -delete
pc060066:~$ find . -name tes-del.txt
Terlihat file yang dihapus (tes-del.txt) tidak ada setelah perintah di atas.
Menemukan dan menghapus fail ekstensi tertentu
Pola: find [nama-direktori] -name ['*.ext'] -delete
pc060066:~$ mkdir test
pc060066:~$ cd test
pc060066:test$ ls
pc060066:test$ touch test{1..5}.txt
pc060066:test$ ls
test1.txt test2.txt test3.txt test4.txt test5.txt
pc060066:test$ touch readme.md
pc060066:test$ ls
readme.md test1.txt test2.txt test3.txt test4.txt test5.txt
pc060066:test$ find . -name '*.txt'
./test1.txt
./test3.txt
./test2.txt
./test5.txt
./test4.txt
pc060066:test$ find . -name '*.txt' -delete
pc060066:test$ ls *.txt
ls: cannot access '*.txt': No such file or directory
Jangan lupa single quote diantara ekstensi ('*.txt'); untuk ekstensi lainnya tanda quote ini tidak perlu. Contohnya *.wav. Kita juga bisa mencari (dan menghapus) file ekstensi tertentu dengan nama tertentu. Contohnya menemukan (dan menghapus) file dengan nama berakhiran *_cd16k.wav.
Menemukan dan menghapus fail ukuran tertentu
Pola: find [nama-direktori] -name [nama-file-opsional] -size [ukuran, -, +] -delete
Contoh:
pc060066:test$ find . -size 4c
./test8.txt
pc060066:test$ find . -size 4c -delete
pc060066:test$ ls
1001_DFA_ANG_XX.wav test1.txt test3.txt test5.txt test7.txt
readme.md test2.txt test4.txt test6.txt
pc060066:test$ find . -size -10c # find below 10 bytes
./test6.txt
./test1.txt
./test3.txt
./readme.md
./test2.txt
./test5.txt
./test4.txt
pc060066:test$ find . -size -10c -delete # delete below 10 bytes
pc060066:test$ ls
1001_DFA_ANG_XX.wav test7.txt
pc060066:test$ find . -size +10c -delete # delete above 10 bytes
pc060066:test$ ls
Jadi tanda "-" untuk kurang dari dan "+" untuk lebih dari ukuran file yang dikehendaki. Tidak ada tanda maka hasilnya pada rentang nilai tersebut. Misal 10c untuk 10 bytes, 10k untuk 10 kilobytes, termasuk 10001 bytes sampai dengan 10999 bytes.
Menghapus file kosong
Pola: find [nama-direktori] -empy -delete
Contoh:
pc060066:test$ find . -empty
./test10.txt
./test6.txt
./test1.txt
./test3.txt
./test2.txt
./test8.txt
./test5.txt
./test9.txt
./test4.txt
./test7.txt
pc060066:test$ find . -empty -delete
pc060066:test$ ls
Menghapus direktori kosong
Pola: find [nama-direktori] -d -empty -delete
Contoh:
pc060066:test$ ls -ltr
total 20
drwxrwxr-x 2 bagus bagus 4096 4月 14 15:33 test_dir_5
drwxrwxr-x 2 bagus bagus 4096 4月 14 15:33 test_dir_4
drwxrwxr-x 2 bagus bagus 4096 4月 14 15:33 test_dir_3
drwxrwxr-x 2 bagus bagus 4096 4月 14 15:33 test_dir_2
drwxrwxr-x 2 bagus bagus 4096 4月 14 15:33 test_dir_1
pc060066:test$ find . -type d
.
./test_dir_5
./test_dir_4
./test_dir_2
./test_dir_1
./test_dir_3
pc060066:test$ find . -type d -empty
./test_dir_5
./test_dir_4
./test_dir_2
./test_dir_1
./test_dir_3
pc060066:test$ find . -type d -empty -delete
pc060066:test$ ls
Bedakan dengan perintah sebelumnya untuk file, untuk direktori kita perlu argumen "-type d".
Menemukan dan menghapus kecuali
Pola: find [nama-direktori] not -name [kecuali] -delete
Contoh:
find . not -name "*_mono.wav" -delete
Argumen "not" bisa diganti dengan tanda seru "!" agar lebih singkat. Untuk folder yangt tidak kosong (directory is not empty), kita perlu menggunakan "rm -fr" sebagai pengganti argumen "-delete".
find . -mindepth 1 ! -name "kecuali-direktori1" ! -name "*.py" ! -name "KECUALI.md" ! -name "kecuali-direktori2" -exec rm -rf {} +