bash-tutorial/tutorial/search_files.org

2.3 KiB

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

find ./ -name "gitea*"

./imagens/gitea-sm.png

Search for all the files ending in '.png'

find ./ -name "*.png"

./imagens/gitea-sm.png ./files/plot.png

Search for empty files in 'home'

find /home -type f -empty

'find' can also be used with grep to match patterns:

cd ~/Documentos/dat/intro_r/answers
find . | grep -E '.js|.png'
./plot.png
./vizjs.js
./Rplot.png
./bipartiteD3Script.js
./demo1.js

To find file with different name patters, 'find' can also be used as follows:

 find . -type f \( -name "*.org" -o -name "*.md" \)
./README.org
./bash_tutorial.org

Matching patterns

Print lines within files that match patterns

grep

Example: searching for all entries that have "root"

grep 'root' /etc/passwd
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

grep -rni IFELSE *
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