What does the -p option do exactly with the cp command?

17,532

enter image description hereAssume the file1 in your example was created yesterday. If you do not use the -p option, the creation date for file2 will be today and right now. If you use -p option, file2 will look like it was created yesterday. And that is just for the timestamp. If you are copying the file as someone other than the creator of the file, it will assume your identity as an owner if -p option is not there.

Please see the screen capture attached.

Share:
17,532

Related videos on Youtube

Mohamed Medhat Sallam
Author by

Mohamed Medhat Sallam

Linux for me is a to enjoy life. Not just a kernel. echo "Thank you Linus trovalds"

Updated on September 18, 2022

Comments

  • Mohamed Medhat Sallam
    Mohamed Medhat Sallam almost 2 years

    So I am trying to understand what is the significance of the -p option for the cp command.

    I opened the manual of the cp command and it says -p same as --preserve=mode,ownership,time stamps.

    But what is that supposed to mean>

    Like I know what is ownership (Who created the file) and I know timestamps(when was the file last modified or touched) and I also know the mode( permission like 777 ).

    But what is the difference between for instance:

    cp file1 file2

    and

    cp -p file1 file2

    I am supposed to create a c program that implement cp -i -p file1 file2

    I know -i basically will prompt the user if file2 was already created and is about to be overwritten but for -p I have no clue.

  • Marius
    Marius over 8 years
    Preserving ownership works only if you happen to be running as root.