Find and fd
This post is an automatic translation from French. You can read the original version here.
After bat, here’s a new Rust tool to replace the venerable find. The goal this time isn’t to have pretty colors and something all shiny, but rather to simplify the syntax… and I’ll admit that’s not a luxury!
Since I’m far from using find on a daily basis, it wouldn’t be very honest to promote fd without first going over find and its syntax. So we’ll do this article in two chapters: one on find, the other on fd.
1. find
The syntax is basic:
1
find <path> <conditions> <action>
Conditions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
-name "*.c"
-user rancune
-nouser
-type f # File
-type d # Directory
-type l # Symbolic link
-depth 2 # Descend at least 3 levels deep
-regex PATTERN
-size 8 # 8 blocks (of 512 bytes)
-size -128c # Smaller than 128 bytes
-size 144k # Exactly 144 KiB
-size +10M # Larger than 10 MiB
-size +2G # Larger than 2 GiB
-newer file.txt
-newerm file.txt # Modified more recently than file.txt
-newerX file.txt # [c]changed, [m] modified or [B] created
-newerXt "1 hour ago" # based on the timestamp
\! -name "*.c" # logical not
\( x -or y \) # logical or
Actions
1
2
3
-exec rm {} \; # Example for deletion
-print
-delete
Examples
1
2
3
4
5
6
7
8
find . -name '*.jpg'
find . -name '*.c' -exec rm {} \;
find . -newerBt "24 hours ago"
find . -type f -mtimes +29 # Search for files modified more than 30 days ago
find . -iname devel.c # Case insensitive
2. fd
Syntax
1
2
3
fd [-HIEsiaLp0hV] [-d depth] [-t filetype] [-e
ext] [-E exclude] [-c when] [-j num] [-x cmd]
[pattern] [path...]
Simple and fast search:
1
2
3
4
5
6
7
8
9
10
11
fd devel.c # Search for devel.c in the entire filesystem
fd '^x.*rc$' # Search based on regular expression
fd passwd /etc # Search from a specific directory
fd -e md # Search by extension
fd -g libc.so /usr # Search for an exact pattern
fd -e zip -x unzip # Execute a command on each file
fd -e zip -X unzip # Execute a command on the list of files
Now that’s beautiful!
-
To see the details (permissions, owners, etc.) of each matched file:
fd … -X ls -lhd –color=always
-
Convert all *.jpg files to *.png with convert:
fd -e jpg -x convert {} {.}.png
-
fd’s display respects ls colors via the LS_COLORS variable
The final word…
Let’s not kid ourselves, fd is quite attractive. Yet, I find find’s logic quite elegant, even if a bit old-school. I think on my system, fd will find its place, but alongside find… not as a replacement!
Rancune