October 10, 2019

From the last class

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 to take a file stored in an open access area of the file system, and use the cp command to copy it to your class04 directory.

First, download the file science.txt into Downloads. Use 'File/Save As..' from the menu bar to save it.

cp (copy)

Change your current folder to your class04 directory.

$ 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.

Exercise

Create a backup of your science.txt file by copying it to a file called science.bak

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 science.bak to your backup directory.

mv (move)

First, change directories to your class04 directory. Then, inside the class04 directory, type

$ mv science.bak backups/.

Type ls and ls backups to see if it has worked.

Displaying the contents of a file on the screen

clear (clear screen)

Before you start the next section, you may like to clear the window

At the prompt, type

$ clear

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

cat (concatenate)

The command 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

The command less writes the contents of a file onto the screen one page at a time. Type

$ less science.txt
  • Press the [space-bar] if you want to see another page
  • Type [q] if you want to quit reading

As you can see, less is better than cat for long files

head

tail

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?

Summary

Command Meaning
cp file1 file2 copy file1 and call it file2
mv file1 file2 move or rename file1 to file2
cat file show the contents of a file
less file show a file a page at a time
head file show the first few lines of a file
tail file show the last few lines of a file

References