Automatically run ls after cd in bash

If you are using linux based operating system then cd and ls are perhaps most frequently used commands. If you too feel that it is logical to change the directory and list the files/folders inside the directory as bundle instead of running two commands separately then here is simple way to make it happen:
  1. Open you terminal and edit bashrc by running sudo gedit ~/.bashrc
    Note: Feel free use editor of your choice (VIM preferbly :smile:), I am using gedit for simplicity
  2. Add the function below at the end of your bashrc file
    cdls() { cd "$@" && ls -al; }
    
    The && means cd to a directory, and if successful (e.g. the directory exists), run ls. Using the && operator is better then using a semicolon ;
    Source.
  3. Save the bashrc file and exit the editor
  4. Open terminal and run cdls <directory name> you should change directory and get the list of files/folders in the directory

Demo

I did with mine and here is a simple demo:
Bash demo

blog comments powered by Disqus