How to copy files or directories from command line in linux ?

For copying files and directories from command line we use cp command. The Syntax for cp command is as follows


cp source destination
cp [options] sourcefile destinationfile
cp [options] sourcefile /path/to/destination_directory
cp [options] /path/to/source_directory /path/to/destination_directory


Examples

To copy a file README.txt from current  directory to a directory named Documents located in my home directory
cp README.txt /home/calypso/Documents


To copy a file README.txt into a new file ReadMe27022014.txt in the same directory, type
cp README.txt ReadMe27022014.txt


To create a backup copy of /etc/resolv.conf configuration file in the home directory, type
cp /etc/resolv.conf /home/calypso/resolv.conf.bak


To copy a directory cp command should be used along with -r  (recursive) option, else cp command will omit the directory. For example to copy a directory named work to another directory Documents located in my home directory, type
cp -r work/ /home/calypso/Documents/



Some Important options: 

  • -f (force) : if an existing destination file cannot be opened, remove it and try again 
  • -i (interactive) : prompt before overwrite
  • -p (preserve) : preserves the mode, ownership and timestamps of the source file in the destination file
  • -v (verbose) : Explains what is being done

For the complete list of options see the cp command's man page



Comments

Popular posts from this blog

Understanding awk command with examples

Understanding sed command with example -Part 1

How to install a Software in Linux