Skip to content

End of Life Ruby 3.2 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

instance method

FileUtils#move

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

Alias of #mv

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
    

FileUtils.move is an alias for FileUtils.mv.

See also #mv .move #mkpath #mkdir_p also in 4.1 · 4.0 · 3.4 · 3.3 · 3.1 · 3.0