Typorithm · medium

find & xargs

bash-findbashfindxargsexec
Saved on this device
Full lesson · 8 learning steps Open any exercise for focused practice
1

Step 1 of 8 · intro card

Learn find & xargs

0%

System

find walks a directory tree filtering by -name (glob), -type, and -mtime; -delete removes matches; pipe results to xargs to build command lines, or use -exec cmd {} + to run a command directly on the matches.

find recursively walks a directory tree starting from the path you give it (here .) and tests each entry against filters: -name takes a glob like "*.log", -type f or -type d picks files or directories, and -mtime -7 selects entries modified in the last 7 days.

Actions run on the matches. -delete removes them in place, while -exec cmd {} + runs a command on the matches, substituting {} for the paths and batching many into each call with the trailing +.

Piping find into xargs is the other common pattern: find prints the paths on stdout, and xargs reads them on stdin to assemble command lines — find . -type f | xargs grep "error" greps every file.

01

-name globs, -type filters files/dirs, -mtime by age.

02

-delete removes matches; -exec cmd {} + runs a command.

03

Pipe to xargs to build command lines from the results.

find . -name "*.log"
find . -type f -mtime -7
find . -name "*.tmp" -delete
find . -type f | xargs grep "error"
find . -name "*.js" -exec wc -l {} +

Read the implementation

Connect each part of the code to the concept before you type.

1. Match by name

find walks the tree from . and keeps files whose name matches the glob *.log.

find . -name "*.log"

2. Type and age

-type f keeps regular files; -mtime -7 keeps ones modified within the last 7 days.

find . -type f -mtime -7

3. Delete matches

-delete removes every matched path — here all *.tmp files under the tree.

find . -name "*.tmp" -delete

4. Pipe to xargs

xargs reads the file list on stdin and builds grep command lines to search each file.

find . -type f | xargs grep "error"

5. Run a command

-exec runs wc -l on the matches; {} is the placeholder and + batches them into few calls.

find . -name "*.js" -exec wc -l {} +

A complete learning loop

01

Understand

Read the concept map and follow the code breakdown.

02

Reconstruct

Type the implementation, repair mistakes and work through every recall exercise.

03

Verify

Use the final check without hints; revisit weak concepts in short practice sessions.

find & xargs: Lesson & Practice · Typorithm | ApplyDjinn