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

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.
主站蜘蛛池模板: 盐源县| 佛教| 永登县| 吉首市| 丁青县| 积石山| 吴旗县| 阿瓦提县| 大名县| 长岭县| 北安市| 哈巴河县| 宕昌县| 福海县| 瓮安县| 沈丘县| 榆林市| 隆回县| 江阴市| 和硕县| 宾川县| 兰考县| 海宁市| 乌恰县| 武平县| 扶风县| 乐至县| 黑龙江省| 伽师县| 海林市| 息烽县| 胶南市| 金秀| 娄底市| 曲靖市| 偏关县| 都昌县| 许昌市| 自治县| 连南| 襄汾县|