A directory is a location where files can be stored. For Ruby, the Dir class and the FileUtils module manages directories and the File class handles the files. Double dot (..) refers to the parent directory for directories and single dot(.)refers to the directory itself. The Dir Class The Dir class provides access to and contents of file system directory structures in Ruby.It provides a means of listing folder contents, generating file names with proper path separators, etc. These are some of the features of the Dir class:
Creating Ruby directories: The mkdir() method in Dir class is used to create directory. We can use the below code to create non nested directory, the mkdir() method returns 0 if the directory is successfully created. Syntax:
Dir.mkdir "name_of_directory"
Example:
Ruby
# creating directory
f=Dir.mkdir "abc"
# a directory named abc is created
print("#{f}")
Output:
0
Checking Ruby directories:
The exists() method in Dir class is used to check if a directory exists or not. Syntax:
Dir.exist?"name_of_directory"
Example:
Ruby
# creating directory
puts Dir.mkdir("folder")
# checking if the directory exists or not
puts Dir.exists?("folder")
Output:
0
true
The empty? method in Dir class is used to check if a directory is empty or not. Syntax:
Dir.empty?"name_of_directory"
Example:
Ruby
# creating directory
puts Dir.mkdir("folder")
# checking if the directory is empty or not
puts Dir.empty?("folder")
Output:
0
true
Working with Ruby directories: Dir class uses different methods for Ruby directory operations, such methods are new(), pwd(), home(), path(), getwd(), chdir(), entries(), glob() etc.
The new() is used to create a new directory object. Syntax:
obj=Dir.new("name_of_directory")
In the above code, folder directory should already exist.
The pwd() method in Dir class returns the current directory. Syntax:
Dir.pwd
Example:
Ruby
# creating directory
Dir.mkdir("folder")
# returns current working directory
puts Dir.pwd
Output:
/workspace
The home() method in Dir class is used to return the home directory of the current user. Syntax:
Dir.home
Example:
Ruby
# creating directory
Dir.mkdir("folder")
# returns home directory
puts Dir.home
Output:
/workspace
The below code will return the home directory of a specific user.
Dir.home('username')
The path() method of Dir class is used to return the path parameter. Syntax:
d=Dir.new("name_of_directory")
d.path
Example:
Ruby
# creating directory
Dir.mkdir("folder")
# creating object of that directory using new() method
obj=Dir.new("folder")
# assigns the path parameter of obj to variable f
f=obj.path
print("#{f}")
Output:
folder
The getwd() method of Dir class is used to return the path of the current directory. Syntax:
Dir.getwd
Example:
Ruby
# creating directory
Dir.mkdir("folder")
# returns the path of the current working directory
puts Dir.getwd
Output:
/workspace
The chdir() method of Dir class is used to modify the current directory. Syntax:
Dir.chdir("name_of_directory")
Example:
Ruby
# creating directories
Dir.mkdir("/workspace/folder1")
Dir.mkdir("/workspace/folder2")
# displaying the path of the current directory
puts Dir.pwd
# changing the current working directory
Dir.chdir("folder2")
puts Dir.pwd
Output:
/workspace
/workspace/folder2
The entries() method in Dir class is used to return all the files and folders present in the directory in an array. Syntax:
Dir.entries("directory")
Example:
Ruby
# creating a directory named folder
Dir.mkdir("folder")
# displaying the path of the current directory
puts Dir.pwd
# changing current working directory to folder
Dir.chdir("folder")
puts Dir.pwd
# creating directories inside folder
Dir.mkdir("subfolder1")
Dir.mkdir("subfolder2")
Dir.mkdir("subfolder3")
# displays all the files and folders present in folder
The glob() method in Dir class is used to display all the files having a certain matching pattern. Syntax:
Dir.glob("pattern")
Example:
Ruby
# creating a directory named folder
Dir.mkdir("folder")
# changing current working directory to folder
Dir.chdir("folder")
# creating directories inside folder
Dir.mkdir("dabce")
Dir.mkdir("abcd")
Dir.mkdir("program.rb")
Dir.mkdir("program2.rb")
# displaying specified files and folders
print"\nAll files in the current working directory: \n"
puts Dir.glob("*")
print"\nAll files containing 'abc' in the name: \n"
puts Dir.glob("*abc*")
print"\nAll ruby files: \n"
puts Dir.glob("*.rb")
Output:
All files in the current working directory:
abcd
dabce
program.rb
program2.rb
All files containing 'abc' in the name:
abcd
dabce
All ruby files:
program.rb
program2.rb
Removing Ruby Directories : There are various methods in class Dir to remove Ruby Directories like rmdir(), delete() and unlink() Syntax:
Current Directory: /workspace
Current Directory: /workspace/parent_folder
true
Current Directory: /workspace/parent_folder/child_folder
true
Moving files and folders: mv() and move() methods in FileUtils module are used to move files and folders from current directory to destination directory. Syntax:
FileUtils.mv("source", "destination")
Example:
Ruby
# creating directories
Dir.mkdir "folder1"
Dir.mkdir "folder2"
require "fileutils"
# moving directory folder1 into directory folder2
FileUtils.mv( "folder1", "folder2")
# changing current directory to folder2
Dir.chdir("folder2")
# checking if folder1 exists in folder 2
puts Dir.exists?("folder1")
Output:
true
Copying files from one directory to another directory: cp() method in FileUtils module is used to copy files from current directory to destination directory. Syntax:
FileUtils.cp("source", "destination")
Example: Consider two directories folder1 and folder2 are already created and folder2 contains test.txt .
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy
Improvement
Please go through our recently updated Improvement Guidelines before submitting any improvements.
This article is being improved by another user right now. You can suggest the changes for now and it will be under the article's discussion tab.
You will be notified via email once the article is available for improvement.
Thank you for your valuable feedback!
Please go through our recently updated Improvement Guidelines before submitting any improvements.
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.