Make writing a habit together! This is the fourth day of my participation in the “Gold Digging Day New Plan · April More text Challenge”. Click here for more details.

This is HelloGitHub’s Explain Open Source project series, a quick introduction to interesting open source projects in one article.

Today we recommend a good + open source file search tool – FD

The tool supports most major operating systems, so come update your toolkit and enjoy the benefits of open source projects!

I. Introduction to FD

Are you still having trouble finding documents? Are you still having trouble remembering a lot of find parameters? Let’s take a look at the project FD that I recommend this time.

Project address: github.com/sharkdp/fd

A simple, fast and user-friendly alternative to ‘find’

I’m going to put up a picture here, just to give you an intuition

Fd is a command line tool that provides a variety of convenient options for searching files, and the default is color output. The project itself is written in Rust. As a system-level programming language Rust has a running speed comparable to C++, so does fd. Moreover, it provides powerful functions for users to search according to various conditions.

This excellent project has successfully attracted your attention ~

How to install FD

As the first step to use, of course, is to install it

Fd provides installation methods for each operating system platform, or can be installed directly from the source code (provided Rust environment).

2.1 One-click Installation

I’ll use BREW on my local Mac as an example

$ brew install fd
Copy the code

Brew can also be upgraded with one click

$ brew upgrade fd
Copy the code

For detailed installation methods for each platform, you can see the installation documentation here

2.2 Installing Source Code

$ git clone https://github.com/sharkdp/fd.git
$ cd fd
$ cargo install --path .
Copy the code

2.3 Viewing Help

After either installation, you can use -h or –help to get help. –help is not used. The difference is that you can use –help to get help if you forget a parameter

$fd -h fd 8.2.1 USAGE: fd [FLAGS/OPTIONS] [<pattern>] [<path>...]  FLAGS: - H - hidden search hidden files and directories - I, - no - ignore don't ignore. (git | fd) ignore files match - no - ignore - VCS don't ignore. -s gitignore file matching, --case-sensitive search (default: smart case) -i, --ignore-case Case insensitive search (default: Smart case) -f, --fixed-strings treats the pattern as a literal string -a, --absolute-path displays an absolute path instead of a relative path -l, --follow follows a symbolic link -p, --full-path searches for the full path (default: Only file-/ dirName) -0, --print0 separates results with null characters -h, --help prints help information -v, --version Prints version information OPTIONS: -d, --max-depth <depth> Sets the maximum search depth (default: none) -t, --type < fileType >... Filter by type: file (f), directory (D), symbolic link (L), executable (x), empty (e) -e, --extension <ext>... Filter by file name extension -x, --exec < CMD > execute the command -e, --exclude <pattern>... Exclude entries that match the given glob pattern --ignore-file <path>... Add custom ignore file -c in.gitignore format, --color <when> when to use color: Never, *auto*, always -j, --threads <num> Sets the number of threads used for searching and executing -s, --size <size>... Limit the results by file size. .Copy the code

3. Fd quick hands-on demonstration

In order to make after the demo has a unified understanding, I here has built a directory as the test directory of fd, I invented some files and directories to simulate the actual situation, including a hidden directory, after my presentation will be based on the root directory, long option if there is a short name and name, sample in short name, for example.

The catalog looks something like this:

.├ ─.hg │ ├─ HelloDjango.md │ ├─ HelloDjango.md │ ├─ HelloDjango.md │ ├─ Hello.java │ ├ ─ ─ World. Java │ └ ─ ─ dir2 │ ├ ─ ─ demo. Py │ ├ ─ ─ not. Py │ ├ ─ ─ dir3 │ │ ├ ─ ─ fd_demo. Rs │ │ └ ─ ─ fd_help. Rs │ └ ─ ─ SSS. Py ├ ─ ─ Hello_fd. Md ├ ─ ─ hello_java. Md ├ ─ ─ Java │ ├ ─ ─ Hello. Java │ └ ─ ─ World. Java ├ ─ ─ my_java. TXT ├ ─ ─ python │ ├ ─ ─ demo. Py │ ├ ─ ─ Not. Py │ └ ─ ─ SSS. Py └ ─ ─ rust │ ├ ─ ─ fd_demo. Rs │ └ ─ ─ fd_help. Rs ├ ─ ─ softdir3 - > dir1-name dir2 / dir3 └ ─ ─ SSS. Py - > dir1/dir2/sss.pyCopy the code

3.1 Simple Search

Fd will recursively search all files in the current directory, listing the file name containing the target content (the result is the relative path to the current directory).

$ fd Hello
dir1/Hello.java
java/Hello.java
Copy the code

3.2 Including Hidden Directories

Options -h or –hidden

$ fd -H Hello
.hg/HelloDjango.md
.hg/HelloRust.md
.hg/HelloVue.md
.hg/HelloZooKeeper.md
dir1/Hello.java
java/Hello.java
Copy the code

3.3 case

By default, fd matches smart case. If you search for content that contains uppercase, it will match case exactly, but if you search for content that contains lowercase, it will ignore case matching, so FD provides two additional options to strictly control case matching

The -i or –ignore-case option ignores case.

$ fd -i Hello
dir1/Hello.java
hello_fd.md
hello_java.md
java/Hello.java
Copy the code

The -s or –case-sensitive options strictly match case.

$ fd -s hello
hello_fd.md
hello_java.md
Copy the code

3.4 Returning the Absolute Path

Option -a or –absolute-path:

