Ruby Directories - GeeksforGeeks

25 Jan, 2022
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 directoryf=Dir.mkdir "abc" # a directory named abc is createdprint("#{f}")
Output:
0
Checking Ruby directories: The exists() method in Dir class is used to check if a directory exists or not. Syntax:
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 directoryputs Dir.mkdir("folder") # checking if the directory exists or notputs 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 directoryputs Dir.mkdir("folder") # checking if the directory is empty or notputs 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:
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 directoryDir.mkdir("folder") # returns current working directoryputs 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 directoryDir.mkdir("folder") # returns home directoryputs 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 directoryDir.mkdir("folder") # creating object of that directory using new() methodobj=Dir.new("folder") # assigns the path parameter of obj to variable ff=obj.pathprint("#{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 directoryDir.mkdir("folder") # returns the path of the current working directoryputs 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 directoriesDir.mkdir("/workspace/folder1")Dir.mkdir("/workspace/folder2") # displaying the path of the current directoryputs Dir.pwd # changing the current working directoryDir.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 folderDir.mkdir("folder") # displaying the path of the current directoryputs Dir.pwd # changing current working directory to folderDir.chdir("folder")puts Dir.pwd # creating directories inside folderDir.mkdir("subfolder1")Dir.mkdir("subfolder2")Dir.mkdir("subfolder3") # displays all the files and folders present in folderprint("Entries:\n")puts Dir.entries("C:/Users/KIIT/Desktop/folder")
Output:
C:/Users/KIIT/Desktop
C:/Users/KIIT/Desktop/folder
Entries:
.
..
subfolder1
subfolder2
subfolder3
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 folderDir.mkdir("folder") # changing current working directory to folderDir.chdir("folder") # creating directories inside folderDir.mkdir("dabce")Dir.mkdir("abcd")Dir.mkdir("program.rb")Dir.mkdir("program2.rb") # displaying specified files and foldersprint"\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:
Dir.delete "folder"
Dir.rmdir "folder"
Dir.unlink "folder"
Example:
Ruby
#creating directoryDir.mkdir("folder")puts Dir.exist?("folder") # deleting directoryDir.rmdir("folder")puts Dir.exist?("folder")
Output:
true
false
Creating nested directory: mkdir_p() method in FileUtils module is used to create a directory and all its parent directories. Syntax:
FileUtils.mkdir_p 'directory_path'
Example:
Ruby
# creating directory parent_folderDir.mkdir "parent_folder"print("Current Directory: ")puts Dir.pwdrequire "fileutils" # creating nested directory in parent_folderFileUtils.mkdir_p "parent_folder/child_folder/folder" # changing current directory to parent_folderDir.chdir("/workspace/parent_folder")print("Current Directory: ")puts Dir.pwd# checking child folder exists or notputs Dir.exists?("child_folder") # changing current directory to child_folderDir.chdir("/workspace/parent_folder/child_folder")print("Current Directory: ")puts Dir.pwd # checking folder exists or notputs Dir.exists?("folder")
Output:
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 directoriesDir.mkdir "folder1"Dir.mkdir "folder2"require "fileutils" # moving directory folder1 into directory folder2FileUtils.mv( "folder1", "folder2") # changing current directory to folder2Dir.chdir("folder2") # checking if folder1 exists in folder 2puts 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 .
Ruby
require "fileutils" # copying test.txt from folder1 to folder2FileUtils.cp( "folder2/test.txt", "folder1")Dir.chdir("folder1") # checking if test.txt exists in folder1puts File.exist?("test.txt")
Output:
true
sagar0719kumar
rajeev0719singh
sumitgumber28
rkbhola5
Picked
Ruby-OOP
Ruby
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Ruby | Enumerator each_with_index function
Ruby For Beginners
Ruby | Decision Making (if, if-else, if-else-if, ternary) | Set - 1
Ruby | Array shift() function
Ruby Mixins
Ruby | Array collect() operation
Ruby | Module
Ruby | String concat Method
Instance Variables in Ruby
Ruby | Array reject() function