Copying Files ( Part 1 )

Creating copies of files can be useful for numerous reasons:
  • If a copy of a file is created before changes are made, then it is possible to revert back to the original.
  • It can be used to transfer a file to removable media devices.
  • A copy of an existing document can be used as a template for a new document.
cp [OPTIONS] SOURCE DESTINATION
Follow Along
Use the following command to switch to the Documents directory:
sysadmin@localhost:~$ cd ~/Documents
​⁠​‌‌⁠⁠⁠⁠​
The cp command is used to copy files. Similar to the mv command it requires at least two arguments: a source and a destination. For example, to copy the /etc/passwd file to the current directory, use the following command:
sysadmin@localhost:~/Documents$ cp /etc/passwd .                                        
Note
The second argument is the . character. Recall from the Changing Directories section that is a shortcut which represents the current directory.
The result of executing the previous command would create a copy of the contents of the /etc/passwd file in the Documents directory, since that is our current directory. This can be confirmed using the lscommand:
sysadmin@localhost:~/Documents$ ls
School           alpha-second.txt  letters.txt   os.csv       zoo.txt
Work             alpha-third.txt   linux.txt     passwd
adjectives.txt   hello.sh          longfile.txt  profile.txt
alpha-first.txt  hidden.txt        newhome.txt   red.txt
Consider This
Permissions can have an impact on file management commands, such as the cpcommand. In order to copy a file, it is necessary to have execute permission to access the directory where the file is located and the read permission for the file being copied.
It is also necessary to have write and execute permission on the directory the file is being copied to. Typically, there are two places where you should always have write and execute permission on the directory i.e home directory and the /tmp directory.

Comments