Skip to content

Maintenance Ruby 3.3 gets fixes, but no new features. Switch to Ruby 4.0 →

class method

FileUtils.mv

mv(src, dest, force: nil, noop: nil, verbose: nil, secure: nil)

Moves entries.

Arguments src (a single path or an array of paths) and dest (a single path) should be interpretable as paths.

If src and dest are on different file systems, first copies, then removes src.

May cause a local vulnerability if not called with keyword argument secure: true; see Avoiding the TOCTTOU Vulnerability.

If src is the path to a single file or directory and dest does not exist, moves src to dest:

Example
tree('src0')
# => src0
#    |-- src0.txt
#    `-- src1.txt
File.exist?('dest0') # => false
FileUtils.mv('src0', 'dest0')
File.exist?('src0')  # => false
tree('dest0')
# => dest0
#    |-- src0.txt
#    `-- src1.txt

If src is an array of paths to files and directories and dest is the path to a directory, copies from each path in the array to dest:

Example
File.file?('src1.txt') # => true
tree('src1')
# => src1
#    |-- src.dat
#    `-- src.txt
Dir.empty?('dest1')    # => true
FileUtils.mv(['src1.txt', 'src1'], 'dest1')
tree('dest1')
# => dest1
#    |-- src1
#    |   |-- src.dat
#    |   `-- src.txt
#    `-- src1.txt

Keyword arguments:

  • force: true - if the move includes removing src (that is, if src and dest are on different file systems), ignores raised exceptions of StandardError and its descendants.

  • noop: true - does not move files.

  • secure: true - removes src securely; see details at FileUtils.remove_entry_secure.

  • verbose: true - prints an equivalent command:

    Example
    FileUtils.mv('src0', 'dest0', noop: true, verbose: true)
    FileUtils.mv(['src1.txt', 'src1'], 'dest1', noop: true, verbose: true)
    

    Output:

    Example
    mv src0 dest0
    mv src1.txt src1 dest1
    
See also #mv .move .options .mkpath also in 4.1 · 4.0 · 3.4 · 3.2 · 3.1 · 3.0