- 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:
- 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
- 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.
- 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".
- 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.
- 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.
推薦閱讀
- AngularJS Testing Cookbook
- ASP.NET Core Essentials
- Django開發從入門到實踐
- C語言程序設計立體化案例教程
- GameMaker Programming By Example
- Learning Salesforce Einstein
- 精通Python自動化編程
- Developing SSRS Reports for Dynamics AX
- Building Serverless Architectures
- Raspberry Pi Robotic Projects(Third Edition)
- JavaScript+jQuery網頁特效設計任務驅動教程
- Elasticsearch Essentials
- Tableau Desktop可視化高級應用
- Hacking Android
- Mastering JavaScript Promises