Linux - fd

What is the purpose of the fd command?

fd is an alternative to the find command.

How can I tell fd to list files matching a certain pattern?

fd pattern

How can I tell fd to skip certain directory?

fd pattern -E folder_to_skip
fd my_query -E '*/cache'
fd -e py -E node_modules -E vendor  # Find all .py files, but ignore the node_modules and vendor folder.

To exclude any directory with a specific name, regardless of its location in the directory tree, use a wildcard in your glob pattern.

To exclude multiple directories, you can either use the -E option multiple times or combine the patterns using a brace expansion glob.

For multiple directory names, a single -E with brace expansion is more concise and easier to read:

fd my_query -E '{cache,temp,logs}'

Remember to quote the brace expansion to prevent your shell from expanding it prematurely.

How can we tell fd to ignore hidden directories?

fd excludes hidden files and directories (those that start with a dot) by default. If you've used the -H or —hidden flag to search hidden files and want to specifically exclude a certain hidden directory, you can still use -E:

# Search all files, including hidden ones, but exclude the ".git" directory
fd -H -E .git

fd respects .gitignore files automatically when searching inside a Git repository. If you want to exclude additional patterns globally, you can add them to fd's global ignore file, which is typically located at ~/.config/fd/ignore

How can we tell fd to ignore certain files?

To tell fd to ignore certain files, you use the -E or —exclude option with a glob pattern. This is the same option used for excluding directories. You can specify a file by its exact name, by its extension, or by a pattern within its name.

fd api -E 'config.json'
fd report -E '*.bak'
fd -e txt -E 'config.json' -E 'tmp.log' # Find all text files, but ignores the specified files.
fd . -E '*.{tmp,swp}' # Find all files, but ignore temporary files and swap files

What is the purpose of the —glob option?

By default, fd uses regular expressions for its search pattern. However, the exclude pattern (-E) always uses glob syntax. If you want to use glob syntax for your main search pattern as well, you can use the —glob (-g) option. This can be helpful if you want to avoid confusion between the two syntax styles.

# Find all files whose name ends with '.md' using glob syntax, but exclude a specific file named 'README.md'
fd --glob '*.md' -E 'README.md'

Does fd have an 'ignore' file?

For a more permanent way to ignore files, you can use fd's ignore file feature.

Local .gitignore: fd automatically respects .gitignore rules in Git repositories.

Local .ignore or .fdignore: fd also honors files named .ignore or .fdignore placed in your project directories.

Global ignore file: You can create a global ignore file, which is typically located at ~/.config/fd/ignore on macOS and Linux, to have fd ignore certain patterns globally.

What do I actually use the fd command for so far?

cd ~/DK/outpost/jobs
fd -E '{venv,daglib.py,.vscode}' | xargs rm

The above command delete all files from inside the ~/DK/outpost/jobs folder, except for the mentioned files that I do not want to delete.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License