fd

fd is a better alternative to the find command. In my opinion, it’s faster, it has sane defaults (eg. no need of -iname, -e flag), shorter flags (eg. -x, -X) and better conventions overall. I use fd every single day, just by itself, and in conjunction with other tools.

Repository Link: https://github.com/sharkdp/fd

Intro

fd

It prints all the files & directories.

Search for files and directories

fd readme

What it does is return everything that has the term ‘readme’ in it (regardless of the case and whether it’s a file or directory).

Include hidden files and directories

fd doesn’t include hidden files and directories by default. Use -H flag to enable it.

fd -H      # short for --hidden

Search for files in a certain directory

Do this: fd <term> <directory>

For example, if you want to search anything that has doc in it within the /usr directory:

fd doc /usr

Search for the exact file/directory

This is where the -g flag (short for --glob) comes in:

fd -g readme.rst

Filter files, directories separately

To retrieve files only:

fd -tf      # short for --type=file

To retrieve directories only:

fd -td      # short for --type=directory

Filter based on extensions

Suppose you want to gather only the PDFs in a directory:

fd -e pdf      # short for --extension=pdf

Exclude files & directories

What if you are in a project folder and want to exclude the build/ directory?
What if you are in a Hugo project folder and want to exclude the public/ directory?

fd --exclude=public

Traverse through symlinks

Why traverse through symlinks?

Well, in my case, in Termux, when you run termux-setup-storage, it sets up a bunch of symlinks leading to the system Downloads dir, the internal storage etc. So, if you are in ~, it helps to use the -L flag to find files within those symlinks.

fd -L <query>

Exact Depth

What is --exact-depth? Suppose you wanna search within only a few levels down, or just one. Instead of typing --min-depth=1 --max-depth=1, you could use this flag instead.

I use it mostly when I want to search within the current directory without traversing down to the directories within.

Let me explain. Suppose you’re in content/ dir, and this is its structure:

    • _index.md
      • _index.md
      • 1.md
      • 2.md
  • hugo.toml
  • If you run fd -e md, it will return all the md files, including the ones in the docs/ directory. But what if you want to return only the files in content/, and not the ones that are levels down? That’s where --exact-depth comes useful.

    Execute commands

    This is where fd’s real power comes in: using it in conjunction with other tools.

    Example 1: Sort all my blog entries in markdown based on line count. I’ll give the output as well.

    ➜ fd -e md -X wc -l --total=never | sort -nr
      368 ./content/thoughts/javascript.md
      171 ./content/thoughts/how my metal addiction went away.md
      128 ./themes/typo/CODE_OF_CONDUCT.md
      121 ./content/thoughts/kde_malayalam.md
      109 ./content/thoughts/i made a website for my father.md
      105 ./content/thoughts/the kid and love..md
       80 ./content/thoughts/gender-politics.md
       ...

    Example 2: Search for a term across my blog entries.

    ➜ fd -tf -X rg -i "file manager"
    ./i made a website for my father.md
    69:- **[yazi](https://github.com/sxyazi/yazi)**: fast terminal-based file manager.
    
    ./iphone.md
    20:it got no proper file manager...

    Example 3: Batch-convert a bunch of PNGs into WEBP in one click. (yeah, i like WEBP)

    ➜ fd -e png -X cwebp {} -o {.}.webp