Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why second time cp command is faster

Tags:

linux

shell

When I execute cp folder1 folder2 -rf,for the first time it takes around 10 mins. But when it I execute the second command cp folder1 folder3 -rf, it takes around 1 min. folder1 contains about 100 000 files.

Why is there run time improvement for the second time?

like image 210
quartz Avatar asked Aug 05 '13 10:08

quartz


People also ask

Which command is faster mv or cp?

Between drives, 'mv' should essentially amount to cp + rm (copy to destination, then delete from source). On the same filesystem, 'mv' doesn't actually copy the data, it just remaps the inode, so it is far faster than cp.

Why is cp slow?

When using a thumb drive with a persistent operating system, it happens that the persistent partition casper-rw gets corrupted, if one pulls out the thumb drive too early after shutting down the operating system.

Why rsync is faster than cp?

rsync is much faster than cp for this, because it will check file sizes and timestamps to see which ones need to be updated, and you can add more refinements. You can even make it do a checksum instead of the default 'quick check', although this will take longer.


2 Answers

This is because of page caching. Run sync ; echo 3 > /proc/sys/vm/drop_caches to make it slow again.

Further reading:

  • http://jim.studt.net/depository/index.php/flushing-caches-for-benchmarking-in-linux
  • https://superuser.com/a/319287/236874
like image 114
mnagel Avatar answered Sep 29 '22 18:09

mnagel


The first time the files are read from your hard drive.

The second time the files are read from memory.

Linux, as most operating systems, caches accessed files/blocks in memory.

like image 22
nos Avatar answered Sep 29 '22 18:09

nos