A Quick Introduction to Unix/Copying Files

From Wikibooks, open books for an open world
Jump to: navigation, search

Copying Files[edit]

cp (copy)[edit]

To make a new copy of some file, say file1 in the current working directory with the name file2, you can use the command cp like this

cp file1 file2

Sometimes you will wish to copy a file across directories. There are two ways you could do this. First you might use cd to get to your destination directory.

% cd ~/mydirectory

Then at the Unix prompt, type,

% cp ~/science.txt .

Don't forget to type the dot . at the end of this first command line. Remember, in Unix, the dot means the current directory.

Now you can if you wish make copies of this file in this directory in the usual way, for example

% cp science.txt science.bak

Another way to achieve the same result is to use the full, absolute pathname - that is start at the root (/) and specify all the directories in the path for the original and all the directories in the path for the destination. You might have a command looking like

cp  /nfs/fs-i/UM0098/ccaajim/train.doc /nfs/fs-i/UM0098/ccaajim/myretiredfiles/train.doc.bak

Of course, it's easy to make mistakes typing long path names.

Copying a directory and files[edit]

You can use cp to copy a directory and the files it contains (including subdirectories and their files) to a new location. The command looks like

cp -r ~/training/linux/* ~/training/backup/linux

This makes a copy of the complete contents of the directory linux in backup/linux. The new, target, directory will be created for you.

Contents[edit]