cdnszip cdnszip - 1 year ago 69
Bash Question

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

I'm really tired of typing

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


folder>cd subfolder
yu fi sd

What I'm expecting:

folder>cd subfolder
yu fj sd

Note I know I can use
cd subfolder;ls

but I really do not want to type
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 Source
  1. If you want ls only after cd

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

    cd() {
        builtin cd $@
  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