Thursday, June 23, 2011

Aliasing cd and ls

In the Linux console, is a common action to list directory content (ls), once entered into that folder (cd). Thus, it would be more efficient to join those 2 command into 1.

We can do this adding the next code into ~/.bashrc file:

function cl {
if [ "$1" == "" ]
then
cd
else
cd $1
fi
ls
}
alias cd='cl'


Therefore if we open a new console and we type 'cd <directory>', it will execute 'cd <directory>' and 'ls', respectively.

Resources
http://www.unix.com/shell-programming-scripting/16318-cd-then-ls-l.html
http://www.linuxforums.org/forum/newbie/161327-aliasing-cd-ls.html