$ fd -a Hello
/Users/junjiexun/fd_test/dir1/Hello.java
/Users/junjiexun/fd_test/java/Hello.java
Copy the code

3.5 Return to the file list

The -l or –list-details option gives a similar effect to ls -l.

$ fd -l hello
-rw-r--r--  1 junjiexun  staff     0B  3  1 18:42 dir1/Hello.java
-rw-r--r--  1 junjiexun  staff     0B  3  1 18:37 hello_fd.md
-rw-r--r--  1 junjiexun  staff     0B  3  1 18:37 hello_java.md
-rw-r--r--  1 junjiexun  staff     0B  3  1 18:38 java/Hello.java
Copy the code

3.6 Search Contents Contain paths

The -p or –full-path options do not only search for file names, but also list the results in the directory that contain the target content.

Because the directory for this test is under /Users/junjiexun, this search results in all the files being searched.

$ fd xun Nothing return... $ fd -p xun dir1 dir1/Hello.java dir1/World.java dir1/dir2 ... (abbreviated)Copy the code

3.7 Includes files in.gitignore

I created a.gitignore file with only a *. Java file for demonstration purposes, and I need to initialize the current directory as a Git project with git init.

Without this parameter, you can see that the.java files in the result set are filtered.

$ fd java
hello_java.md
java
my_java.txt
Copy the code

After adding -i, the result includes.java ending files.

$ fd -I java
dir1/Hello.java
dir1/World.java
hello_java.md
java
java/Hello.java
java/World.java
my_java.txt
Copy the code

Gitignore and.git directories will be deleted for the rest of the demo.

These simple features cover half of your everyday search needs, so let’s take a look at the more advanced search options fd offers.

Advanced search options

4.1 according to the depth

Select -d or –max-depth

, the current path is considered depth 1, and the rs file under dir3 is depth 4.

$ fd rs
dir1/dir2/dir3/fd_demo.rs
dir1/dir2/dir3/fd_help.rs
rust/fd_demo.rs
rust/fd_help.rs
Copy the code
$ fd -d 3 rs
rust/fd_demo.rs
rust/fd_help.rs
Copy the code

4.2 By File Type

With -t or –type

, fd provides the following filetype options:

  • F: the file
  • D: the directory
  • L: symlink
  • X: the executable
  • E: the empty
  • S: the socket
  • P: pipe
$ fd -t l
softdir3
sss.py
Copy the code
$ fd -t d
dir1
dir1/dir2
dir1/dir2/dir3
java
python
rust
Copy the code

I’ve given all py files executable permissions

$ fd -t x
python/demo.py
python/demo1.py
python/sss.py
Copy the code

4.3 By Extension

Option -e or –extension

$ fd -e md
hello_fd.md
hello_java.md
Copy the code

4.4 eliminate

The -e or –exclude option supports wildcards and excludes all results containing the letter S.

$ fd -E '*s*'
dir1
dir1/Hello.java
dir1/World.java
dir1/dir2
dir1/dir2/demo.py
dir1/dir2/demo1.py
dir1/dir2/dir3
hello_fd.md
hello_java.md
java
java/Hello.java
java/World.java
my_java.txt
python
python/demo.py
python/demo1.py
Copy the code

You can see that all rust, RS, SSS, and Soft are not present in the result set.

4.5 By Owner

Option -o or –owner

$ fd -l -o junjiexun drwxr-xr-x 5 junjiexun staff 160B 3 1 18:42 dir1 -rw-r--r-- 1 junjiexun staff 0B 3 1 18:42 dir1/Hello.java -rw-r--r-- 1 junjiexun staff 0B 3 1 18:42 dir1/World.java drwxr-xr-x 6 junjiexun staff 192B 3 1 18:42 dir1/dir2 -rw-r--r-- 1 junjiexun staff 0B 3 1 18:42 dir1/dir2/demo.py ... (abbreviated)Copy the code

Fd-l-o Junjiexun: Staff can also achieve the same effect, but FD does not support separate search groups, nor does it support wildcards. If you have an idea, you can issue it to him

4.6 Combining Commands

Fd provides -x or –exec < CMD >, -x or — exec-Batch < CMD > for further processing of the search result set

Find anything that matches Java and delete it! (For demonstration only, rm-RF should be used with caution)

$ fd java -X rm -rf
Copy the code

Find all py and open it via vim

$ fd py -X vim
Copy the code

You can also use such as unzip, ls, convert, and so on other commonly used commands, also can use the * Unix directly grammar | pipeline operator grammar further processing.

4.7 Regular Expressions

In order to search for the contents of a file, I have shown that Hello, Java, py are full text. In fact, fd supports regular expression search by default, but the regular expression needs to be wrapped in single quotation marks (‘).

$ fd '^s.*'
dir1/dir2/sss.py
python/sss.py
softdir3
sss.py
Copy the code

If you don’t want to use regular expressions and want to make wildcard matching easier, you can do the same with the -g or –glob options.

$ fd -g 's*'
dir1/dir2/sss.py
python/sss.py
softdir3
sss.py
Copy the code

Most of the options above can be used at the same time, so I’m not going to continue the demonstration here for lack of space.

Five, the summary

Fd is a simple and friendly command line file search tool, and its open source properties are excellent as a learning object for Rust source code.

Open Source Projects: github.com/HelloGitHub…

If you are also interested in open source projects and want your article or project to be liked by more people, click on Open Source Projects to share interesting, entry-level open source projects.