Fundamentals 9 min read

Master Linux File Search: Powerful find & grep Techniques for Developers

This guide explains how to efficiently locate executables, files, and content on Linux using commands like whereis, which, locate, find, and grep, with practical examples for filtering by name, time, size, depth, and pattern matching.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Master Linux File Search: Powerful find & grep Techniques for Developers

Searching is a daily activity for developers, whether looking for files, information, or errors. Linux command line offers several powerful tools to perform these searches quickly.

Executable and Program Search

whereis program_name

searches standard installation directories (e.g., /bin, /sbin, /usr/bin, /usr/lib, /usr/local/man) for binaries, source, and documentation. which program_name shows the path of the program found in the current PATH environment, helping you verify which executable will run.

Example: vim `which sp_pheatmap.sh` opens the script directly; cp `which sp_pheatmap.sh` . copies it to the current directory without needing the full path.

If which bwa returns /usr/bin/which: no bwa in (/home/usr/bin:/bin), the program is not in the PATH and cannot be invoked by name.

Quick File Location with locate

locate

finds files using a pre‑built index created by updatedb. The index is typically refreshed daily, so newly created files may not appear until the next run. Administrators can manually run updatedb to refresh the index.

Multi‑Condition File Search with find

Basic name search: find / -name bwa scans the entire filesystem for files named bwa.

Suppress permission errors: find / -name bwa 2>/dev/null.

Time‑based search (files modified within the last hour): find . -name *.log -mmin -60.

Bookmarking new logs: after reviewing logs, create an empty file with touch check, then later run find . -name *.log -newer check to list only logs newer than the bookmark.

Filter out empty logs: find . -name *.log -newer check -size +0.

Search by type and size: find . -type f -size +100G lists files larger than 100 GB.

Limit search depth: find . -maxdepth 2 -name *.log restricts results to two directory levels.

Exclude patterns: find . -not -name *.log shows files not ending with .log.

Content Search with grep

Find files containing a word: find . -name *.log -exec grep -l 'Error' {} \; or find . -name *.log | xargs grep -l 'Error'.

Show matching lines with context: grep -A 5 -B 1 'Bioinfo' ehbio.log displays one line before and five lines after each match.

Count matches: grep -c 'Bioinfo' ehbio.log (case‑insensitive with -i).

Extract FASTA sequences for IDs: grep -A 1 -Fw -f id ehbio.fa | grep -v -- '--'. Options explained: -f id reads patterns from a file, -F treats them as fixed strings, -w matches whole words.

Remove empty lines: grep -v '^$' ehbio.fa >ehbio.clean.fa.

Regex power: basic regex is default, -E enables extended regex, and -P uses Perl‑compatible regex.

Summary

Linux command‑line tools such as whereis, which, locate, find, and grep form essential skills for bioinformatics and general development, enabling fast file discovery, content inspection, and automated log handling.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Shell scriptingbioinformaticsGrepfind
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.