4

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?

2 Answers 2

3

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.

3
  • Why not go straight for an ls function?
    – Jeff Schaller
    Nov 24, 2017 at 3:27
  • @JeffSchaller, what do you mean? Does ls have a built-in function capability? I can't find any info about it. Thanks!
    – John Moon
    Nov 24, 2017 at 3:57
  • simply unalias ls, then define ls() { ... # body of ls_mod ... }, and replace the ls calls with command ls, to bypass the function name.
    – Jeff Schaller
    Dec 19, 2017 at 1:55
2

To flesh out my comment on John Moon's answer, here's a function that wraps ls and adds a -a flag if the path matches a certain list:

function ls() {
  # if no arguments are provided, the current directory is assumed
  [ "$#" -eq 0 ] && set .
  case "$(realpath "$1")" in
        (/path/to/jason/filestoprocess*|\
         /path/to/jason/otherfilestoprocess*)
                set -- -a "$1" ;;
  esac
  command ls "$@"
}

The first section of the function checks to see if a bare ls is being done; in this case, we set . (the current directory) as parameter #1; otherwise, we assume that the (given) first parameter is the path to list.

If the set-or-given path matches the patterns listed (either ...filestoprocess or ...otherfilestoprocess here), then we use set again to add the -a flag into the parameters. At the end, we use command to call the actual ls command with the arguments we've arranged. Thanks to muru for pointing out the set simplification!

To add more paths, simply continue the patterns, separating them with pipe (|) characters. I've broken the two existing patterns onto separate lines with the backslash escape character \ to make them easier to read.

If you have ls aliased to something already, that will take precedence and would need to be removed to make this function work (otherwise, any flags that the alias adds would be passed to this function and end up in $1). If there are flags that you like to have in ls by default, simply add them to the two corresponding command ls calls.

3
  • To avoid repetition, and still allow some support for ls with multiple args, paste.ubuntu.com/26212880
    – muru
    Dec 19, 2017 at 4:34
  • I like it, @muru! Do you mind if I incorporate it?
    – Jeff Schaller
    Dec 19, 2017 at 10:51
  • Sure. I didn't want to make too much of a change to your function without asking, hence the comment
    – muru
    Dec 19, 2017 at 10:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .