LINUX Tutorial Two

2.1 Copying Files

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 the file me.txt which you created previously, and use the cp command to copy it to your linuxstuff directory.

First, cd to your linuxstuff directory.

# cd /linuxstuff

Then at the LINUX prompt, type,

# cp /me.txt .

(Note: Don't forget the dot (.) at the end. Remember, in LINUX the dot means the current directory.)

The above command means copy the file me.txt to the current directory, keeping the name the same.

Exercise 2a

Create a backup of your me.txt file by copying it to a file called me.bak in the same directory.

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

First, change directories to your linuxstuff directory if you are not already there. Then, inside the linuxstuff directory, type

# mv me.bak backups/

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

Suppose you want to rename the file me.txt to my_name.txt. This can be done by typing

# mv me.txt my_name.txt

Type ls to see if it has worked.

2.3 Removing files and directories

rm (remove), rmdir (remove directory)

To delete (remove) a file, use the rm command. As an example, we are going to create a copy of the me.txt file then delete it.

Inside your linuxstuff directory, type

# cp me.txt tempfile.txt
# ls (to check if it has created the file)
# rm tempfile.txt
# ls (to check if it has deleted the file)

You can use the rmdir command to remove a directory (but only if 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.

2.4 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 me.txt

But what if the file was longer than than the size of the window? In this case the contents of the file scrolls past making it unreadable. To see let's look at the yum.log file, which contains information about programs installed or removed using the program 'yum'. To see this type:

# cat /var/log/yum.log

more

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

# more /var/log/yum.log

Press the [space-bar] if you want to see another page, type q if you want to quit reading. As you can see, more is used in preference to cat for long files.

head

The head command writes the first ten lines of a file to the screen.

First clear the screen then type

# head /var/log/yum.log

The command head -n will write the first n lines of a file to the screen. For example, to write the first 20 lines of the file /var/log/yum.log type

# head -20 /var/log/yum.log

tail

The tail command writes the last ten lines of a file to the screen.

Clear the screen and type

# tail /var/log/yum.log

Like the head command, the -n option can also be specified.

2.5 Searching the contents of a file

grep

grep is one of many standard LINUX utilities. It searches files for specified words or patterns. First clear the screen, then type

# grep firewall /var/log/yum.log

As you can see, grep has printed out each line containing the word firewall.

Now try typing

# grep Firewall /var/log/yum.log

Note that the grep command is case sensitive; it distinguishes between firewall and Firewall.

To ignore upper/lower case distinctions, use the -i option, i.e. type

# grep -i firewall /var/log/yum.log

To search for a phrase or pattern, you must enclose it in single quotes (the apostrophe symbol). For example to search for Erased: firewall, type

# grep -i 'Erased: firewall' /var/log/yum.log

Some of the other options of grep are:

-v display those lines that do NOT match
-n precede each maching 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 with the word 'firewall' (ignoring case) is

# grep -ic firewall /var/log/yum.log

Exercise 2c

What are the line numbers of the log file where the word firewall occurs?

wc (word count)

A handy little utility is the wc command, short for word count. To do a word count on /var/log/yum.log, type

# wc -w /var/log/yum.log

To find out how many lines the file has, type

# wc -l /var/log/yum.log

2.6 Editing the contents of a file

vi

The text editor vi is an extremely powerful and popular terminal-based text editor. The command vi file will open a file using the vi text editor. If the file does not exist, then invoking vi can be used to create a new file. For example, type

# vi test.txt

to create a file named test.txt for editing. Note that whenever you open a file, you will be in command mode (see below).

Note that terminal text editors are much different than graphical ones, and will take some getting used to. The vi text editor has two modes:

Insert mode allows the user to insert (type) content into the file. To enter insert mode, type the letter 'i'. When in insert mode, you will see the word INSERT on the bottom left corner of the screen. When in this mode you can type normally to add content to the file, and use the arrow keys to move the cursor.

Command mode allows the user to enter special commands for navigating and editing the file. When in insert mode, hit the ESCAPE key to switch to command mode. Command mode allows you to use various commands, some of which are provided below.

CategoryCommandDescription
Saving/Exiting :w saves (writes) the file. You can type :w name to save the file as name.
:q quits the text editor. Note: if changes have been made, you will need to use :q! to quit without saving
:wq save and quit
Movement w move to the beginning of the next word
e move to the end of the next word
b move back to the beginning of the current word
:n move to line number n of the file
$ move to the end of the current line
0 move to the start of the current line
[Shift] g move to the last line of the file
Editing x delete the character under the cursor
dw delete all characters from the cursor to the end of the word
yy copy the current line to the clipboard
p paste the clipboard content
dd delete the current line
r replace the character under the cursor with the next character typed
u undo the last change
[Ctrl] r redo the last change
Find/Replace /string searches for 'string' in the file; type n to find the next matching string or [Shift] n to find the previous match
:%s/string1/string2/g replaces all occurences of string1 with string2

Note that many of the 'movement' and 'editing' commands can be automatically repeated multiple times by preceeding the command with a number. For example, typing 5dd will delete the 5 lines beginning with the current one. Similarly, 10x will delete the 10 characters beginning with the current one; typing 5 and then the right arrow key will move the cursor 5 characters to the right.

Exercise 2d

First, type cd / to ensure that you are in the topmost directory. Using vi, create a file named test.txt that contains the following:

This is a test.
I am using vi in CSC 343.

Save your changes, then edit the first line to say This is an exercise. Switch to command mode, go directly to the beginning of the second line. Then search for vi, move to this word, and add the words 'the text editor' before it. Skip a line, type your name, then copy and paste your name 10 times by switching to command mode, and typing yy on the line with your name, to copy it, and typing 10pp to then paste your name 10 times. Save your file.

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 a file
more 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
vi file open a file in the vi text editor

 

Last Modified: January 2019