LECTURE 2
Copying Files and Directories (cp)
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 unixstuff directory.
First, change to your unixstuff directory.
cd ~/unixstuff
Then at the shell prompt type:
cp /cm/shared/training/tutorial/[Link] .
Don't forget the dot (.) at the end. Remember, in UNIX, the dot means the current
directory. The above command means copy the file [Link] to the current
directory, keeping the name the same.
Directories can also be copied with the cp command, but it’s necessary to add the
option –R to do so. This option means ‘recursive’ and will copy the contents of the
directory as well as the directory itself, for example:
cp -R directory1 directory2
Try running
cp -R /cm/shared/training/tutorial ~/unixstuff
Exercise 2a
Create a backup of your [Link] file by copying it to a file called [Link]
Moving files and Directories (mv)
The move command has a variety of similar but subtly different uses. It can be used
to move a file to a different location (i.e. a different directory). It can also be used to
move multiple files to a different directory. It can also be used to rename a file or a
directory. For example:
mv file1 directory1/
6
This would move file1 from the current directory into directory1.
mv file1 file2 file3 directory1/
This would move file1, file2 and file3 from the current directory into directory1.
mv file1 file2
This would rename file1 as file2.
mv directory1/ directory2/
This would rename a directory. Finally,
what will this do? mv file1 directory/file2
This would move and rename a file in one step.
We are now going to move the file [Link] to your backup directory. First,
change directories to your unixstuff directory (can you remember how?). Then, inside
the unixstuff directory, type
mv [Link] backups/
To see if it worked type
ls
ls backups
Removing Files (rm) and Directories (rmdir)
To delete (remove) a file, use the rm command. As an example, we are going to
create a copy of the [Link] file then delete it.
Inside your unixstuff directory, type
cp [Link] [Link]
ls
rm [Link]
ls
In order to delete an empty directory you can use the command
rmdir directory
7
However this won't remove directories that already have files in them, instead you
can use
rm -r directory
to recursively delete files in directory (use sparingly - there is no Recycle bin!)
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 Linux will not let
you remove a non-empty directory.
Exercise 2b
Create a directory called tempstuff using mkdir, then remove it using the rmdir
command.
Displaying the contents of a file on the screen
clear (clear screen)
Before you start the next section, you may like to clear the terminal window of the
previous commands so the output of the following commands can be clearly
understood.
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 [Link]
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 a page at a time. Type
less [Link]
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 used in preference to cat for long files.
8
head
The head command writes the first ten lines of a file to the screen. First clear the
screen then type
head [Link]
Then type
head -5 [Link]
What difference did the -5 do to the head command?
tail
The tail command writes the last ten lines of a file to the screen. Clear the screen
and type
tail [Link]
How can you view the last 15 lines of the file?
Searching the contents of a file
Simple searching using less
Using less, you can search though a text file for a keyword (pattern). For example, to
search through [Link] for the word 'science', type
less [Link]
then, still in less (i.e. don't press q to quit), type a forward slash (/) followed by the
word to search for, e.g.
/science
As you can see, less finds and highlights the keyword. Type n to search for the next
occurrence of the word.
grep
grep is one of many standard UNIX utilities. It searches files for specified words or
patterns. First clear the screen, then type
grep science [Link]
University of Leicester | Tutorial Two 9
As you can see, grep has printed out each line that contains the word science. Or
has it?
Try typing
grep Science [Link]
The grep command is case sensitive; it distinguishes between Science and science.
To ignore upper/lower case distinctions, use the -i option, i.e. type
grep -i science [Link]
Often when there is a lot of text it is useful to highlight the matches (this is a default
setting on ALICE / SPECTRE now but may not be on other systems)
grep --color -i science [Link]
To search for a phrase or pattern, you must enclose it in single quotes (the
apostrophe symbol). For example to search for the phrase spinning top, type
grep -i 'spinning top' [Link]
Some of the other options of 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 science or
Science is
grep -ivc science [Link]
wc (word count)
A handy little utility is the wc command, short for word count. To do a word count on
[Link], type
wc -w [Link]
To find out how many lines the file has, type
wc -l [Link]
10
To find out how many characters the file has, type
wc -m [Link]
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
cat file Display or concatenate a file
less file display a file a page at a time
head file display the first few lines of a file
tail file display the last few lines of a file
grep 'keyword' file search a file for keywords
wc file count number of lines/words/characters in file