Hello, I’m Liang Xu.
We know that under Windows, there is a very useful tool called Everything. It can find the files/directories you want in a very short time, as shown below:
Linux also has artifacts similar to everything: Locate, Catfish, Tracker, and so on. These tools are also very powerful, here is not a demonstration, interested partners can go to experience.
However, in fact, we can also realize a lightweight everything by ourselves, which can not only meet our own needs, but also improve our technology, and show off in front of programmers
Without further ado, let’s go straight to the script:
#! /bin/sh # lazy find # GNU All-Permissive License # Copying and distribution of this file, with or without modification, # are permitted in any medium without royalty provided the copyright # notice and this notice are preserved. This file is offered as-is, # without any warranty. ## help function function helpu { echo " " echo "Fuzzy search for filename." echo "$0 [--match-case|--path] filename" echo " " exit } ## set variables MATCH="-iname" SEARCH="." ## parse options while [ True ]; do if [ "$1" = "--help" -o "$1" = "-h" ]; then helpu elif [ "$1" = "--match-case" -o "$1" = "-m" ]; then MATCH="-name" shift 1 elif [ "$1" = "--path" -o "$1" = "-p" ]; then SEARCH="${2}" shift 2 else break fi done ## sanitize input filenames ## create array, retain spaces ARG=( "${@}" ) set -e ## catch obvious input error if [ "X$ARG" = "X" ]; then helpu fi ## perform search for query in ${ARG[*]}; do /usr/bin/find "${SEARCH}" "${MATCH}" "*${ARG}*" doneCopy the code
This script is not the original work of Liang Xu, but the work of a young brother abroad. The script is still very concise and uses some common Shell programming techniques, so studying it will improve your Shell programming skills.
So how does this script work?
2020 selected Ali/Tencent and other front-line big factory interview, resume, advanced, e-book public number “Liang Xu Linux” background reply “information” free access
First, create a script file.
For example, the script file is lazy_find.sh.
$ vim lazy_find.shCopy the code
Next, we copy the script contents in and press :wq to save and exit.
Next, we use the chmod command to grant the script executable permissions:
$ chmod +x lazy_find.shCopy the code
Second, use scripts to search for files.
The most basic use is to follow the name of the script directly with the name of the file to search for. You don’t need to follow the full name, just part of it, for example:
$ ./lazy_find.sh scrCopy the code
The running results are as follows:
As you can see, this script can not only search for files under the current path, but also search for subdirectories and grandson directories under the current directory, which is a recursive search function.
The above usage is case insensitive, that is, it can match both SCR and SCR. So, if you want to be case-sensitive, what do you do?
Just add the -m option.
$ ./lazy_find.sh -m scrCopy the code
The running results are as follows:
As you can see, the Scr is no longer matched.
If we do not want to search the current directory, but want to search the specified directory, we simply add –path and specify the path we want to search.
For example, if we wanted to search for files/directories containing the hello keyword in our home directory, we could run the command:
$ ./lazy_find.sh --path ~ helloCopy the code
The running results are as follows:
2020 selected Ali/Tencent and other front-line big factory interview, resume, advanced, e-book public number “Liang Xu Linux” background reply “information” free access
Third, customize the command to invoke the script
In the above method, we need to run the script file manually, which is definitely very inconvenient. We can greatly improve efficiency by customizing a command, such as LF, that will automatically invoke the script when the command is run.
For those of you with Linux roots, we can use the alias command to do this. In order to be able to use lf command even after system restart, we changed it directly in.bashrc.
$ vim ~/.bashrcCopy the code
At the end of the.bashrc file, add a line like this:
Alias lf=~/bin/lazy_find.sh # alias lf=~/bin/lazy_find.shCopy the code
After adding the statement, press :wq to save and exit. Then, enable our modification:
$ . ~/.bashrcCopy the code
OK, we are done, we can use this command anywhere.
summary
In this paper, we use a script to realize some basic functions of everything. By learning this script, we can improve our Shell programming ability and learn how to customize commands to call scripts written by ourselves.
This article is more basic, suitable for small white entry.
All you see is true love. Why don’t you like it before you leave? Your “sanlian” is the biggest motivation for liang Xu’s continuous creation!
- Pay attention to the original public number “good Xu Linux”, the first time to get the latest Linux dry goods!
- Public number background reply [information] [interview] [resume] to get selected first-line big factory interview, self-improvement, resume and other information.
- Follow my blog: lxLinux.net