I am writing a Ruby script inside which I would like to invoke/execute some shell commands.

The shell commands I would like to run should check if a directory named 'tmp' under /var/lib/mysql/ exists or not.

  • if it exists, remove all the files (including sub-dirs & files) inside /var/lib/mysql/tmp/.

  • If it does not exist, just create it.

(P.S. only root user can access /var/lib/mysql/)

I know mkdir will create directory, but I am not sure how to make a command to check if the directory exists or not.

As a whole, I would like to have some shell commands to achieve the scenario described above, and the more elegant way the better, as I will run the shell commands in a ruby script.

Anyone can Help me ?



 [ -d /var/lib/mysql/tmp ] || mkdir /var/lib/mysql/tmp

what you are looking for? See man test for details.

Or to achieve both of your goals:

 [ -d ${_DIR} ] && rm -rf ${_DIR}
 mkdir ${_DIR}
  • 1
    Can you explain the script you wrote please, I am not quite familiar with shell script...
    – Mellon
    Nov 29 '11 at 13:16
  • 2
    The [ is actually the command test on most Unices so the above reads test -d DIR or literally: exit cleanly (with code 0) if DIR exists and is a directory. Actually, all this is not absolutely necssary as rm -rf won't complain about a non-existant directory, i.e. rm -rf DIR; mkdir DIR would do the job.
    – u-punkt
    Nov 29 '11 at 13:31
  • I prefer the solution in your above comment more than the one in the answer section. As I mentioned in my post, the more elegant the better. Thank you.
    – Mellon
    Nov 29 '11 at 13:39
  • The second code snippet is unsafe as it currently is. You need to quote ${_DIR} to allow spaces or characters that are interpreted by the shell.
    – Chris Down
    Nov 29 '11 at 23:12

The following two shell commands will do the trick:

mkdir -p  /var/lib/mysql/tmp/    # create directories (that don't exist, yet)
rm    -rf /var/lib/mysql/tmp/*   # delete all files and sub-directories

Note: you may need to prepend each line with sudo to enable your Ruby script to carry out the shell commands with root privileges. However, it can be tricky to set up sudo correctly.

The -p option to mkdir to eliminates the need to test existence for a directory:

-p        Create intermediate directories as required.  If this option is
          not specified, the full path prefix of each operand must already
          exist.  On the other hand, with this option specified, no error
          will be reported if a directory given as an operand already
          exists.  Intermediate directories are created with permission
          bits of ``rwxrwxrwx'' (0777) as modified by the current umask,
          plus write and search permission for the owner.

You could use

test -d /var/lib/mysql/tmp && rm -rf /var/lib/mysql/tmp/* || mkdir /var/lib/mysql/tmp

but I suggest you use ruby’s own built-ins to check if the directory exists, and then call rm … or mkdir … depending on the outcome of the test.

  • I tried using Ruby build-in File.exists?('/var/lib/mysql/tmp') , but it always returns false though I have the tmp dir, I guess this is because only root user has access to the path as I mentioned in my post. That's why I did not use Ruby to check, but if you have any workaround for this, I would like to hear.
    – Mellon
    Nov 29 '11 at 13:25
  • 1
    You could skip the test and simply do rm -rf /var/lib/mysql/tmp; mkdir -p /var/lib/mysql/tmp, as others have suggested, but that doesn’t exactly answer your question, since it removes and re-creates the directory /var/lib/mysql/tmp every time, also when it exists beforehand. Nov 30 '11 at 8:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.