<< back to Guides

Search Commands Guide: Searching Files, Searching Inside Files, and Comparing Files

1. Searching for Files

find

The find command is used to search for files and directories in a directory hierarchy.

Basic syntax:

find [path] [expression]

Options

Option Description Example
-name [pattern] Search for files matching the pattern (case-sensitive) find . -name "*.txt"
-iname [pattern] Search for files matching the pattern (case-insensitive) find . -iname "*.txt"
-type [type] Search for a specific file type (e.g., f for file, d for directory) find . -type f
-size [size] Search for files of a specific size find . -size +100M
-mtime [n] Search for files modified in the last n days find . -mtime -7
-atime [n] Search for files accessed in the last n days find . -atime -7
-ctime [n] Search for files changed in the last n days find . -ctime -7
-user [username] Search for files owned by a specific user find . -user username
-group [groupname] Search for files owned by a specific group find . -group groupname
-perm [mode] Search for files with a specific permission mode find . -perm 644
-exec [command] Execute a command on the files found find . -name "*.txt" -exec cat {} \;
-print Print the results (default action) find . -name "*.txt" -print
-delete Delete the files found (use with caution) find . -name "*.log" -delete
-empty Search for empty files or directories find . -empty
-maxdepth [n] Limit the search to n levels of directories find . -maxdepth 2 -name "*.txt"
-mindepth [n] Limit the search to files at least n levels deep find . -mindepth 2 -name "*.txt"

Examples:

find . -type f
find /home/user/ -name "file.txt"
find /home/user/ -mtime -7
find /home/user/ -size +1G

locate

The locate command uses a pre-built index of files, making it faster than find, but it requires updating the index periodically.

Basic syntax:

locate [filename]

Options

Option Description Example
-i Perform case-insensitive search locate -i "*.txt"
-r [pattern] Search using a regular expression locate -r "\.jpg$"
-c Show only the count of matches locate -c "file.txt"
-n [number] Limit the number of results shown locate -n 10 "file"
-l [number] Limit the number of results shown (alternative to -n) locate -l 5 "file"
-e Only show results for files that actually exist (useful after updatedb) locate -e "*.log"
-S [number] Show the size of each file found locate -S "*.txt"
--help Show help information about locate command locate --help
--version Display the version of locate locate --version

Example:

locate file.txt
sudo updatedb

which

The which command shows the path of the executable that would have been executed in the current shell environment.

Basic syntax:

which [command]

Options

Option Description Example
-a Show all matches of the command (not just the first one found) which -a python
--version Display the version of the which command which --version
-h Show help information which -h

Example:

which python3

2. Searching Inside Files

grep

The grep command searches for patterns inside files. It's one of the most commonly used commands for searching inside files.

Basic syntax:

grep [options] "pattern" [file]

Options

