LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   alias (https://www.linuxquestions.org/questions/linux-newbie-8/alias-512046/)

ryedunn 12-19-2006 04:21 PM

alias
 
is it possible to setup an alias for cd to automatically give me an ls?

for example: alias -x cdls="cd $whatever:ls -la"

Dudydoo 12-19-2006 04:39 PM

alias cdls='cd $whatever; ls -la'

ryedunn 12-19-2006 07:27 PM

that will work for any directory? so now if I put cdls /home its the same as
$cd /home
$ls -la

correcT?

Dudydoo 12-20-2006 02:30 PM

Yes.

To be more precise, if you set your alias like this:

Code:

alias cdls='cd $1; ls -la $1'
the $1 means the first argument given to `cdls` (which is the path)

so if you type:

Code:

cdls /tmp
the shell interprets it as:

Code:

cd /tmp; ls -la /tmp
also to make this permanent add the alias line to your ~/.bashrc file.

Dudydoo 12-20-2006 04:30 PM

I have since found that the above doesn't actually work as intended, apparently aliases don't work with arguments inside the command string; therefore you must use shell functions instead.

Type this in your ~/.bashrc instead:

Code:

cdls() {

    cd $1;
    ls -la $1

}


ryedunn 12-21-2006 05:24 PM

great, thank you


All times are GMT -5. The time now is 02:02 PM.