Method: FileUtils.move
- Defined in:
- lib/fileutils.rb
.move ⇒ Object
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:
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:
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 removingsrc(that is, ifsrcanddestare on different file systems), ignores raised exceptions of StandardError and its descendants. -
noop: true- does not move files. -
secure: true- removessrcsecurely; see details at FileUtils.remove_entry_secure. -
verbose: true- prints an equivalent command:FileUtils.mv('src0', 'dest0', noop: true, verbose: true) FileUtils.mv(['src1.txt', 'src1'], 'dest1', noop: true, verbose: true)Output:
mv src0 dest0 mv src1.txt src1 dest1
FileUtils.move is an alias for FileUtils.mv.
1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 |
# File 'lib/fileutils.rb', line 1198 def mv(src, dest, force: nil, noop: nil, verbose: nil, secure: nil) "mv#{force ? ' -f' : ''} #{[src,dest].flatten.join ' '}" if verbose return if noop fu_each_src_dest(src, dest) do |s, d| destent = Entry_.new(d, nil, true) begin if destent.exist? if destent.directory? raise Errno::EEXIST, d end end begin File.rename s, d rescue Errno::EXDEV, Errno::EPERM # move from unencrypted to encrypted dir (ext4) copy_entry s, d, true if secure remove_entry_secure s, force else remove_entry s, force end end rescue SystemCallError raise unless force end end end |