Option Description Example
-i Perform a case-insensitive search grep -i "pattern" file.txt
-v Invert the match (show lines that do not match the pattern) grep -v "pattern" file.txt
-r or -R Recursively search directories for the pattern grep -r "pattern" /path/to/directory
-l Only show the names of files that contain the pattern grep -l "pattern" *.txt
-n Show line numbers along with matching lines grep -n "pattern" file.txt
-c Show the count of matching lines grep -c "pattern" file.txt
-H Show the filename in the output (useful when searching multiple files) grep -H "pattern" *.txt
-h Suppress the filename in the output (useful when searching one file) grep -h "pattern" file.txt
-o Only show the matched portion of the line grep -o "pattern" file.txt
-E Use extended regular expressions `grep -E "pattern1
-F Treat the pattern as a fixed string (disables regular expressions) grep -F "literal_string" file.txt
-P Use Perl-compatible regular expressions grep -P "\d+" file.txt
-q Quiet mode (do not output anything, just return the status) grep -q "pattern" file.txt
--color Highlight matching text in color grep --color "pattern" file.txt
-w Match the whole word grep -w "pattern" file.txt
-x Match the whole line grep -x "pattern" file.txt

Examples:

grep "error" logfile.txt
grep -r "error" /var/log/
grep -i "error" logfile.txt
grep -n "error" logfile.txt
grep -c "error" logfile.txt

ack

ack is an alternative to grep, designed for searching code, and is optimized for speed.

Basic syntax:

ack [pattern] [path]

Options

Option Description Example
-i Perform a case-insensitive search ack -i "pattern"
-v Invert the match (show lines that do not match the pattern) ack -v "pattern"
-r Recursively search directories (default behavior) ack -r "pattern"
-l Only show the names of files that contain the pattern ack -l "pattern"
-c Show the count of matching lines in each file ack -c "pattern"
-n Show line numbers of matching lines ack -n "pattern"
-w Match the whole word (pattern must match a complete word) ack -w "pattern"
-x Match the whole line (pattern must match the entire line) ack -x "pattern"
-g Search only files that match the given pattern (without searching the content) ack -g "*.js"
--type=[type] Restrict the search to files of a specific type (e.g., perl, python, etc.) ack --type=python "pattern"
--ignore-case Equivalent to -i, for case-insensitive search ack --ignore-case "pattern"
--files-with-matches Only show files with matching content, similar to -l ack --files-with-matches "pattern"
--no-filename Suppress filename in the output when searching multiple files ack --no-filename "pattern"
--color Highlight matching text in color ack --color "pattern"
--max-count Limit the number of matching lines displayed ack --max-count=5 "pattern"

Example:

ack "function" --js

ag (The Silver Searcher)

ag is another alternative to grep, known for its speed, especially when searching through large directories.

Basic syntax:

ag [pattern] [path]

Options

Option Description Example
-i Perform a case-insensitive search ag -i "pattern"
-v Invert the match (show lines that do not match the pattern) ag -v "pattern"
-r Recursively search directories (default behavior) ag -r "pattern"
-l Only show the names of files that contain the pattern ag -l "pattern"
-c Show the count of matching lines in each file ag -c "pattern"
-n Show line numbers of matching lines ag -n "pattern"
-w Match the whole word (pattern must match a complete word) ag -w "pattern"
-x Match the whole line (pattern must match the entire line) ag -x "pattern"
-g Search only files that match the given pattern (without searching the content) ag -g "*.js"
-s Show the matching file’s path relative to the current directory ag -s "pattern"
-A [num] Show num lines after each match ag -A 2 "pattern"
-B [num] Show num lines before each match ag -B 2 "pattern"
-C [num] Show num lines around each match (before and after) ag -C 2 "pattern"
--ignore-case Equivalent to -i, for case-insensitive search ag --ignore-case "pattern"
--files-with-matches Only show files with matching content, similar to -l ag --files-with-matches "pattern"
--color Highlight matching text in color ag --color "pattern"
--stats Display statistics on the search results (e.g., number of matches) ag --stats "pattern"
--max-count Limit the number of matching lines displayed ag --max-count=5 "pattern"

Example:

ag "TODO"

sed

sed is a stream editor, but it can also be used for searching and replacing text in files.

Basic syntax:

sed -n '/pattern/p' [file]

Options

Option Description Example
-e Allows you to add multiple expressions sed -e 's/old/new/g' file.txt
-i Edit the file in-place (modify the original file) sed -i 's/old/new/g' file.txt
-n Suppress automatic printing of lines, only print lines with p command sed -n 's/old/new/p' file.txt
-r Use extended regular expressions (similar to -E in grep) `sed -r 's/old
-E Enable extended regular expressions `sed -E 's/old
-f Read commands from a file instead of the command line sed -f commands.sed file.txt
--help Display help information sed --help
--version Display the version information sed --version
-i[SUFFIX] Edit the file in-place and create a backup with the given suffix sed -i.bak 's/old/new/g' file.txt
-g Apply the substitution to all occurrences in the line (default is the first match) sed 's/old/new/g' file.txt
-l[width] Specify the line length when printing with -n option sed -l 80 's/old/new/' file.txt

Example:

sed -n '/error/p' logfile.txt

3. Comparing Files

diff

The diff command compares files line by line and shows the differences between them.

Basic syntax:

diff [file1] [file2]

Options

Option Description Example
-u Unified format (shows a few lines of context around the differences) diff -u file1.txt file2.txt
-c Context format (shows more context around differences) diff -c file1.txt file2.txt
-i Ignore case differences diff -i file1.txt file2.txt
-w Ignore all white space differences diff -w file1.txt file2.txt
-b Ignore changes in the amount of white space diff -b file1.txt file2.txt
-q Suppress output of differences, only show whether files differ diff -q file1.txt file2.txt
-r Recursively compare directories diff -r dir1 dir2
-N Treat absent files as empty diff -N file1.txt file2.txt
-y Side-by-side comparison diff -y file1.txt file2.txt
--strip-trailing-cr Remove trailing carriage return characters (useful for Windows files) diff --strip-trailing-cr file1.txt file2.txt
-u [num] Limit the number of lines of context shown diff -u 3 file1.txt file2.txt
-p Show which C function a change occurred in (useful for code) diff -p file1.c file2.c
--side-by-side Display output in two columns for easier comparison diff --side-by-side file1.txt file2.txt

Examples:

diff file1.txt file2.txt
diff -w file1.txt file2.txt
diff -r dir1/ dir2/

cmp

The cmp command compares two files byte by byte.

Basic syntax:

cmp [file1] [file2]

Options

Option Description Example
-l Print byte and line numbers of all differing bytes cmp -l file1.txt file2.txt
-b Print all differing bytes, including the ones with the same value cmp -b file1.txt file2.txt
-i Ignore the first n bytes of both files cmp -i 10 file1.txt file2.txt
-n Compare only the first n bytes cmp -n 100 file1.txt file2.txt
-s Suppress output (only return exit status) cmp -s file1.txt file2.txt
-v Invert the sense of the comparison (useful with -l) cmp -v file1.txt file2.txt
--help Display help information cmp --help
--version Display version information cmp --version

Example:

cmp file1.bin file2.bin

comm

The comm command compares two sorted files line by line and outputs the lines that are unique to each file or common to both.

Basic syntax:

comm [file1] [file2]

Options

Option Description Example
-1 Suppress lines unique to the first file comm -1 file1.txt file2.txt
-2 Suppress lines unique to the second file comm -2 file1.txt file2.txt
-3 Suppress lines that are common to both files comm -3 file1.txt file2.txt
-i Ignore case differences comm -i file1.txt file2.txt
-u Use unadorned output (suppress columns of output) comm -u file1.txt file2.txt
--help Display help information comm --help
--version Display version information comm --version

Example:

comm file1.txt file2.txt

sdiff

The sdiff command displays the side-by-side comparison of two files.

Basic syntax:

sdiff [file1] [file2]

Example:

sdiff file1.txt file2.txt

Summary of Key Commands

Command Description Example
find Search for files and directories find /home/user/ -name "*.txt"
locate Search for files using a prebuilt index locate file.txt
grep Search for patterns inside files grep "error" logfile.txt
ack Search for patterns inside code files ack "function" --js
ag Fast file pattern search tool ag "TODO"
sed Search and edit files using regular expressions sed -n '/pattern/p' file.txt
diff Compare files line by line diff file1.txt file2.txt
cmp Compare files byte by byte cmp file1.bin file2.bin
comm Compare two sorted files comm file1.txt file2.txt
sdiff Side-by-side comparison of files sdiff file1.txt file2.txt
<< back to Guides