Text Processing and File OperationsLesson 4.4
Bash file and directory operations at scale
find command, find with exec, xargs for bulk operations, mktemp, file locking with flock, recursive operations, handling special filenames, stat command, file permissions with chmod
find: The Right Tool for File Operations
find searches a directory tree with precise criteria. Combine with -exec or xargs to act on results.
# Find all .log files modified in the last 7 days
find /var/log -name "*.log" -mtime -7
# Find and delete empty files
find /tmp -type f -empty -delete
# Execute a command per file
find . -name "*.sh" -exec chmod +x {} \;
# xargs: faster for large result sets
find . -name "*.log" | xargs gzip
# Handle filenames with spaces — use null delimiter
find . -name "*.txt" -print0 | xargs -0 wc -lSafe Temp Files
tmpfile=$(mktemp) # /tmp/tmp.XXXXXXXX
tmpdir=$(mktemp -d) # creates a temp directory
trap "rm -rf $tmpfile $tmpdir" EXIT
echo "data" > "$tmpfile"
process_file "$tmpfile"File Locking
#!/usr/bin/env bash
lockfile="/var/run/myscript.lock"
exec 9>"$lockfile"
if ! flock -n 9; then
echo "Another instance is running" >&2
exit 1
fi
echo "Got the lock. Running exclusively."
# Lock released automatically when script exitsNever use if [ -f lockfile ] for locking — it has a race condition. flock is atomic at the OS level.
