December 10, 2015

Print messages when someone accessing a particular directory

If you ever want to print a message when some accessing a particular directory like below.
 [root@pxe-yum ~]# cd /TEST.DIR/  
 WARNING! WARNING! WARNING!  
 DO NOT -  
  SAVE, REMOVE OR ALTER any files or directories in this mount point.  
 This mount point is ONLY for SYSTEM backup.  
 [root@pxe-yum TEST.DIR]# pwd  
 /TEST.DIR  


This can be easily achieved using the below process -
 - Add the following lines (customize it as required) in the /etc/bashrc file (considering bash is the default shell for the user, which is in case of Linux).  
 cd () {  
   builtin cd "$@"  
   if [[ -e .readme ]]; then  
    echo  
    cat .readme  
    echo  
   fi  
 }  
 - Then, the directory where you want to print a messages, create a .readme file with whatever you wants to print, something like below -  
 [root@pxe-yum TEST.DIR]# pwd  
 /TEST.DIR  
 [root@pxe-yum TEST.DIR]# cat .readme  
 WARNING! WARNING! WARNING!  
 DO NOT -  
  SAVE, REMOVE OR ALTER any files or directories in this mount point.  
 This mount point is ONLY for SYSTEM backup.  

Well, thats it, whenever someone moves into that directory (e.g. cd /TEST.DIR), it will print the messages from the .readme file.
This will work for any directory in the Operating System, all you need to do is after adding the lines in the /etc/bashrc, add a .readme with the required text in the directory where you want to print a message.


No comments:

Post a Comment