Skip to content

Instantly share code, notes, and snippets.

@earwig
Last active May 19, 2018 22:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save earwig/d05e55ac8a12dafe4ca50099f7938546 to your computer and use it in GitHub Desktop.
Save earwig/d05e55ac8a12dafe4ca50099f7938546 to your computer and use it in GitHub Desktop.
x: eXamine files/dirs using the right tool
#!/bin/bash
set -euo pipefail
if [[ $# -eq 0 ]]; then
args=(".")
else
args=("$@")
fi
for f in "${args[@]}"; do
if [[ -d "$f" ]]; then
ls -lAF --color=auto "$f"
else
len=($(wc -cl "$f" | sed -e 's/^ *//' -e 's/ */ /' | cut -d' ' -f1-2)) || continue
lines=$(tput lines)
if [[ "$(file -b -L --mime-encoding "$f")" == "binary" ]]; then
if [[ $((${len[1]}/16)) -ge $lines ]]; then
xxd "$f" | less
else
xxd "$f"
fi
else
if [[ ${len[0]} -ge $(($lines - 1)) ]]; then
less "$f"
else
cat "$f"
fi
fi
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment