Display Custom Message On Entering A Directory (Linux)

Ever wondered, how you can display a custom message whenever a user enters a particular directory in linux? Well, Here’s how to do it:

1. Open your ~/.bashrc (can use ~/.bash_profile or ~/.bash_login files too), navigate to bottom and add the following bash snippet, write and quit.

2. Source your .bashrc file by running ’. ~/.bashrc’ or ‘source ~/.bashrc’

3. Now, navigate to your target directory using ’cd dirname’, which should display the warning message you added in you ~/.bashrc file as shown in the following image.  

image

How it works ?

Simple, whenever you enter ’cd’ command, bash executes our alias ’mycd’ whose code snippet takes the argument (directory name) that you pass to cd, and checks if it matches with target directory (/production in our case), when there’s a match, it echos the message onto console. You can use this logic to print out help messages/commands and more on your Linux boxes. Hope it helps.