bash-tutorial/tutorial/search_files.org

103 lines
2.3 KiB
Org Mode

#+options: toc:nil num:nil author:nil
* Searching
** Search for files
'find' can be used with multiple arguments such as:
* -name: search for name
* -iname: search for case-insensitive name
* -type: type of file
* f = file
* d = directory
Search within all the directory and subdirectories by name
#+begin_src bash
find ./ -name "gitea*"
#+end_src
#+RESULTS:
./imagens/gitea-sm.png
Search for all the files ending in '.png'
#+begin_src bash
find ./ -name "*.png"
#+end_src
#+RESULTS:
./imagens/gitea-sm.png
./files/plot.png
Search for empty files in 'home'
#+begin_src bash
find /home -type f -empty
#+end_src
'find' can also be used with grep to match patterns:
#+begin_src bash
cd ~/Documentos/dat/intro_r/answers
find . | grep -E '.js|.png'
#+end_src
#+RESULTS:
| ./plot.png |
| ./vizjs.js |
| ./Rplot.png |
| ./bipartiteD3Script.js |
| ./demo1.js |
To find file with different name patters, 'find' can also be used as follows:
#+begin_src bash
find . -type f \( -name "*.org" -o -name "*.md" \)
#+end_src
#+RESULTS:
| ./README.org |
| ./bash_tutorial.org |
** Matching patterns
Print lines within files that match patterns
: grep
Example: searching for all entries that have "root"
#+begin_src bash
grep 'root' /etc/passwd
#+end_src
#+RESULTS:
: root:x:0:0::/root:/bin/bash
Additional arguments can be used with ~grep~:
* r: recursive search (within directories)
* n: print line number
* i: case-insensitive search
* '*' Wildcards to search in all the directories
Search within files in the actual directory with wildcards
#+begin_src bash
grep -rni IFELSE *
#+end_src
#+RESULTS:
|answers/repl_values.R:137: mutate(Status = ifelse(Status == "almost peak" &|
|answers/categ_colum.R:40:df$lead_likely <- ifelse(grepl("UNCOVERED CURB | SERVICE", df$remarks), "non-lead", "unknown")|
|docs/vectorization.org:354:(counting number of repeated value), and =ifelse= (vectorized if...else|
'grep' can be used also to search for files in directories as shown in the anterior
example using grep with extended regular expressions ("-E") and 'find'
: find . | grep -E '.js|.png'
In this example, 'find' will be passed to 'grep' and any pattern matching
will be printed