Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I frequently run the ls command after running the cd command. How can I create an alias (like cs) for this operation?

share|improve this question
add comment

5 Answers

From Bash Tips and Tricks: 'cd' with style:

Finally, I want to show you how to write your own custom replacement for the 'cd' command.

Do you find yourself always typing the same thing upon changing into a directory? You probably at least list the files there every time, perhaps so much that your hands automatically type 'ls' after every 'cd'.

Well, by trying every way I could think of, it turns out there's only one way to properly accomplish the goal we're seeking. We have to create a shell function.

Shell functions are part of shell programming. Like in compiled programming languages, functions provide a sort of procedural modularizability. One can create a generic function to perform an often-used bit of logic or computation with different parameters. In this case, the parameter is the current working directory.

Here's a simple one:

function cs () {
    cd $1
    ls
}

As @geirha corretly notes, the above function will fail if you try to switch to a directory with a space in its name:

$ cs A\ B/
-bash: cd: A: No such file or directory
<current directory listing>  

You should instead use the following function:

function cs () {
    cd "$@" && ls
    }

Once you add that code to your ~/.bashrc, you should be able to do this:

hello@world:~$ cs Documents/
example.pdf tunafish.odt
hello@world:~/Documents$
share|improve this answer
1  
That'll fail for directories containing whitespace. See the comment to dv3500ea's answer. –  geirha Feb 2 '11 at 8:15
1  
@geirha Thanks for the info, fixed my answer. –  Alvin Row Feb 2 '11 at 18:22
add comment

Use a function instead of an alias:

cs() { cd "$1" && ls; }
share|improve this answer
4  
+1, as this version will stop if 'cd' fails. –  htorque Dec 4 '10 at 12:44
add comment

You can use the builtin command in bash :

function cd() {
    new_directory="$*";
    if [ $# -eq 0 ]; then 
        new_directory=${HOME};
    fi;
    builtin cd "${new_directory}" && ls
}
share|improve this answer
2  
This is pretty slick. I had a solution similar to @Florian in my .bashrc for a very long time, but this is much more satisfactory for when I forget that colleagues don't have my 'cs' alias on their computers. –  dino Oct 1 '12 at 12:45
    
I like this answer the best. For some reason, you can't use aliases to overwrite the command, so you have to make a function like this answer. :D –  trusktr Oct 30 '12 at 20:45
    
But what about non-builtin commands? For example, the above won't work for overwriting the ls command. Is there some keyword to use instead of "builtin"? –  trusktr Oct 30 '12 at 20:52
    
use the full path for it, example : function ls() { /usr/bin/ls $* } –  OneOfOne Oct 31 '12 at 17:40
add comment
up vote 5 down vote accepted

Thanks Florian Diesch for the tip of using a function. I can't use cs as the name because there is a cs command in the csound package, so I used lc.

I added this to ~/.bash_aliases (nano ~/.bash_aliases):

function lc () {
    cd $1;
    ls 
}

The terminal needs to be reset for this to come into effect.

share|improve this answer
6  
Having $1 unquoted like that will make it fail if the directory contains whitespace. Also, you should check the return value of cd; if it failed (e.g. permission denied), there's no point in running the ls. lc() { cd "$@" && ls; } –  geirha Feb 2 '11 at 8:13
add comment

As an expansion to this function: cs() { cd "$1" && ls; }, you may want to pass all of the function's arguments to cd by using $@ instead of "$1" as such: cs() { cd $@ && ls; }.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.