UNIX Tutorial Two

2.1 Copying Files

cp (copy)

cp file1 file2  is the command which makes a copy of file1 in the current working directory and calls it file2

What we are going to do now is download a file from myCourses (look for the file named copyme.pdf under today's lesson in the Content section) into the directory ~/Desktop, and then use the cp command to copy it to your macstuff directory.

First, cd to your macstuff directory.

% cd ~/macstuff

Then at the UNIX prompt, type,

% cp ~/Desktop/copyme.pdf .

(Note: Don't forget the dot (.) at the end. Remember, in UNIX, the dot means the current directory.)

The above command means copy the file copyme.pdf  to the current directory, keeping the name the same. 

Exercise 2a

Make a copy of the copyme.pdf file and name it copiedme.pdf using the cp command, keeping both in the same directory.


2.2 Moving files

mv (move)

mv file1 file2 moves (or renames) file1 to file2

To move a file from one place to another, use the mv command. This has the effect of moving rather than copying the file, so you end up with only one file rather than two.

It can also be used to rename a file, by moving the file to the same directory, but giving it a different name.

We are now going to move the file copyme.pdf to your backup directory.

First, change directories to your macstuff directory (remember how?). Then, inside the macstuff directory, type

% mv copyme.pdf backups/

Type ls and ls backups to see if it worked.

2.3 Removing files and directories

rm (remove), rmdir (remove directory)

To delete (remove) a file, use the rm command. As an example, we are going to delete the copy of the copyme.pdf file in the ~/Desktop directory.

From inside your ~/Desktop directory, type

% rm copyme.pdf
% ls (to check if it has deleted the file)

You can use the rmdir command to remove a directory (make sure it is empty first). Try to remove the backups directory. You will not be able to since UNIX will not let you remove a non-empty directory.

Exercise 2b

Move all the files and directories out of the macstuff directory into the ~ directory.  Then remove macstuff  using the rmdir command.

2.4 Clearing the contents from the terminal screen

clear (clear screen)

You may want to occasionally clear the terminal window of the previous commands so the output of the next commands can be clearly seen.

At the prompt, type

% clear

This will clear all text and leave you with the % prompt at the top of the window.

Summary

cp file1 file2

copy file1 and call it file2

mv file1 file2

move or rename file1 to file2

rm file

remove a file

rmdir directory

remove a directory

man command

see the manual pages for command

clear

you guessed it!









This tutorial was modified from the original work by M.Stonebank@surrey.ac.uk, 19 October 2001 in accordance with the Creative Commons License, under which it is still protected.

Creative Commons License