Here's a function version of this instead of an alias. It does rely on extended glob, which can be turned on with shopt -s extglob
. This allows for SHELL pattern matching, which in this implementation prevents .
and ..
from being returned as entries.
hidden() {
if [[ $# -eq 1 ]]; then
exec 3>&1
_hidden=$(cd "${1}"; ls -d .!(|.)?* 1>&3)
unset _hidden
exec 3>&-
elif [[ $# -gt 1 ]]; then
exec 3>&1
for i in $@; do
_hidden=$(cd "${i}"; echo "${i}:" 1>&3; ls -d .!(|.)* 1>&3)
done
unset _hidden i
exec 3>&-
else
ls -d .!(|.)*
fi
}
It cd
s into the desired directory in a subshell, and redirects the output to the current shell. This stops the path of the provided directory being written preceding each entry. It should behave very similarly to ls
when given directory arguments.
If you want to show .
and ..
, just change the three ls
commands in the function to ls -d .*
which is also not dependant on extended glob. You can also add other arguments to this command, like --color=auto
. The function itself will not accept these option arguments. If options are already included in an alias redefining ls
, the function will use the alias and therefore implement those options.
'ls -a | grep '"'"'^\.'"'"'
It looks awful (and for this example it is completely unnecessary because you can just swap out double-quotes for either of the pairs), but on very rare occasions it is useful. You might also want-C
flag (force multi-column output). See this image http://imgur.com/a/VIVFP – Dylan Sep 29 '16 at 00:21