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:- Open you terminal and edit
bashrcby runningsudo gedit ~/.bashrc
Note: Feel free use editor of your choice (VIM preferbly :smile:), I am usinggeditfor simplicity - Add the function below at the end of your
bashrcfilecdls() { cd "$@" && ls -al; }The&&meanscdto a directory, and if successful (e.g. the directory exists), runls. Using the&&operator is better then using a semicolon;
Source. - Save the
bashrcfile and exit the editor - 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:
