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

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.
主站蜘蛛池模板: 睢宁县| 乌什县| 安国市| 宜阳县| 咸阳市| 邵阳市| 顺昌县| 瑞金市| 宁武县| 久治县| 南昌市| 井陉县| 凤庆县| 巴彦县| 鹿泉市| 伊春市| 林芝县| 讷河市| 罗江县| 喀喇| 洛阳市| 武乡县| 南通市| 加查县| 偏关县| 卢湾区| 静安区| 武冈市| 团风县| 旬邑县| 新竹县| 黄陵县| 加查县| 平远县| 清涧县| 克拉玛依市| 深州市| 高邑县| 新源县| 博湖县| 鹤庆县|