Automatic LS after change directory? ( Linux)

mrgstiffler

[H]F Junkie
Joined
Dec 20, 2000
Messages
13,408
This is a rather odd question I know... But I just have this compulsion with knowing what files are in the directory I'm in. Even though it has no benefit for me, it's just something that I need. Like an itch I have to scratch. What would be the best way to have 'ls -al' run automatically after changing directories?
 
The easiest way to do that I think would be to create eather a shell function or alias, depending on your shell. If your running bash, put something like this in your .bashrc startup script:

Code:
cdl () {
  cd "$@" && ls -al
}

What this does is create a shell function called "cdl" that does a cd into the directory you specifiy and then an "ls -al". You can use it just like "cd".
 
Could also do it using the bash PROMPT_COMMAND environment variable and some references to the most recent command...I've never done it, but seems like it'd be doable. The man page has a whole list of the vars you can work with.
 
You can also make a bash script exactly the same way Cristopher has his bashrc function.

Just make a file (minus the quotes)
"touch /usr/bin/cdl"

Then put this in the file (minus the quotes)
"#!/bin/bash
cd "$@" && ls -al"

Then you could invoke it by typing cdl /something/some/thing

Of course I think Cristophers bashrc function would be easier, and more convenient
 
Christopher said:
The easiest way to do that I think would be to create eather a shell function or alias, depending on your shell. If your running bash, put something like this in your .bashrc startup script:

Code:
cdl () {
  cd "$@" && ls -al
}

What this does is create a shell function called "cdl" that does a cd into the directory you specifiy and then an "ls -al". You can use it just like "cd".

Yeah, that's what I was thinking about doing. I figured it would be the best way to go about it. It works great too. Just gotta get used to using "lcd" instead of "cd". "lcd" is a little easier to type than "cdl", plus that always comes out "cd l" when I try to type it. Trained fingers...

Thanks for all the replies!
 
If you want to use cd instead of lcd or cdl, just alias it in your .bashrc file (I don't remember the exact syntax, but most distro's will already have some aliases in the file you can look at)
 
penguin said:
If you want to use cd instead of lcd or cdl, just alias it in your .bashrc file (I don't remember the exact syntax, but most distro's will already have some aliases in the file you can look at)

Code:
alias cd='/bin/cd && /bin/ls'
 
I'm not that familiar with bash. I usually use tcsh, and in tcsh (and probably csh) if you define the alias cwdcmd, the command you aliased will be run after every cd command.
 
Just use csh then, and alias it.

alias cd 'chdir \!*; ls -al'
 
Back
Top