cdnszip cdnszip - 6 months ago 32
Bash Question

Shell option for automatically listing all files when cd to a folder in Linux

I'm really tired of typing

ls
. Is there a command or option in shell that shows you all the files after you
cd
into a folder without having to type
ls
? Thank you!

Original:

folder>cd subfolder
subfolder>ls
yu fi sd


What I'm expecting:

folder>cd subfolder
yu fj sd
subfolder>


Note I know I can use
cd subfolder;ls

but I really do not want to type
ls
as it is very boring.

Basically I was seeking for a command embedded inside shell so that I can turn on or turn off this feature as my wish.

Answer
  1. If you want ls only after cd

    You actually need in .bashrc define cd as a function using something like:

    cd() {
        builtin cd $@
        ls
    }
    
  2. If you want to ls after each command in your shell:

    Then set in .bashrc something like:

    prompt_func() {
        export PS1="$(ls)\n$ "
    }
    export PROMPT_COMMAND=prompt_func