5

Suppost I want to create a directory like "a/b/c" under the current dir.

How to make it?

I have tried this:

Dir.mkdir("a/b/c").

However it does not work.

3 Answers 3

15

I guess this is because parent directories of c do not exist. If you try to do the same in the shell you will also get an error. You can force creation of parent directories if they do not exist with the -p option:

mkdir -p a/b/c

which in Ruby can be done with mkdir_p found in the FileUtils module:

require "fileutils"

FileUtils::mkdir_p "a/b/c"
Sign up to request clarification or add additional context in comments.

Comments

2

What can make your life much easier is

system 'mkdir "/home/awais/development/rails/testing2"'

1 Comment

This was quite useful to know that the path needs to be within double quotes "..", I'm struggling to get this to work now where I want a variable that carries this nested path. Any ideas using the system command? system('mkdir #{path/to/dir}') didn't work, neither does system('mkdir "#{path/to/dir}" ') which creates a directory with the literal text between the quotes
1

You need to use FileUtils.mkdir_p

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.