cp (copy files and directories)

The cp (“copy”) command is one of the first that Linux beginners learn:

cp SOURCE DEST

It copies the file SOURCE to DEST. If you want to copy a whole directory, use the -r switch (“recursive”).

Straightforward, isn't it? However, few people know that by copying files in this way, important file attributes may get lost. For example, you probably don't want your vacation photos to lose their timestamps (indicating when they were taken). If you copy files using cp without options, timestamps are discarded and the new files are stamped with the current date and time. If you want to avoid this default behavior of cp, try using the option -p (“preserve”). As the man page of cp tells us, this option is a shorthand for:

--preserve=mode,ownership,timestamps

It should be clear from this that you can preserve other file attributes as well. I find that -p is a reasonable choice in most cases. You may want to save the following →alias definition in your ~/.bashrc configuration file:

alias cp=`cp -p'

Or even better:

alias cp=`cp -ip'

The -i option (“interactive”) makes cp ask for permission before overwriting files. I keep this alias in my own .bashrc. It has saved me a lot of trouble.