Skip to content

Instantly share code, notes, and snippets.

@whitelynx
Created April 30, 2019 23:31
Show Gist options
  • Save whitelynx/ab53dfa9737de4fe606e38669a5fbe11 to your computer and use it in GitHub Desktop.
Save whitelynx/ab53dfa9737de4fe606e38669a5fbe11 to your computer and use it in GitHub Desktop.
Generic "read" script that can list either directory contents or file contents.
#!/bin/bash
c_link=$(tput setaf 199)
c_dir=$(tput setaf 68)
c_file=$(tput setaf 155)
c_special=$(tput setaf 221)
c_dim=$(tput setaf 240)
c_norm=$(tput sgr0)
while [ $# -gt 0 ]; do
path="$1"
if [ -L "$path" ]; then
echo -n "${c_link}$path ${c_dim}=>${c_norm} "
path="$(readlink -f "$path")"
fi
if [ -d "$path" ]; then
echo "directory ${c_dir}$path${c_norm}:"
ls --color=auto "$path"
elif [ -f "$path" ]; then
echo "file ${c_file}$path ${c_dim}($(file -b "$path"))${c_norm}:"
#TODO: If path is a "binary" file (contains control chars), don't cat it to the terminal!
cat "$path"
else
echo "special file ${c_file}$path ${c_dim}($(file -b "$path"))${c_norm}"
fi
shift
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment