Skip to content

Instantly share code, notes, and snippets.

@mdavy86
Last active October 6, 2023 15:38
Show Gist options
  • Save mdavy86/d443c0e066f45530430a to your computer and use it in GitHub Desktop.
Save mdavy86/d443c0e066f45530430a to your computer and use it in GitHub Desktop.
Finding stuff in bash

Finding files containing specific content of interest

grep and find are two utilities for searching, grep searches for content within files, find searches for file names themselves.

Using grep

Defining some additional options in grep allows searching using colour where filenames and row positions are highlighted.

alias grepc='grep --color=auto -iRnH'
alias grepe='egrep --color=auto -iRnH'

The drawback is it cannot search for specific file extensions

grepc [PATTERN] *

works but

grepc [PATTERN] *.sh

does not work recursively

Using find and grep recursively

Using find with pipe to xargs to run grep from above.

Example 1

Search and syntax highlight the longest poly-A tail in a fastq file

cat [FILE] | perl -ne '$.%4 ==2 && print $_' | egrep --color=auto -iRnH "A{14,}" -

Example 2

We want to replace the [PATTERN] with text of interest, quoted patterns can contain metacharacters. for all files of the form *.sh

find . -name "*.sh" -print | xargs -n1 -I{} grep --color=auto -iRnH [PATTERN] {} 2> /dev/null

This does allow files of certain extensions to be recursively searched for content.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment