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:
- The exists() method in Dir class is used to check if a directory exists or not.
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:
- The new() is used to create a new directory object.
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 print( "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 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:
Dir.delete "folder" Dir.rmdir "folder" Dir.unlink "folder"
- Example:
Ruby
#creating directory Dir .mkdir( "folder" )
puts Dir .exist?( "folder" )
# deleting directory Dir .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_folder Dir .mkdir "parent_folder"
print( "Current Directory: " )
puts Dir .pwd
require "fileutils"
# creating nested directory in parent_folder FileUtils.mkdir_p "parent_folder/child_folder/folder"
# changing current directory to parent_folder Dir .chdir( "/workspace/parent_folder" )
print( "Current Directory: " )
puts Dir .pwd
# checking child folder exists or not puts Dir .exists?( "child_folder" )
# changing current directory to child_folder Dir .chdir( "/workspace/parent_folder/child_folder" )
print( "Current Directory: " )
puts Dir .pwd
# checking folder exists or not puts 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 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 .
Ruby
require "fileutils"
# copying test.txt from folder1 to folder2 FileUtils.cp( "folder2/test.txt" , "folder1" )
Dir .chdir( "folder1" )
# checking if test.txt exists in folder1 puts File .exist?( "test.txt" )
|
- Output:
true