Shell – way to have ls show hidden files for only certain directories

dot-fileslinuxlsshell

I have a directory where regardless of user or options selected, I would like it to always show hidden files.

I know the -a option will show hidden files. I just want to automate the decision on using the option.

Say I'm in /home/user I don't care to see the hidden files, but if I'm in /filestoprcess I want to see the hidden files.

Does this type of functionality exists?

Best Answer

The easiest way I can think of to do this would be to create a shell alias that maps to a function. Say we're using bash and add the following alias to your .bashrc:

alias ls=ls_mod

Now, add the ls_mod function below:

ls_mod () {
    DIRS_TO_SHOW_HIDDEN=(dir1 dir2 dir3)
    for DIR in "$@"; do
        for CHECK in "${DIRS_TO_SHOW_HIDDEN[@]}"; do
            if [ "$(realpath "$DIR")" == "$CHECK" ]; then
                ls -a "$DIR"
            else
                ls "$DIR"
            fi
        done
    done
}

I haven't tested this, so I doubt it's perfect, but at least it gives you the idea. You may need to work to pass extra arguments to ls.

Related Question