Linux: How to copy large number of files and overwrite without prompt

I was moving one of my WordPress blogs to another CMS and I had to copy all uploads directory to keep image paths intact. What I didn’t realize was that the uploads directory has very large number of files. My first attempt to copy got stuck. I thought may be server is down but it wasn’t. Uploads directory just has large number of images in it and I ended up waiting for too long.

I tried copying again with no prompt but it failed again complaining for large number of files with this message:

/bin/cp: Argument list too long

I checked the total with ls -1 | wc -l and discovered that I have 75360 images to copy. Wow that’s a lot of images.

Finally I ended up with three problems

1) Copy files from source to destination

2) No prompt

3) Deal with large number of files and cp command failure as explained above

With some search I found this simple one line of command to solve all of above 3 problems.

yes | for file in /source_destination/*; do cp “$file” /destination_directory/; done

yes takes care of disabling prompt for overwriting destination files.

Next is the for loop to copy files from source to destination.

It does the job as I expected but slows down the server by consuming 13-14% of CPU as shown below

Linux: Copying large number of files
Linux: Copying large number of files consumes a lot of CPU time

Better solution

If there is any better and faster way to to this then don’t hesitate to drop suggestions in the comments below.


One response to “Linux: How to copy large number of files and overwrite without prompt”

  1. Rajendra Avatar

    Really a great command that was really helpful

Leave a Reply

Your email address will not be published. Required fields are marked *