#files Clear
Tags #php #kotlin #bash #go #sql #rust #typescript #html #java #python #files #utils #strings #http #concurrency #async #json #arrays #security #types #crypto #database #dates #format
PHP CSV Writer with Proper Escaping
Write rows to a CSV file using fputcsv with sensible quoting. Also writes a UTF-8 BOM so Excel opens the file with the right encoding.
Bash Get Script's Own Directory
The "where am I" boilerplate that every nontrivial bash script needs. Resolves symlinks correctly with realpath, falls back gracefully if realpath is missing.
Kotlin Write to a File
`writeText` overwrites; `appendText` appends. For multiple writes, use `bufferedWriter().use { }` for efficiency + auto-close.
PHP Tail Last N Lines of File
Efficiently read the last N lines of a (possibly large) file by seeking to the end and walking backwards in fixed-size chunks. Avoids loading the entire file into memory.
Bash Tail Last N Lines (no `tail` shortcuts)
`tail -n 20` works for any case where tail is available. The pure-bash version below is useful inside containers or rescue shells where coreutils is missing.
Kotlin Path Operations with java.nio.file
Modern Kotlin code uses `java.nio.file.Path` over the legacy `File`. Operator overloads + Kotlin extensions make it ergonomic.
Java Path Operations — Resolve, Relativize, Normalize
`Path` (NIO) replaces `File` for new code. Operator-like methods compose paths cleanly across OSes — and the underlying file isn't touched until you do I/O.
Rust CSV Read/Write with the csv Crate
`csv` + `serde` lets you read/write CSVs directly into typed structs. No manual field parsing; column order tolerant via headers.
PHP Human-Readable File Size
Format a byte count as a human-friendly string (KB / MB / GB / TB). Defaults to base-1024 sizes; pass base 1000 for SI units.
PHP Rotating Log Writer
Append to a single log file but auto-rotate to .1/.2/.3 once the file exceeds a configurable size. No external dependencies — just rename + size check.
PHP Cron Singleton with flock
Make sure only one copy of a long-running cron job runs at a time. Uses a non-blocking advisory lock on a sentinel file — second invocation exits immediately if the lock is held.
Python pathlib Quick Reference
Forget os.path.* — pathlib.Path is the modern, OS-aware way to work with paths. Operator overloading for joins, attribute access for components, methods for reads/writes/iterations.
Rust Walk a Directory Tree
`walkdir` recursively iterates every entry under a path, skipping inaccessible directories cleanly. The standard-library alternative requires manual recursion.
Bash flock — Cron-Singleton Lock
Prevent two copies of a cron job from running simultaneously. flock takes an FD to a lock file; if the lock is held, the second invocation exits immediately.
PHP Find Files Modified in Last N Days
List every file under a directory that was modified within the last N days. Useful for incremental backups or detecting stale entries.
Python Walk Directory Tree (Filtered)
Recursively yield every file under a directory matching a predicate. Built on pathlib.rglob; the predicate gives you precise control without listing the whole tree up front.
Java Files.walk — Recursive Tree Traversal
`Files.walk` returns a lazy Stream over every entry under a path. Filter with stream operations; remember to close the stream (or use try-with-resources).
PHP Recursively Delete Directory
Remove a directory and everything under it. Defensive against symlinks (unlinks the symlink rather than recursing into it). Returns the count of items removed.