Copy and replace dierectory

copy folders from source location and replace in target location
how can i achive this?

Perhaps you are misunderstanding the purpose of this forum. It’s not a coding service…

1 Like

Take a look at FileUtils module:

You can use the kernel method which would be the easiest way. if you are using linux I recommend the sudo gem to help smooth things over. Using Kernel.system " system command" ( or just system " ") allows you to run system commands like moving directory trees. You can even put it into a defined method to make the process easier.

def relocate_file(dir_name, new_path)
puts “enter directions path:”
print “/home/#{$USER}/” ; dir_name.gets
puts “enter new destination:”
print /home/#{$USER}/" ; new_path.gets

system " mv /home/#{$USER}/#{dir_name} /home#{$USER}/{#new_path}"
end

That’s a rather bad practice. Because:

  1. FileUtils#mv(somefile, dest) can just do what system(“mv #{somefile} #{dest}”) does. FileUtils is default in Ruby, you don’t need to install any gem, and FileUtils is there for a very long time. FileUtils is cross platform, system('mv…) is not.

  2. Using a lot of system() calls can create a lot of processes. It’s not particularly bad but until the max_pid is reached, it will make your /proc/maxpid count in millions.

  3. Using system() is way way slower than using Ruby’s default. Benchmark:
    image

You can see, print “\e[H\e[2J\e[3J” is way faster than running clear command from Linux terminal. In this case, 214 times faster.


So I will just suggest to go with what Ruby’s standard library has, this will also work in any other OS supporting Ruby.

Here’s a bad test I did:
I renamed /bin/cp as /bin/cp1. Then I ran FileUtils#cp. It failed. That means it just runs the cp command in Linux. On windows, it will use something else, but it makes programs platform independent.

Thank you so much!! for responses

1 Like