From cc086b5b6778eca2f7a5b85161563b93cc225b06 Mon Sep 17 00:00:00 2001 From: tactonbishop Date: Wed, 25 May 2022 20:30:04 +0100 Subject: [PATCH] Last Sync: 2022-05-25 20:30:04 --- Programming_Languages/Shell_Scripting/Find.md | 39 ++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/Programming_Languages/Shell_Scripting/Find.md b/Programming_Languages/Shell_Scripting/Find.md index 667fa6b..31399d7 100644 --- a/Programming_Languages/Shell_Scripting/Find.md +++ b/Programming_Languages/Shell_Scripting/Find.md @@ -39,7 +39,7 @@ Within a specified directory: $ find i3 -type f ``` -### Filename +#### Filename This is the most frequent use case: filter files by name with globbing. ```bash @@ -56,4 +56,41 @@ The same, but case insensitive: `iname` ```bash $ find -iname "*.JS" +``` + +#### Path +As above but this time includes directory names in the match. `ipath` is the case-insensitive version. + +```bash +$ find -path "utils*" +utils.js +utils/do-something.js +``` + +### Operators +We can combine `find` commands by using logical operators: `-and`, `-or`, `-not`. For example: + +```bash +$ find -not -name "*.js" -type f +./app/index.html +./app/style.css +./dist/index.html +./dist/style.css +``` + +## Actions +Using the `exec` keyword we can run a program against the files that are returned from `find`. + +In this syntax we use `{}` as a placeholder for the path of the file that is matched. We use `;` (escaped) to indicate the end of the operation. + +### Examples + +This script deletes the files that match the filter criteria: + +```bash +$ find -name "*.js" -exec rm {} \; +``` +This script finds all the files with the substring 'config' in their name and writes their file size to a file. +```bash +find -name '*config*' -exec wc -c {} \; > config-sizes ``` \ No newline at end of file