Skip to content

Instantly share code, notes, and snippets.

@nicholaschiasson
Last active May 4, 2017 02:02
Show Gist options
  • Save nicholaschiasson/e2712d020a6317779555274f58b70f2f to your computer and use it in GitHub Desktop.
Save nicholaschiasson/e2712d020a6317779555274f58b70f2f to your computer and use it in GitHub Desktop.
Some bash functions and aliases to list hidden dot files and directories.

ls_hidden

  • ls_hidden.sh

    Some bash functions and aliases to list hidden dot files and directories.

    Aliases ls. and ll. are created to functionally imitate the ls command and popular alias ll, respectively.

    Command line arguments are supported. All options available to the ls command apply, however the -a option does not list ALL items in a directory, but only those preceeded by a dot. Items not preceeded by a dot can be listed if specified manually as a command line argument.

MIT License
Copyright (c) 2017 Nicholas Omer Chiasson
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
# Custom function and aliases to list hidden items the way that ls would.
_HIDDEN_LS_FUNC()
{
WORKING_OS=`uname -s`
case "${WORKING_OS}" in
Darwin)
COLOR_FLAG=-G
;;
*)
COLOR_FLAG=--color=auto
;;
esac
if [ $# -lt 1 ]; then
ls -dF ${COLOR_FLAG} .*
else
_HIDDEN_LS_FUNC_WORKING_DIR="$PWD"
unset FIRST_LISTING_PASSED
SPECIFIED_FILES=()
LS_ARGUMENTS=()
TMP_DIR_LIST_FILE=`mktemp`
for arg in $@; do
if [ -d "$arg" ]; then
echo "$arg" >> "$TMP_DIR_LIST_FILE"
elif [ -e "$arg" ]; then
FIRST_LISTING_PASSED=1
SPECIFIED_FILES+=("$arg")
else
LS_ARGUMENTS+=("$arg")
fi
done
sort "$TMP_DIR_LIST_FILE" -o "$TMP_DIR_LIST_FILE"
if [ -n "$FIRST_LISTING_PASSED" ]; then
ls -F ${COLOR_FLAG} ${LS_ARGUMENTS[@]} ${SPECIFIED_FILES[@]}
elif [ -z "$(cat $TMP_DIR_LIST_FILE)" ]; then
ls -df ${COLOR_FLAG} ${LS_ARGUMENTS[@]} .*
fi
while IFS='' read -r line || [[ -n "$line" ]]; do
if [ -n "$FIRST_LISTING_PASSED" ]; then
echo
fi
FIRST_LISTING_PASSED=1
echo "$line:"
\cd "$line"
ls -dF ${COLOR_FLAG} ${LS_ARGUMENTS[@]} .*
\cd "$_HIDDEN_LS_FUNC_WORKING_DIR"
done < "$TMP_DIR_LIST_FILE"
rm "$TMP_DIR_LIST_FILE"
unset _HIDDEN_LS_FUNC_WORKING_DIR FIRST_LISTING_PASSED SPECIFIED_FILES LS_ARGUMENTS TMP_DIR_LIST_FILE
fi
}
alias ls.='_HIDDEN_LS_FUNC'
alias ll.='_HIDDEN_LS_FUNC -l'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment