cp file1 file2
|make a copy of file1 and call it file2
mv file1 file2
|move or rename file1 to file2
|remove or delete a file
|remove or delete a directory
|dump the contents of a file to the screen
|display a file 1 page at a time
|display the first 10 lines of a file
|display the last 10 lines of a file
grep 'keyword' file
|search a file for ‘keyword’
|count number of lines, words, or characters in file
Very quickly, lets do the setup for these exercises. Open a terminal and type in
mkdir -p ~/unixstuff/backups
You are now ready to move on to Copying Files.
cp(1) - copy
cp file1 file2 is the command which makes a copy of file1 in the current
working directory and calls it file2
Now what we are going to do, is to take a file stored in an open access area of
the file system, and use the cp(1) command to copy it to your unixstuff
First, cd to your
Then at the UNIX prompt, type:
Note: Don’t forget the dot ‘.’ at the end. Also, remember that in UNIX, the
dot means the current directory.
The copy command means ‘copy the file services to the current directory’,
keeping the name the same.
Note: The directory /etc/ is an area where system configuration files are
Create a backup of your (copy of the) services file by copying it to a file
mv(1) - move
mv file1 file2 moves (or renames) file1 to file2
Moving files is very similar to copying files. To move a file from one place to
another, use the mv(1) 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 services.bak to your backup directory.
First, change directories to your unixstuff directory (do you remember how?).
Then, once you are in your unixstuff directory, type:
% mv services.bak backups/.
ls backups to see if it has worked.
Removing files and directories
rm(1) - remove, rmdir(1) - remove directory
To remove (delete) a file, use the rm command. As an example, we are going to
create another copy of the services file (the one we have in our unixstuff
directory), and then delete it.
Inside your unixstuff directory, type:
% cp services tempfile.txt
backups services tempfile.txt
% rm tempfile.txt
You can use the rmdir(1) 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.
Create a directory called tempstuff using mkdir(1), then remove it using the
Displaying the contents of a file
clear(1) - clear screen
Before you start the next section, you may wish to
clear the terminal
window of the previous commands so the output of the following commands can be
(more) clearly understood.
At the prompt, type:
This will clear all text and leave you with the
$ prompt at the
top of the window.
cat(1) - concatenate
The command cat(1) can be used to display the contents of a file on the screen.
In your unixstuff directory, type:
As you can see, the file is longer than than the height of the window, so it
scrolls past making most of the file unreadable. Some terminals offer the
ability to scroll up and down, but a better solution that will work everywhere
is to use a pagination tool (a tool which displays content one (1) page at a
The less(1) command writes the contents of a file onto the screen one (1) page
at a time. In your unixstuff directory, type:
Press the [space-bar] if you want to see another page, and type
q if you
want to quit reading. As you can see, less is used in preference to cat for long
files (the typical, and default size of a UNIX terminal is 80x25. That means 80 characters wide, and 25 lines tall. If your file is more than 25 lines long, it won’t all fit on the screen at one).
The head(1) command writes the first ten (10) lines of a file to the screen.
Change directories to your unixstuff directory, then clear the screen. Now
What difference did the -5 do to the head command? With a default UNIX
termainal, would you ever want to use
The tail(1) command writes the last ten (10) lines of a file to the screen.
In your unixstuff directory, clear the screen and type:
How can you view the last 15 lines of the file?
Searching the contents of a file
Simple searches using less
Using less, you can search though a text file for a keyword (pattern). For
example, to search through the services file for the word ‘http’, change
directories to your unixstuff directory and type:
then, with out exiting less, type a forward slash
/ followed by the word
you wish to search for (in this example:
/http). This will search forwards through the file.
As you can see, less finds and highlights the keyword. Type
n to search
for the next occurrence of the word. If you wish to search backwards through the
file, instead of using a
/, use a
n to move to the
next instance of the keyword works the same way.
grep(1) - don’t ask why it is called grep
grep(1) is one of many standard UNIX utilities. It searches files for specified
words or patterns. In your unixstuff directory, clear the screen and then type:
As you can see, grep has printed out each line from the file ‘services’ which
contains the word http.
Or has it? Try typing:
The grep(1) command is case sensitive; it distinguishes between Http and http
(or even HTTP).
To ignore upper/lower case distinctions, use the
-i option, i.e. type:
To search for a phrase or pattern, you must enclose it in single quotes (the
apostrophe symbol). For example to search for ‘http protocol’, type:
% grep -i 'http protocol' services
Some of the other commonly used options for grep are:
- -v display those lines that do NOT match
- -n precede each matching line with the line number
- -c print only the total count of matched lines
Try some of them and see the different results. Don’t forget, you can use more
than one option at a time. For example, the number of lines without the words
http or Http is
% grep -ivc http services
wc(1) - word count
A handy little utility is the wc(1) command, short for word count. To do a word
count on services, type:
To find out how many lines the file has, type: