4

I don't know if this is possible, but, I have a folder which I would like to show some warning message when the user enters in it. In my case would say that the folder could be deleted without previous warning to save some disk space. I already create a file inside the folder with the warning message:

WARNING!
##########################################################################################################################################################
Please, 
be advised, that the folder /company-backup/amazon-s3 can be deleted without previous WARNING to save disk space as the INFRASTRUCTURE TEAM judge necessary.

Best regards,
Infrastructure Team.
###########################################################################################################################################################

Is that possible ? Any idea ?

5
  • 1
    The user enters this folder inside a shell session? (Or via FTP?) Is the shell used identical for all users? With zsh you can simply use chpwd () { [[ -e .readme ]] && cat .readme }. chpwd is called whenever the current working dir is changed, .readme is of course your message. This should be doable in other shells, too. In the worst case by redefining cd.
    – mpy
    Jun 25, 2013 at 15:01
  • @mpy that's the second good answer you have given as a comment in the last 24h, why don't you write these up as answers?
    – terdon
    Jun 25, 2013 at 15:05
  • @terdon: Because I'm too lazy ;). With some more infos from the OP here, I think about writing an answer because I use this per-folder reminder heavily myself.
    – mpy
    Jun 25, 2013 at 15:16
  • @mpy, your loss, I just stole your comment in my answer in the linked post :). You should be able to do this micely for both bash and zsh if you set cd to be an alias to the function you suggested.
    – terdon
    Jun 25, 2013 at 15:18
  • @terdon: Haha, I'm fine with that -- you did a good job. Btw. Oliver Salzburg made a really good comment on that topic on meta (meta.superuser.com/a/6830/195224).
    – mpy
    Jun 25, 2013 at 15:38

3 Answers 3

7

Here is one possibility, if your users access the directory via command line:

Redefine the shell's builtin cd command in a global rc file to check if a file .readme is present in the current dir and if so, display it (with some nice optical elements).

a. With zsh (/etc/zshrc) you can use a specially designed hook function (chpwd), which is called whenever the current working dir is changed:

function chpwd {
  if [[ -e .readme ]]; then
    echo
    echo \#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#
    cat .readme
    echo \#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#
    echo
fi }

b. I don't know if there's a similar possibility in bash, however you can redefine cd in /etc/bash.bashrc:

cd() {
   builtin cd "$@"
   if [[ -e .readme ]]; then
     echo
     echo \#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#
     cat .readme
     echo \#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#
     echo
   fi
}

c. And finally the same with csh (/etc/csh.cshrc) syntax:

alias cd 'cd \!*; eval "if (-e .readme) then \\
  echo \\
  echo ################################################################################ \\
  cat .readme \\
  echo ################################################################################ \\
  echo \\
endif"'

And this it how it looks like:

user@machine:~> cd temp

################################################################################
Attention! This is a temporary directory!
################################################################################

user@machine:~/temp> 
1
  • Ah, this is a cool alternative method to what I used in my answer! I didn't notice that you added a bash and csh method as well. Jun 25, 2013 at 15:55
2

Use inotify-tools:

$ inotifywait -e open YOUR_DIRECTORY

Example:

$ inotifywait -e open for_delete
Setting up watches.  
Watches established.
for_delete/ OPEN,ISDIR
$

There is good example of script in link above.

0

The suggestion regarding zsh was good, but if you want to do something for bash, you might need to script that yourself. I hooked into the cd command in bash for a different purpose (so it would display PWD in the title of the terminal window) but the same technique could be used to cat a .readme file whenever you cd into a directory.

All I did was write a script to perform the cd (using the builtin keyword to allow me to overwrite the actual command--you could also use \cd) and also do whatever operation I wished after it completed. I stored the script in my home directory somewhere (I like ~/scripts, personally), and then created an alias in my .bashrc file that sources it.

This is how it looks for me. For your case, instead of the echo command, you would want to cat $PWD/.readme.

~/scripts/cd.sh:

builtin cd $1
echo -ne "\033]0;$shell_title - `pwd | sed "s|$HOME|~|g"`\007"

~/.bashrc:

alias cd='source ~/scripts/cd.sh'

Now, whenever I type cd into the terminal, it will source that script, changing directory and my window title.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .