官术网_书友最值得收藏!

  • Bash Cookbook
  • Ron Brash Ganesh Naik
  • 661字
  • 2021-07-23 19:17:42

How it works...

Repeat after me—"locate is simple and needs to be updated, find works when in a bind, but powerful and cryptic and can break things." Lets continue and begin with the explanations:

  1. As mentioned previously, the locate command is a relatively simple search tool that uses a database as a backend, which contains an indexed list of all of the files for quick and efficient searches. Locate is not real-time unlike the find command, which searches everything as it exists at the time of execution (depending on the parameters provided to find). Locating stdio.h will produce several results depending on your system. However, when we run locate again, it does not know or contain any information regarding the /usr/filethatlocatedoesntknow.txt and /usr/filethatlocatedoesntknow2.txt files. Running updatedb will re-index the files and then using the locate command will return the appropriate results. Notice that locate works with partial names or full path matching:
$ locate stdio.h
/usr/include/stdio.h
/usr/include/c++/5/tr1/stdio.h
/usr/include/x86_64-linux-gnu/bits/stdio.h
/usr/include/x86_64-linux-gnu/unicode/ustdio.h
/usr/lib/x86_64-linux-gnu/perl/5.22.1/CORE/nostdio.h
/usr/share/man/man7/stdio.h.7posix.gz
$ sudo touch /usr/filethatlocatedoesntknow.txt /usr/filethatlocatedoesntknow2.txt
$ sudo sh -c 'echo "My dear Watson ol\'boy" > /usr/filethatlocatedoesntknow.txt'
$ locate filethatlocatedoes
$ sudo updatedb
$ locate filethatlocatedoes
/usr/filethatlocatedoesntknow.txt
/usr/filethatlocatedoesntknow2.txt
  1. In the second step, we are introduced to some of the amazing functionality provided by the find command.
Again, be aware that using find for operations such as deletion can break your system if not handled appropriately or if the input isn't carefully monitored and filtered.
  1. At a minimum, the find command is executed this way: $ find ${START_SEARCH_HERE} ${OPTIONAL_PARAMETERS ...}. In the first use of the find command, we begin searching within our user's home directory (${HOME} environment variable), and then use a wild card to look for hidden files that begin with a ..Finally, we use -ls to create a file listing. This is not by accident as you may have observed; you can create files that are absent upon first inspection in the GUI's file explorer (especially in your user's home directory) or on the console (for example, unless you use the ls command with the -a flag). In the next command, we use find -type d to search for a directory named .git. Then, we search for files that match either *.sh or *.txt using a special notation for find: -type f \( -name "*.sh" -o -name "*.txt" \). Notice the forward slash \ and then the parenthesis (. We can then specify multiple name matching arguments using -o -name "string".

 

  1. In the third step, we use find to search for subdirectories using -type d that are often present inside of cloned or exported git-related directories. We can chain the find commands together using this format: $ cmd 1 && cmd2 && cmd3 && .... This guarantees that if the proceeding command evaluates to true, then the next will execute and so on. Then, we introduce the -exec flag, which is used to execute another command once a match has been found. In this case, we search for all files and then use grep immediately to search within the file. Notice the {} + at the end of the grep. This is because {} will be replaced with find's returned results. The + character delimits the end of the exec command, and appends the results so that rm -rf will be executed less times than the total number of files found/matched.
  2. In the final step, we delete files using two methods. The first method using the -delete flag may not be available on all distributions or implementations of find, but upon match, it will delete the file. It is more efficient than executing the sub process rm on large numbers of files. Secondly, using -exec rm -rf {} \;, we can delete files found easily and in a portable way. However, there is a difference between \; and + and the difference is that in the \; version, rm -rf is executed for each file found/matched. Be careful with this command as it is not interactive.
主站蜘蛛池模板: 鄂州市| 台前县| 梓潼县| 仁寿县| 河间市| 自治县| 子长县| 阿克陶县| 威信县| 平凉市| 岑溪市| 辽阳县| 介休市| 巫溪县| 高尔夫| 寻甸| 南丰县| 舞阳县| 望城县| 竹北市| 苍南县| 天水市| 海丰县| 共和县| 大港区| 铜川市| 蓝山县| 乌什县| 江油市| 隆德县| 太白县| 申扎县| 收藏| 溆浦县| 南华县| 正镶白旗| 沂南县| 江城| 黄龙县| 商水县| 财经|