Unix / Linux Commands #5: grep and find, Search Text and Files Like a Pro
Code & Test Automation
0:00 / 0:00
Unix / Linux Commands #5: grep and find, Search Text and Files Like a Pro
5 просмотров · 1 дн. назад
Code & Test Automation
22 подписчика
5 просмотров · 1 дн. назад
Code & Test Automation | / @codetestautomation
Search like a pro in a real bash terminal with real output: grep to find text inside files and find to locate files by name, type, size and age. Covers grep -i, -n, -w, -v, -c, -l, -L, -E, context with -A -B -C, recursive grep -rn with --include and --exclude-dir, and find -name, -iname, -type, -maxdepth, -size, -mtime and -exec, plus combining find with grep and the macOS / BSD differences.
In this video:
grep basics, case sensitivity, exit status 0, 1 and 2
grep -n, context lines, -w, --color, -v, -c, -E with alternatives, ^ and $
grep on several files, -l and -L, grep -r and -rn, --include, --exclude-dir, Ctrl+R history search
find -name with quotes, -iname, -type f and d, -maxdepth
find -size, -exec, -mtime and -newermt, find + grep, safe use of -delete
macOS / BSD grep and find differences
Video 5 of 10 in the Unix / Linux Commands series, for freshers and experienced engineers on Linux, macOS and most Unix systems: navigation, file operations, viewing and searching files, permissions, pipes and redirection, processes, disk tools and daily workflows. Almost every minute is a real terminal session with real output.
CHAPTERS
0:00 Introduction
1:07 grep: search inside a file
1:27 grep -c: count matches
1:49 grep is case sensitive
2:14 grep -i: ignore case
2:32 grep -n: line numbers
2:50 grep with context
3:13 grep -w: whole words
3:36 Highlighting matches
3:56 grep -v: invert the match
4:14 grep -v with -c
4:34 Several patterns with -E
5:00 Anchors: start and end of line
5:24 grep on several files
5:43 grep -l: list files only
6:02 When the file does not exist
6:28 grep -r: search a whole folder
6:49 grep -rn: path and line number
7:06 Limit the search to some files
7:29 TODO or FIXME, and -rl
7:48 History: find that grep again
8:06 macOS and BSD grep
8:29 find: files by name
8:53 Quote the pattern
9:19 find -type f and -type d
9:41 Where to start
10:06 find -size: big files
10:32 find -exec: act on each result
10:55 find -mtime: by age
11:36 Combining find and grep
12:18 Delete safely with find
12:39 macOS and BSD find
13:03 grep or find: which one?
13:24 Recap
14:45 Next episode
HOW THE DEMOS WERE MADE
Everything between the title card and the recap is a live screen recording of a real bash 5.2 shell in xfce4-terminal on Debian Linux (GNU coreutils 9.7). Every command was typed at a human pace and every line of output is the real output; nothing was edited in afterwards. The shell runs in a private practice sandbox: an unprivileged Linux user, mount and PID namespace whose home folder, /home/learner, holds only the practice files created by the mklab.sh script in the project zip, and whose host name is unix-lab. No sudo was used, and no command touched anything outside the practice folder. This minimal machine has no man-db, so the man command here is a small wrapper that shows the official GNU manual pages with mandoc and the real less pager. Keys pressed in the terminal appear in on-screen badges with the macOS Terminal keys and the Linux keys. When macOS or BSD tools use different options, the macOS form is shown as TYPED ONLY: typed on screen, never run, and cancelled with Ctrl+C, so no output is ever invented.
CORRECTIONS TO THE COURSE NOTES
grep -r: GNU grep searches the current folder when no path is given, but BSD grep on macOS warns about reading standard input, so always pass the folder (grep -r TODO .).
find . -size +10M: GNU find rounds sizes up to whole units (MiB here), so -size -1M matches only empty files; use -size -1024k for 'smaller than 1 MiB'.
find -mtime counts whole 24-hour days and drops the fraction: -mtime -7 is 'less than 7 days ago', and -mtime +30 means at least 31 full days old. GNU find has -newermt DATE for an exact date.
BSD find (macOS) requires the starting folder: find -name x fails there; always write find . -name x. Quote the pattern ('*.txt') so the shell does not expand it first.
egrep is obsolete (GNU grep 3.8+ warns); use grep -E 'a|b'. No output from grep is not an error: exit status 1 means no match, 2 means an error.
PRACTICE
1. cd projects and run grep -rn TODO . then add --include='*.java'.
2. grep -v INFO logs/app.log | head and grep -c ERROR logs/*.log.
3. find . -name '*.java' and find . -size +10M.
4. find logs -mtime +30 (list only; never add -delete before reading the list).
Practice tip: open a terminal on Linux, macOS or WSL, make a practice folder, and type every command right after you see it. The recording is a real bash 5.2 session on Debian Linux with GNU coreutils; when macOS or BSD tools behave differently, the video shows the macOS form as TYPED ONLY (not run) and says so.
Next video: #06 Permissions: chmod, chown and ls -l.
#Linux #Unix #Bash #LinuxCommands #CodeAndTestAutomation
Subscribe to Code & Test Automation for more Linux, Java, coding and test automation tutorials: / @codetestautomation