はじめに
よく使うが忘れるのでメモ。
fdはfindコマンドの高速な代替ツール。デフォルトで正規表現を使い、gitignoreを尊重し、隠しファイルを除外するなど、使いやすいデフォルト設定になっている。
fd コマンド チートシート
概要
fd
は、ファイルシステム内のエントリを検索するためのシンプルで高速なコマンドラインツール。findコマンドの代替として使われる。
デフォルトでは正規表現でパターンマッチングを行うが、--globオプションでグロブパターンも使用可能。また、デフォルトで.gitignoreや.fdignoreのルールを尊重し、隠しファイルやディレクトリを除外する。
基本的な使い方
パターンを指定してファイルを検索する。
fd [pattern] [path...]
パターンを省略すると、すべてのファイルがマッチする。
fd
特定のディレクトリを検索する。
fd pattern /path/to/search
よく使うオプション
| オプション | 説明 |
|---|---|
-H, --hidden |
隠しファイルやディレクトリを含める(デフォルトでは除外される) |
-I, --no-ignore |
.gitignoreや.fdignoreのルールを無視する |
-u, --unrestricted |
無制限検索(--no-ignore --hiddenのエイリアス) |
-s, --case-sensitive |
大文字小文字を区別して検索 |
-i, --ignore-case |
大文字小文字を区別せずに検索 |
-g, --glob |
グロブパターンを使用(デフォルトは正規表現) |
-F, --fixed-strings |
パターンをリテラル文字列として扱う |
-a, --absolute-path |
絶対パスを表示 |
-l, --list-details |
ls -lのような詳細なリスト形式で表示 |
-L, --follow |
シンボリックリンクをたどる |
-p, --full-path |
パターンをフルパスに対してマッチさせる |
-0, --print0 |
結果をnull文字で区切る(xargsと組み合わせる際に便利) |
-d, --max-depth <depth> |
検索の深さを制限 |
-t, --type <filetype> |
ファイルタイプでフィルタ(f: ファイル、d: ディレクトリ、l: シンボリックリンクなど) |
-e, --extension <ext> |
拡張子でフィルタ |
-E, --exclude <pattern> |
グロブパターンで除外 |
-S, --size <size> |
ファイルサイズでフィルタ(例: +10M、-100k) |
-x, --exec <cmd> |
各検索結果に対してコマンドを実行 |
-X, --exec-batch <cmd> |
すべての検索結果を一度にコマンドに渡す |
-j, --threads <num> |
使用するスレッド数を指定 |
応用例
1. 拡張子で検索
特定の拡張子のファイルを検索する。
fd -e md
fd -e js -e ts
2. ファイルタイプでフィルタ
ディレクトリのみを検索する。
fd -t d
実行可能ファイルを検索する。
fd -t x
3. 深さを制限
最大2階層まで検索する。
fd -d 2 pattern
4. 隠しファイルも含めて検索
fd -H pattern
5. gitignoreを無視して検索
fd -I pattern
6. 検索結果に対してコマンドを実行
見つかったファイルを削除する。
fd -e tmp -x rm
見つかったファイルを変換する(例: jpgをpngに)。
fd -e jpg -x convert {} {.}.png
すべての検索結果を一度にコマンドに渡す。
fd -e rs -X wc -l
7. サイズでフィルタ
10MB以上のファイルを検索する。
fd -S +10M
100KB以下のファイルを検索する。
fd -S -100k
8. 変更日時でフィルタ
過去2週間以内に変更されたファイルを検索する。
fd --changed-within 2weeks
特定の日付より新しいファイルを検索する。
fd --changed-after 2024-01-01
特定の日付より古いファイルを検索する。
fd --changed-before 2024-01-01
9. グロブパターンを使用
fd -g '*.md'
fd -g 'test_*.py'
10. フルパスで検索
パターンをフルパスに対してマッチさせる。
fd -p '**/.git/config'
11. 除外パターンを使用
特定のパターンを除外する。
fd --exclude '*.pyc' --exclude node_modules
参考リンク
man fdより引用
FD(1) General Commands Manual FD(1)
NAME
fd - find entries in the filesystem
SYNOPSIS
fd [-HIEsiaLp0hV] [-d depth] [-t filetype] [-e ext] [-E exclude] [-c
when] [-j num] [-x cmd] [pattern [path...]]
DESCRIPTION
fd is a simple, fast and user-friendly alternative to find(1).
By default fd uses regular expressions for the pattern. However, this
can be changed to use simple glob patterns with the '--glob' option.
By default fd will exclude hidden files and directories, as well as any
files that match gitignore rules or ignore rules in .ignore or .fdig‐
nore files.
If you wish to search all files in a specific directory, you can use ''
or . as the search pattern, to match all files. Or you can use the
'--search-path' option to specify the path(s) instead of the positional
parameter.
Options may be given anywhere on the command line.
OPTIONS
-H, --hidden
Include hidden files and directories in the search results (de‐
fault: hidden files and directories are skipped). The flag can
be overridden with '--no-hidden'.
Ignored files are still excluded unless --no-ignore or --no-ig‐
nore-vcs is also used.
-I, --no-ignore
Show search results from files and directories that would other‐
wise be ignored by
• .gitignore
• .git/info/exclude
• The global gitignore configuration (by default $HOME/.con‐
fig/git/ignore)
• .ignore
• .fdignore
• The global fd ignore file (usually $HOME/.config/fd/ignore )
The flag can be overridden with '--ignore'.
-u, --unrestricted
Perform an unrestricted search, including ignored and hidden
files. This is an alias for '--hidden --no-ignore'.
-s, --case-sensitive
Perform a case-sensitive search. By default, fd uses case-insen‐
sitive searches, unless the pattern contains an uppercase char‐
acter (smart case).
-i, --ignore-case
Perform a case-insensitive search. By default, fd uses case-in‐
sensitive searches, unless the pattern contains an uppercase
character (smart case).
-g, --glob
Perform a glob-based search instead of a regular expression
search. If combined with the '--full-path' option, '**' can be
used to match multiple path components.
--regex
Perform a regular-expression based search (default). This can be
used to override --glob.
-F, --fixed-strings
Treat the pattern as a literal string instead of a regular ex‐
pression. Note that this also performs substring comparison. If
you want to match on an exact filename, consider using '--glob'.
-a, --absolute-path
Shows the full path starting from the root as opposed to rela‐
tive paths. The flag can be overridden with '--relative-path'.
-l, --list-details
Use a detailed listing format like 'ls -l'. This is basically an
alias for '--exec-batch ls -l' with some additional 'ls' op‐
tions. This can be used to see more metadata, to show symlink
targets and to achieve a deterministic sort order.
-L, --follow
By default, fd does not descend into symlinked directories. Us‐
ing this flag, symbolic links are also traversed. The flag can
be overridden with '--no-follow'.
-p, --full-path
By default, the search pattern is only matched against the file‐
name (or directory name). Using this flag, the pattern is
matched against the full path.
-0, --print0
Separate search results by the null character (instead of new‐
lines). Useful for piping results to xargs.
-d, --max-depth d
Limit directory traversal to at most d levels of depth. By de‐
fault, there is no limit on the search depth.
-t, --type filetype
Filter search by type:
f, file
regular files
d, dir, directory
directories
l, symlink
symbolic links
b, block-device
block devices
c, char-device
character devices
s, socket
sockets
p, pipe
named pipes (FIFOs)
x, executable
executable (files)
e, empty
empty files or directories
-e, --extension ext
Filter search results by file extension ext. This option can be
used repeatedly to allow for multiple possible file extensions.
-E, --exclude pattern
Exclude files/directories that match the given glob pattern.
This overrides any other ignore logic. Multiple exclude pat‐
terns can be specified.
-S, --size size
Limit results based on the size of files using the format
<+-><NUM><UNIT>
'+' file size must be greater than or equal to this
'-' file size must be less than or equal to this
'NUM' The numeric size (e.g. 500)
'UNIT' The units for NUM. They are not case-sensitive. Allowed
unit values:
'b' bytes
'k' kilobytes (base ten, 10^3 = 1000 bytes)
'm' megabytes
'g' gigabytes
't' terabytes
'ki' kibibytes (base two, 2^10 = 1024 bytes)
'mi' mebibytes
'gi' gibibytes
'ti' tebibytes
--changed-within date|duration
Filter results based on the file modification time. Files with
modification timestamps greater than the argument will be re‐
turned. The argument can be provided as a duration (10h, 1d,
35min) or as a specific point in time as full RFC3339 format
with time zone, as a date or datetime in the local time zone
(YYYY-MM-DD or YYYY-MM-DD HH:MM:SS), or as the prefix '@' fol‐
lowed by the number of seconds since the Unix epoch (@[0-9]+).
--change-newer-than, --newer or --changed-after can be used as
aliases.
--changed-before date|duration
Filter results based on the file modification time. Files with
modification timestamps less than the argument will be returned.
The argument can be provided as a duration (10h, 1d, 35min) or
as a specific point in time as full RFC3339 format with time
zone, as a date or datetime in the local time zone (YYYY-MM-DD
or YYYY-MM-DD HH:MM:SS), or as the prefix '@' followed
by the number of seconds since the Unix epoch (@[0-9]+).
--change-older-than or --older can be used as aliases.
-x, --exec command
Execute command for each search result in parallel (use
--threads=1 for sequential command execution).
Note that all subsequent positional arguments are considered to
be arguments to the command - not to fd. It is therefore recom‐
mended to place the -x/--exec option last. Alternatively, you
can supply a ';' argument to end the argument list and continue
with more fd options.
Before executing the command, any placeholder patterns in the
command are replaced with the corresponding values for the cur‐
rent file. The same placeholders are used as in the "--format"
option.
If no placeholder is present, an implicit "{}" at the end is as‐
sumed.
-X, --exec-batch command
Execute command once, with all search results as arguments.
The order of the arguments is non-deterministic and should not
be relied upon.
This uses the same placeholders as "--format" and "--exec", but
instead of expanding once per command invocation each argument
containing a placeholder is expanding for every file in a batch
and passed as separate arguments.
If no placeholder is present, an implicit "{}" at the end is as‐
sumed.