Ruby Directories

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"

    Exmaple:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    # creating directory
    f=Dir.mkdir "abc" 
      
    # a directory named abc is created
    print("#{f}")

    chevron_right

    
    

    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"

      Exmaple:

      filter_none

      edit
      close

      play_arrow

      link
      brightness_4
      code

      # creating directory
      puts Dir.mkdir("folder")
        
      # checking if the directory exists or not
      puts Dir.exists?("folder")

      chevron_right

      
      

      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"

      Exmaple:

      filter_none

      edit
      close

      play_arrow

      link
      brightness_4
      code

      # creating directory
      puts Dir.mkdir("folder")
        
      # checking if the directory is empty or not
      puts Dir.empty?("folder")

      chevron_right

      
      

      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

      Exmaple:

      filter_none

      edit
      close

      play_arrow

      link
      brightness_4
      code

      # creating directory
      Dir.mkdir("folder")
        
      # returns current working directory
      puts Dir.pwd  

      chevron_right

      
      

      Output:

      /workspace
    • The home() method in Dir class is used to return the home directory of the current user.
      Syntax:

      Dir.home

      Exmaple:

      filter_none

      edit
      close

      play_arrow

      link
      brightness_4
      code

      # creating directory
      Dir.mkdir("folder")
        
      # returns home directory
      puts Dir.home 

      chevron_right

      
      

      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

      Exmaple:



      filter_none

      edit
      close

      play_arrow

      link
      brightness_4
      code

      # 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}")

      chevron_right

      
      

      Output:

      folder
    • The getwd() method of Dir class is used to return the path of the current directory.
      Syntax:

      Dir.getwd

      Exmaple:

      filter_none

      edit
      close

      play_arrow

      link
      brightness_4
      code

      # creating directory
      Dir.mkdir("folder")
        
      # returns the path of the current working directory
      puts Dir.getwd 

      chevron_right

      
      

      Output:

      /workspace
    • The chdir() method of Dir class is used to modify the current directory.
      Syntax:

      Dir.chdir("name_of_directory")

      Exmaple:

      filter_none

      edit
      close

      play_arrow

      link
      brightness_4
      code

      # 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

      chevron_right

      
      

      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")

      Exmaple:

      filter_none

      edit
      close

      play_arrow

      link
      brightness_4
      code

      # 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 presesnt in folder
      print("Entries:\n")
      puts Dir.entries("C:/Users/KIIT/Desktop/folder"

      chevron_right

      
      

      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")

      Exmaple:

      filter_none

      edit
      close

      play_arrow

      link
      brightness_4
      code

      # 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")

      chevron_right

      
      

      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:

      filter_none

      edit
      close

      play_arrow

      link
      brightness_4
      code

      #creating directory
      Dir.mkdir("folder")
      puts Dir.exist?("folder")
        
      # deleting directory
      Dir.rmdir("folder")
      puts Dir.exist?("folder")
        

      chevron_right

      
      

      Output:

      true
      false

    The FileUtils module
    FileUtils is a module in Ruby which handles both files and directories. FileUtils module is much better than the class of File or Dir alone. It has a lot of similar features, but it can also copy, move, rename files, alter links, etc.
    In order to use FileUtils methods we need to load it first for which we use the require() method.

    require 'fileutils'

    These are some of the features of FileUtils module:

    • 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'

      Exmaple:



      filter_none

      edit
      close

      play_arrow

      link
      brightness_4
      code

      # creating direcotry 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")

      chevron_right

      
      

      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")

      Exmaple:

      filter_none

      edit
      close

      play_arrow

      link
      brightness_4
      code

      # creating direcotries
      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")

      chevron_right

      
      

      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")

      Exmaple:
      Consider two directories folder1 and folder2 are already created and folder2 contains test.txt .

      filter_none

      edit
      close

      play_arrow

      link
      brightness_4
      code

      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")

      chevron_right

      
      

      Output:

      true



    My Personal Notes arrow_drop_up

    Check out this Author's contributed articles.

    If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


    Article Tags :

    1


    Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.