The Linux directory tree obeys the Filesystem Hierarchy Standard, which allows Linux users and developers to locate system files in standard directories. Since Linux is a very file-oriented operating system, it is important that you know how to find files. It exists some available commands which can help to the job. In this article, I will show you how you can find your files with the fd command on your Linux system.
What is fd command?
fd is a simple, fast and user-friendly alternative to the Linux find command. It has the particularity to colorize the terminal output and can ignore hidden directories/files. It also supports the use of regular expressions.
Install fd Tool
fd command needs the rust's package manager cargo to be installed.
Install rust packages and cargo command
So we will first install rustc and cargo
# curl https://sh.rustup.rs -sSf | sh info: downloading installer Welcome to Rust! This will download and install the official compiler for the Rust programming language, and its package manager, Cargo. It will add the cargo, rustc, rustup and other commands to Cargo's bin directory, located at: /root/.cargo/bin This path will then be added to your PATH environment variable by modifying the profile files located at: /root/.profile /root/.bash_profile You can uninstall at any time with rustup self uninstall and these changes will be reverted.
To get started you need Cargo's bin directory ($HOME/.cargo/bin) in your PATH environment variable. You can configure it in your current shell by running
# source $HOME/.cargo/env
You can check as below
# echo $PATH /root/.cargo/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
Now we can install fd
Install fd command
now that cargo is installed, we can install the fd command
# cargo install fd-find Updating registry `https://github.com/rust-lang/crates.io-index` Downloading fd-find v3.0.0 Installing fd-find v3.0.0 Downloading ignore v0.2.2 .......................... .......................... Compiling fd-find v3.0.0 Finished release [optimized] target(s) in 186.48 secs Installing /root/.cargo/bin/fd
How to use fd command to find file
Before using fd command, we need to understand how it works and this is possible by reading the help of the command
# fd --help fd 3.0.0 USAGE: fd [FLAGS/OPTIONS] [<pattern>] [<path>] FLAGS: -s, --case-sensitive Case-sensitive search (default: smart case) -p, --full-path Search full path (default: file-/dirname only) -H, --hidden Search hidden files and directories -I, --no-ignore Do not respect .(git)ignore files -L, --follow Follow symlinks -0, --print0 Separate results by the null character -a, --absolute-path Show absolute instead of relative paths -n, --no-color Do not colorize output -h, --help Prints help information -V, --version Prints version information OPTIONS: -d, --max-depth Set maximum search depth (default: none) -j, --threads The number of threads used for searching ARGS: the search pattern, a regular expression (optional) the root directory for the filesystem search (optional)
Now that we have seen how the concept of the command, we can try to find a file as below
# fd book cargo/src/doc/book cargo/src/doc/book/book.toml
We can search files which end for example with "jpeg"
# fd jpeg$ Pictures/centos-logo.png.jpeg Pictures/redhat.jpeg Pictures/mozilla.jpeg
or find file which start with bbb
# fd ^bbb Pictures Pictures/BigBluebutton/bbb-check-final-config.jpg Pictures/BigBluebutton/bbb-check-final-config.png Pictures/BigBluebutton/bbb-check.png Pictures/BigBluebutton/bbb-conf-check.jpg
We can include hidden folder for research
# fd -H config config .gitconfig .config .ssh/config
Let us take our first example with -p parameter to see the result
# fd -p book cargo/src/doc/book cargo/src/doc/book/README.md cargo/src/doc/book/book.toml cargo/src/doc/book/src ...................... ...................... cargo/src/doc/book/src/reference/specifying-dependencies.md cargo/src/doc/book/theme cargo/src/doc/book/theme/favicon.png
You can see that for the same research, it display this time the same result plus more other information. It shows the result of each path which contains the word book
It is possible to use the command without any argument and it will print each file contained in a directory on each line of the terminal
# fd gifcurry-linux-22.214.171.124.tar.gz funny-cat.mkv plage jumpfm labor gifcurry-linux-126.96.36.199 duplicate ................... ................... electron-quick-start/main.js electron-quick-start/renderer.js electron-quick-start/index.html electron-quick-start/README.md electron-quick-start/package.json electron-quick-start/LICENSE.md jumpfm/build/icons jumpfm/build/superhero.png.ico jumpfm/build/superhero.png.icns jumpfm/ts/files.ts jumpfm/fonts/RobotoMono-Regular.ttf labor/package/pack6.txt gifcurry-linux-188.8.131.52/packages/00-index.tar gifcurry-linux-184.108.40.206/packages/00-index.cache
fd command can be useful to find files than the other command. Colorize the result is very useful to see the result and it is possible, for some reason to deactivate the result colorization.