1

Let's say I have a source directory with the following directory structure:

+-- rootfs
|   +-- bin
|   |   +-- cat
|   |   +-- chmod
|   |   +-- cp
|   |   +-- ls
|   |   +-- echo
|   |   +-- grep
|   +-- usr
|   |   +-- bin
|   |   |   +-- a
|   |   |   +-- b
|   |   |   +-- c
|   +-- lib
|   |   +-- libc.so.6
|   |   +-- libssl.so.0.9.8
|   |   +-- libcrypto.so.1.0.0
|   +-- tmp
|   +-- mnt

where all the files in the second level are directories (bin, usr, lib, mnt, tmp)

I need to copy this directory to my destination. So, I do:

cp -r /path/to/source/rootfs /path/to/destination/

Now, let's say I copy two new files to this directory structure (in the destination) from elsewhere, so it now looks like

+-- rootfs
|   +-- bin
|   |   +-- cat
|   |   +-- chmod
|   |   +-- cp
|   |   +-- ls
|   |   +-- echo
|   |   +-- grep
|   |   +-- **mke2fs**
|   |   +-- **e2fsck**
|   +-- usr
|   |   +-- bin
|   |   |   +-- a
|   |   |   +-- b
|   |   |   +-- c
|   +-- lib
|   |   +-- libc.so.6
|   |   +-- libssl.so.0.9.8
|   |   +-- libcrypto.so.1.0.0
|   |   +-- **libm.so.6**
|   |   +-- **librt.so.1**
|   +-- tmp
|   +-- mnt

At this point, if I copy over the source directory to the destination directory again, the new files I created in the destination folder remain as it is.

cp -r /path/to/source/rootfs /path/to/destination

The modified times of all the files that are in the source directory have been updated to the time of the second copy, which makes sense.

The modified time for the bin directory is updated to the time of the second copy, whereas the modified time of the lib directory is not, which I didn't really get - since both those directories have new files.

This made me wonder - how does the linux copy command work, and how does it update the modified times for the folders?

1 Answer 1

2

The only reason why cp -r would change a directory mtime is that a file in the source directory is missing in the target directory.

You can check what happens with

cp -ruv /path/to/source/rootfs/. /path/to/destination

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.