Manipulating files
Overview
Command | Action |
---|---|
cp file1 file2 |
make a copy of file1 and call it file2 |
mv file1 file2 |
move or rename file1 to file2 |
rm file |
remove or delete a file |
rmdir directory |
remove or delete a directory |
cat file |
dump the contents of a file to the screen |
less file |
display a file 1 page at a time |
head file |
display the first 10 lines of a file |
tail file |
display the last 10 lines of a file |
grep 'keyword' file |
search a file for ‘keyword’ |
wc file |
count number of lines, words, or characters in file |
Setup
Very quickly, lets do the setup for these exercises. Open a terminal and type in the following:
1mkdir -p ~/unixstuff/backups
You are now ready to move on to Copying Files.
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 directory.
First, cd to your unixstuff
directory.
1% cd ~/unixstuff
Then at the UNIX prompt, type:
1% cp /etc/services .
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 stored.
Exercise 2a
Create a backup of your (copy of the) services file by copying it to a file called services.bak
Moving files
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:
1% mv services.bak backups/.
Type: ls
and 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:
1% cp services tempfile.txt
2% ls
3backups services tempfile.txt
4% rm tempfile.txt
5% ls
6backups services
7%
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.
Exercise 2b
Create a directory called tempstuff using mkdir(1), then remove it using the rmdir(1) command.
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:
1% clear
This will clear all text and leave you with the %
or $
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:
1% cat services
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 time.
less(1)
The less(1) command writes the contents of a file onto the screen one (1) page at a time. In your unixstuff directory, type:
1% less services
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).
head(1)
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 type:
1% head services
Then type:
1% head -5 services
Exercise 2c
What difference did the -5 do to the head command? With a default UNIX
termainal, would you ever want to use head -30
?
tail(1)
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:
1% tail services
Exercise 2d
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:
1% less services
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 ?
. Using 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:
1% grep http services
As you can see, grep has printed out each line from the file ‘services’ which contains the word http.
Or has it? Try typing:
1% grep Http services
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:
1% grep -i http services
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:
1% 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
1% 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:
1% wc -w services
To find out how many lines the file has, type:
1% wc -l services
Comments