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"
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"
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"
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
/workspace
- The home() method in Dir class is used to return the home directory of the current user.
Syntax:
Dir.home
/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.pathfolder
- The getwd() method of Dir class is used to return the path of the current directory.
Syntax:
Dir.getwd
/workspace
- The chdir() method of Dir class is used to modify the current directory.
Syntax:
Dir.chdir("name_of_directory")/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")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")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"
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'
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")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 .
true