October 10, 2019
cp file1 file2 is the command which makes a copy of
file1 in the current working directory and calls it
What we are going to do now, is to take a file stored in an open access area of the file system, and use the
cp command to copy it to your
First, download the file
Downloads. Use 'File/Save As..' from the menu bar to save it.
Change your current folder to your
$ cd ~/class04
Then at the UNIX prompt, type,
$ cp ~/Downloads/science.txt .
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
science.txt to the current directory, keeping the name the same.
Create a backup of your
science.txt file by copying it to a file called
mv file1 file2 moves (or renames)
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
science.bak to your backup directory.
First, change directories to your
class04 directory. Then, inside the
class04 directory, type
$ mv science.bak backups/.
ls backups to see if it has worked.
clear (clear screen)
Before you start the next section, you may like to clear the window
At the prompt, type
This will clear all text and leave you with the
$ prompt at the top of the window.
cat can be used to display the contents of a file on the screen. Type:
$ cat science.txt
As you can see, the file is longer than than the size of the window, so it scrolls past making it unreadable.
less writes the contents of a file onto the screen one page at a time. Type
$ less science.txt
As you can see,
less is better than
cat for long files
head command writes the first ten lines of a file to the screen.
First clear the screen then type
$ head science.txt
$ head -5 science.txt
What difference did the
-5 do to the
tail command writes the last ten lines of a file to the screen.
Clear the screen and type
$ tail science.txt
Question. How can you view the last 15 lines of the file?
cp file1 file2
|copy file1 and call it file2
mv file1 file2
|move or rename file1 to file2
|show the contents of a file
|show a file a page at a time
|show the first few lines of a file
|show the last few lines of a file
This class is a derived work from http://www.ee.surrey.ac.uk/Teaching/Unix/
M.Stonebank@surrey.ac.uk, © 9th October 2000
Licensed under a Creative Commons License