I am trying to understand what the -a option does exactly with the cp command? The man pages on my system does not give much information about this.
2 Answers
According to the man page, cp -a is the same as cp --no-dereference --preserve=mode,ownership,timestamps,context,links,xattr
--no-dereference: copy symbolic link rather than what it points to--preserve=mode: copy the permission bits--preserve=ownership: copy the owner and group (if possible)--preserve=timestamps: copy the mtime and atime (ctime can't be copied)--preserve=context: copy SELinux or SMACK context attributes--preserve=xattr: copy the extended attributes
The answer by user10489 is correct. But it is possible to approach this question from the different angle. To ask "What function does it make cp perform?"
Here we are actually hinted quite a bit by the fact that -a is the short-hand for --archive, and that is exactly when you will use this the most. The last time i needed to use this option was when i was changing the distribution, while simultaneously reformatting the drive. I wanted to preserve most of the files in my home directory, but if you let the regular cp --recursive run, it is possible that some of the information that is stored in the file system itself will not be copied. I didn't care all that much about timestamps (although it was great that they were preserved as well), but the fact that symbolic links were copied as is, rather than following them is great. The ownership information is also very important, specially if you are backing up your home directory as root or some user that is automatically created during the LiveCD launch.
Keep in mind that it is essential to use -a when you "archive" and when you are restoring your files. Otherwise, during the restoration you can potentially lose what you have saved originally.