cp losing file's metadata

7,175

Solution 1

If you use man cp to read the manual page for the copy command you'll find the -p and --preserve flags.

-p same as --preserve=mode,ownership,timestamps

and

--preserve[=ATTR_LIST] preserve the specified attributes (default: mode,ownership,timestamps), if possible additional attributes: context, links, xattr, all

What this boils down to is that you should use cp -p instead of just cp.

Solution 2

I think there are a couple of observations worth pointing out.

You say that you're not sure what distribution you're using, but in your update you included output that identified your distribution as being BusyBox v1.19.3. Looking at the Developer Guide for the Asustor NAS confirms that the OS in question is BusyBox.

The answer posted by @roaima quotes the man page for the cp command. This makes sense in general, but maybe not in this case.

BusyBox is a minimalistic Linux OS designed for embedded devices and it's a little bit of a different environment than the more popular Linux flavors that most people are probably used to. In particular there are probably no man pages on your NAS and the commands may have slightly different features, behavior, and syntax. Here is the documentation for BusyBox:

And here is the entry for the cp command on BusyBox:

cp

cp [OPTIONS] SOURCE DEST

Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY

Options:

        -a      Same as -dpR
        -d,-P   Preserve links
        -H,-L   Dereference all symlinks (default)
        -p      Preserve file attributes if possible
        -f      Force overwrite
        -i      Prompt before overwrite
        -R,-r   Recurse
        -l,-s   Create (sym)links

We can see that the -p option flag is still there and has the same semantics as it usually does, although it lacks the more flexible syntax available on more full-featured distributions:

--preserve[=ATTR_LIST]

This is consistent with the usage message that you got when trying to run the cp command with the --preserve flag.

Another option to try, since you say that the -p flag isn't working, would be to copy the file over without the -p flag and then use the touch command to update the modification time, e.g.:

root@busybox:~# cp original_file new_file
root@busybox:~# touch -r original_file new_file

If this doesn't work then you might want to update your question to include the error message that you get. You might also want to update your question to include the properties of the files you're trying to copy, the locations you're trying to copy them from and to, and the user that you're running, the exact commands that you're using, and the user you're logged in as when running these commands.

That said, I suspect that your cp -p command might be generating a permissions-related error because you're copying across mounted volumes and the file owner exists on one volume but not the other (or something similar to that).

Share:
7,175

Related videos on Youtube

Hikari
Author by

Hikari

Updated on September 18, 2022

Comments

  • Hikari
    Hikari almost 2 years

    I have an Asustor NAS that runs on Linux; I don't know what distro they use.

    I'm able to log in it using SSH and use all Shell commands. Internal Volume uses ext2, and external USB HDs use NTFS.

    When I try to use cp command to copy any file around, that file's date metadata is changed to current datetime.

    In example, if I use Windows to copy the file from SMB and the file was modified in 2007, the new file is marked as created now in 2017 but modified in 2007. But with Linux cp command its modified date is changed to 2017 too.

    This modified date is very relevant to me because it allows me to sort files on Windows Explore by their modified date. If it's overridden, I'm unable to sort and they all seem to have been created now. I also use modified date to know when I acquired some rare old files.

    Is there any parameter I can use in cp command to preserve original file metadata?

    Update: I tried cp --preserve=timestamps but it didn't work, it printed:

    cp: unrecognized option '--preserve=timestamps'
    BusyBox v1.19.3 (2017-03-22 17:23:49 CST) multi-call binary.
    
    Usage: cp [OPTIONS] SOURCE DEST
    
    Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY
    
            -a      Same as -dpR
            -R,-r   Recurse
            -d,-P   Preserve symlinks (default if -R)
            -L      Follow all symlinks
            -H      Follow symlinks on command line
            -p      Preserve file attributes if possible
            -f      Overwrite
            -i      Prompt before overwrite
            -l,-s   Create (sym)links
    

    If I try just -p it says cp: can't preserve permissions of '...': Operation not permitted, but as far as I've tested, timestamps are being preserved.

    • Ignacio Vazquez-Abrams
      Ignacio Vazquez-Abrams over 6 years
      Other than --preserve?
    • Hikari
      Hikari over 6 years
      Resolution: -p works. The error message is probably related to me copying files from internal ext4 HDs to external NTFS HDs. Anyway I don't need to preserve permissions, only timestamps, and in all tests I did timestamps are being properly preserved. I just ignore the permissions error messages and run md5 to assure files' integrity. Basicly I use cp -pR.
    • Ignacio Vazquez-Abrams
      Ignacio Vazquez-Abrams over 6 years
      rsync -rltDvu --modify-window=1 will do the integrity verification for you.
  • Basile Starynkevitch
    Basile Starynkevitch over 6 years
    sometimes cp -a is useful too...
  • roaima
    roaima over 6 years
    Yes. I use -a (sometimes with -l) for recursive copying, and -p almost always otherwise. @BasileStarynkevitch
  • Barmar
    Barmar over 6 years
    Note that timestamps preserves mtime and atime, but AFAIK it's not possible to preserve ctime.
  • Hikari
    Hikari over 6 years
    Sorry for the late reply. It didn't work for me. Updated the question.
  • Hikari
    Hikari over 6 years
    Thanks a lot for the detailed info. I didn't know about BusyBox distro, so I didn't notice it. Good to know what distro I'm running. I added a comment to my question explaining the resolution.