Download as pdf or txt
Download as pdf or txt
You are on page 1of 29

Laboratory Exercise 1

Linux Basics
Objectives:
At the end of the session, the students are expected to:
 become familiar with a Linux system
 learn the correct usage of basic Linux commands

Materials:

 1 PC with Linux

Procedures:

Ask your instructor or lab facilitator to assist you in logging in a PC with Linux installed.

1. LINUX COMMAND FORMAT

Most Linux commands have the following format:

commandname [options] [arguments]

A user types in the name of the command he wants to execute followed by options (options are
generally preceded by a dash or minus "-" sign) and then by the arguments. Options tell Linux
how the command will be executed while arguments are the data to be processed by the
command.

Some commands do not require the use of options and/or arguments. Examples of such are
the passwd and clear commands.

Try out the clear command. It should clear the screen.

2. THE ls (LIST) COMMAND

The ls (list) command lists the name of files in a given directory. Typing ls without any option
and argument at the prompt gives the user a listing of the files in columns and in alphabetical
order.

Example:

$ ls ↵
arithmetic chapter1.doc eric.let
figures memo.to.henry report.1
sample.c test.program.c

Linux Basics
Page 1 of 1
Try out the ls command and take note of the files found in the current working directory

Page 2 of 11
Some options and arguments of the ls command:

a. The l option gives a long listing of the files that includes information such as the size of the
file, the owner of the file, the date and time the file was last modified, etc.

Example:

$ ls -l ↵
-r-xr-xr-x 1 mitch staff 4222 Jan 13 11:10 arithmetic
-rw-r----- 1 mitch staff 512 Dec 1 8:00 chapter1.doc
-rw-rw-r-- 1 mitch staff 1020 Feb 9 10:15 eric.let
drwx------ 2 mitch staff 2023 Jun 4 9:31 figures
. . . . .
. . . . .
. . . . .

Explanation of the ls -l Output

Column Explanation

First Security Access Modes


Second Number of Links
Third Owner of the File or Directory
Fourth Group where the Owner belongs
Fifth Size of the File
Sixth Date the file was last modified
Seventh Time the file was last modified
Eight Name of the File or Directory

The security access mode (the first column) consists of four components:

First Character A “d” means this is a directory, while a "-" means this is
just a regular file (text or executable file).
Next Three The Security Access Mode or permissions for the owner
of the file or directory.
Next Three The Security Access Mode or permissions for the group
mates of the owner.
Last Three The Security Access Mode or permissions for the other
users in the system that do not belong to the owner's
group.

Types of permissions:

r Read Permission. The r permission means that a file can


be viewed and copied. If it is a directory, the contents of
the directory can be listed using the ls command.
w Write Permission. The w permission means that the
contents of the file can be modified. If it is a directory,
files can be added or deleted.
x Execute Permission. The x permission means the file can
be executed (if it is an executable file).

Linux Basic Page 3 of 11


Examples:

-rwxr----x The first character is a -, so this refers to a file (not a


directory). The next three characters (rwx) indicate that
the owner of the file has read, write, and execute
permissions. The next three characters (r--) indicate that
the group mates of the owner only have the read
permission. The last three characters (--x) indicate that
other users of the system only have the execute
permission.

Try out the ls -l command and determine the security access mode of the
different files and directories.

b. The a option gives a listing of all the files including hidden files (those which start with a ".")

Example:

$ ls -a ↵
. .. .profile
.sh_history arithmetic chapter1.doc
eric.let figures memo.to.henry
report.1 sample.c test.program.c

In some Linux versions, the ls command will list the files one line per file. In cases like
this, the C option may be used to list the files by columns.

Try out the ls -a command and determine the hidden files.

c. The ls command can also have arguments.

Example:

$ ls arithmetic
↵ arithmetic

$ ls *.c ↵
sample.c test.program.c

Try out the ls command for a specific file or group of files.

d. The ls command can also have a combination of arguments and options.

$ ls -l sample.c ↵
-rw-r--r-- 2 mitch staff 20432 Oct 25 10:30 sample.c

Linux Basics Page 4 of 11


Try out the ls -l command for a specific file or group of files.

Linux Basics Page 5 of 11


3. COMMAND USAGE PRINCIPLES

Separation of Components

A space should separate each component of a Linux command.

Example:

$ ls -l sample.c ↵

Ordering of Components

Do not interchange the position of the components of a Linux command. The order should
always be:

commandname [options] [arguments]

Example:

$ ls -l eric.let ↵

Multiple Options

The rule of separation can be applied in case of multiple options. A dash "-" symbol should
precede an option and a space should separate each option.

Example:

$ ls -a -l ↵

This will give you a long listing of all the files.

Try out the ls -a -l command.

A user may also combine multiple options in a single dash symbol.

Example:

$ ls -al ↵

Take note that there should be no space between the a and the l.

Try out the ls -al command.

Also, the ordering of the options is not important. Therefore, ls -l -a and ls -la will also work.

Try out the ls -l -a and ls -la commands.

Linux Basics Page 6 of 11


Multiple Arguments

The rule of separation is also applicable in case of multiple arguments. A space separates one
argument from another.

Example:

$ ls -l arithmetic eric.let sample.c ↵

Try out the ls -l command on two specific files.

Multiple Commands

A user may enter several commands at a single Linux prompt. In such a case, a semi-colon
(;) delimits each command unit. The commands will then be executed consecutively.

Example:

$ ls; ls -a; ls -l ↵

Type ls; ls -a; ls -l at the command prompt and see what happens.

4. THE man (MANUAL) COMMAND

The man command can be used by users to get more information about the different commands
in Linux. It displays information from the online Linux reference manuals, also known as man
pages. These online manual pages provide detailed descriptions of command syntax and their
available option(s).

The syntax for this command is man [commandname]

Example: To get more information about the ls command:


$ man ls ↵

Once the information is displayed, the following keys may be used to move through the man
pages:

Spacebar shows the next screen of man page


Return scrolls the next line of man page
b scroll back one screen
f scroll forward one screen
q exits the man page
/pattern searches forward of this pattern
n finds the next occurrence of the pattern
h displays help menu

Don’t forget to press q to exit the man page and return to the command prompt.

Linux Basics Page 7 of 11


Try out the man command to get more information about the ls command. Then use the
different keys to move through the man pages.

Try out the man command to get more information about the date command. After learning
what this instruction does, experiment with it.

Try out the man command to get more information about the cal command. After learning
what this instruction does, experiment with it.

5. THE vi TEXT EDITOR OF UNIX

The vi (visual) text editor of UNIX is for creating and editing text files such as letters, memos,
reports, correspondences, and the like. vi is a screen-oriented text editor.

There are two modes of operations in vi.

1. The text-edit mode is where the user can enter or edit text. When in the text-edit mode,
each time the user types a letter, that letter will enter the document the user is editing.

2. The command mode is where the user issues commands to the editor. In this mode, a letter
corresponds to a command and not a character to be entered in the text file.

For example, in the command mode, the letter h is for moving the cursor one position to
the left. When the user types the letter h while in the text-edit mode, it enters the file.
Therefore, the user has to be in the command mode in order to perform functions such as
moving the cursor (moving around the document), deleting characters, words, or lines, and
saving the document.

The format of vi:

vi filename(s)

Example:

$ vi sample.text↵

If the file exists, the contents of the file will be displayed. If the file does not exist, then Linux
will create a new file and the following screen will appear.

Linux Basics Page 8 of 11


~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
“sample.text” [new file]

The tilde “~” character indicates unused lines. The last line on the screen is used by the editor
for communicating information to the user.

Upon entering the vi editor, the user is automatically taken into the command mode. At this
point, the keys on the keyboard represent commands, that is, any character the user types will
not enter the file as text but will cause the execution of a certain action.

The cursor in vi is represented by an underscore (underline). In some Linux versions, the cursor
is seen as a box. Once Linux starts the vi text editor, it places the cursor at the start of the first
character of the document.

As the user starts editing the file, the changes are not actually made on the file itself. Every time
vi starts, it copies the file from the disk and into main memory (edit buffer). All editing changes
will be made in the edit buffer. The file on the disk remains intact. Changes on the actual
document will be made once the user saves the document. In this way, the user may choose
to abandon the changes and leave the document as it was.

MOVING IN vi

In the command mode, the user may move the cursor anywhere in the document by pressing
any of the following keys (no need to press the Enter key):

Command Action

h or <BACKSPACE> move 1 position to the left


l or <SPACEBAR> move 1 position to the right
j move one line down
k move one line up
w move one word to the right
b move one word to the left
^ move at the beginning of a line
$ move at the end of the line

Linux Basics Page 9 of 11


# Shift + g move to line #
Ctrl-F move forward 1 full page
Ctrl-B move backward 1 full page
Shift + g move to last line of the document
Ctrl-D move forward 1/2 page
Ctrl-U move backward 1/2 page
- move to the first character of the preceding line
Shift + h move to top of the screen
Shift + m move to the middle of screen
Shift + l move to bottom of the screen

If the user types a number before certain commands, it will cause the cursor to move several
units. Examples are:

1. Pressing 5h will cause the cursor to move 5 options to the left.

2. Pressing 10k will cause the cursor to move 10 lines up.

3. Pressing 3w will cause the cursor to move 3 words to the right.

DELETING TEXT IN vi

While in the command mode, the user may also delete characters, words, or lines. Just move
the cursor to the character, word, or line to be deleted and then press the corresponding keys:

Command Action

x delete one character

dw delete a word

dd delete a line

D delete from the current cursor position to the end of line

If the user types a number before any of these commands, it will cause the deletion of several
units. Examples are:

1. Pressing 12x will delete the next 12 characters.

2. Pressing 7dw will delete the next 7 words.

3. Pressing 6dd will delete the next 6 lines.

EDITING TEXT IN vi

Command Action

i insert new text before the cursor position


Shift + i insert new text at start of the current line
a append new text after the cursor position
Shift + a append new text at end of current line
o open a new line after the current line

Linux Basics Page 10 of 11


Shift + o open a new line before the cursor position
r change a character (no need to press <ESCAPE> key afterwards
Shift + r overwrite everything until the <ESCAPE> key is pressed
cw change word
Shift + c change all text from current cursor position
~ convert from upper to lower case and vice versa
u undo the last editing change
shift + u undo changes in a line
. repeat the last editing change
shift + j join two lines

Note: Press the ESC key after making changes.

EXITING vi

To exit the vi editor, just simple type a colon ‘:’ (the user should be in the command mode). A
colon will appear at the bottom of the screen. At this point, the user can type any of the following
(followed by the Enter key):

Command Action

q quit vi

q! quit vi without saving changes

wq save changes and then quit vi

x same as wq

Use vi to create a new file and then type the first two pages of this experiment. Since this is
a plain text file, do not worry of indentation, bullets, etc. Just copy the text. Experiment with
the different vi commands as you go along.

Linux Basics Page 11 of 11


Operating Systems

Laboratory Exercise 2
Linux Files and Directories
Objectives:
At the end of the session, the students are expected to:
 learn the Linux commands for manipulating files and directories

Materials:

 1 PC with Linux

Procedures:

Ask your instructor or lab facilitator to assist you in logging in a PC with Linux installed.

1. LINUX FILE AND DIRECTORY NAMING CONVENTIONS

Linux filenames can have a maximum of 255 characters consisting of letters, numbers,
periods, dashes, and/or underscores.

Linux filenames do not follow the concept of file extensions as in Windows. In other words, the
period is just like an ordinary character.

Example:

letters.to.me and c.prog.mitch are valid UNIX filenames

Linux filenames are case sensitive unlike in Windows

Example:

payroll.1994, PAYROLL.1994, and Payroll.1994 are all different filenames in Linux.

Special characters such as slash (/), asterisk (*), ampersand (&), pipe (|), quote (“ “), and dollar
sign ($) should not be used.

2. LINUX FILE SYSTEM

The Linux File System organizes all files into a hierarchy of directories just like in Windows.
The highest directory level is called the root (/). Take note that Windows represents its own
root directory by a backward slash (\).

The root directory contains numerous subdirectories, many of which are standard in Linux
systems. Some of Linux’s important subdirectories are:

Linux Files and Directories Page 1 of 9


Operating Systems

− bin. This contains the executable programs that make up most of the important Linux
system commands

− etc. This contains important system administration programs and data files

− lib. This contains some of the libraries that programming language compilers use

− dev. This contains Linux device files

− tmp. This contains temporary files to hold data for a short period of time

− usr. This contains files of general use to the users of a Linux system

Sample Directory Structure

bin dev usr lib etc

mitch cynthia

docs letters

mydoc letter1 letter2

In this directory structure, the subdirectories bin, dev, usr, lib, and etc are under the root
directory. Under usr, there are two subdirectories called mitch and cynthia. Under mitch,
there are two subdirectories called docs and letters. There are two files in subdirectory letters
namely letter1 and letter2. The file mydoc is in subdirectory docs.

3. PATHS

A file could be in any directory and its location is called its path. A path is a chain of directory
names that tell UNIX how to find a particular file

Linux Files and Directories Page 2 of 9


Operating Systems

An Absolute Path traces the location of the file from the root directory

Examples:

/usr/mitch/docs/mydoc

/usr/mitch/letters/letter1

A Relative Path traces the location of the file starting at the current working directory

Examples:

Assuming that the current working directory is docs:

../letters/letter1

../letters/letter2

3. FILE / DIRECTORY COMMANDS

pwd (print working directory) command

Everytime a user logs-in into an UNIX system, he is automatically taken to his home directory
(for example, /usr/mitch). However, a user can move to any directory in the file system as long
as he has access to it. In order for the user to determine which directory he is currently in, the
command to use is pwd.

Format:

pwd

Example:

$ pwd
/usr/mitch

Use the pwd instruction to determine your current working directory.

cd (change directory) command

If a user wants to move from one directory to another, he should use the cd command.

Format:

cd directory_name

Where:

Linux Files and Directories Page 3 of 9


Operating Systems

cd is the name of the change directory command

directory_name is the name of the directory the user wants to move


into. Absolute and relative path names may be used.

Note: Typing cd alone will always take the user to his home directory

Examples:

1. If the user is currently in /usr/rikki and wants to go to the root directory (/), then

$ cd / or $ cd ../..

2. If the user is currently at /usr/mitch and wants to move to /usr, then

$ cd /usr or $ cd ..

3. If the user is currently in /usr/mitch and wants to move to /usr/rikki, then

$ cd /usr/rikki or $ cd ../rikki

4. If the user is currently at /usr/mitch and wants to go to /usr/mitch/letters, then

$ cd /usr/mitch/letters or $ cd letters

5. If the user is currently in /usr/mitch and wants to go to /bin, then

$ cd /bin or $ cd ../../bin

Use the cd / command to get to the root directory. From there, use the ls -l command to
determine the different directories in the root directory. Then keep on using the cd and
the ls -l commands to move around the directories. Once you are comfortable with the
directory structure, use the cd command to go back to your home directory. Use pwd
to make sure you are in your home directory.

mkdir (make directory) command

The mkdir command is for creating new directories or subdirectories within the user’s home
directory (or in any directory to which he is authorized to do so) and for creating subdirectories
within subdirectories.

Format:

mkdir directory_name(s)

Where:

mkdir is the name of the make directory command

Linux Files and Directories Page 4 of 9


Operating Systems

directory_name is the name of the directory the user wants to create.


Absolute and relative path names may be used.

Examples:

1. If the user is currently in /usr/mitch and wants to create a subdirectory called


memos in /usr/mitch, then

$ mkdir memos

2. If the user is currently in /usr/mitch and wants to create a subdirectory michelle


under the subdirectory memos, then

$ cd memos or $mkdir memos/michelle


$ mkdir michelle

3. If the user is currently in /usr/mitch and wants to create the subdirectories temp1,
temp2 and temp3, then

$ mkdir temp1
$ mkdir temp2 or $ mkdir temp1 temp2 temp3
$ mkdir temp3

Use the mkdir command to create three subdirectories (dir1, dir2, and dir3) in your
working directory. Then use the cd command to go to dir1. Under dir1, create two
subdirectories (dir1_1 and dir1_2). Now go to dir2 and create two subdirectories (dir2_1
and dir2_2). Now go to dir3 and create two subdirectories (dir3_1 and dir3_2). Then go
back to your home directory.

rmdir (remove directory) command

The rmdir command is for removing or deleting directories. The command can remove a
directory or subdirectory only if there are no files in it. However, this command is only possible
if the user is authorized to do so.

Format:

rmdir directory_name(s)

Where:

rmdir is the name of the remove directory command

directory_name is the name of the directory the user wants to remove.


Absolute and relative path names may be used.

Examples:

Linux Files and Directories Page 5 of 9


Operating Systems

1. If the user is currently in /usr/mitch/memos and wants to delete the subdirectory


nico which is under memos, then

$ rmdir nico

2. If the user is currently in /usr/mitch and wants to delete the subdirectories temp1,
temp2, and temp3, then

$ rmdir temp1
$ rmdir temp2 or $ rmdir temp1 temp2 temp3
$ rmdir temp3

3. If the user is currently in /usr/mitch and wants to delete the subdirectory called
michelle which is under /usr/mitch/memos, then

$ cd memos
$ rmdir michelle or $ rmdir memos/michelle

Use the rmdir command to delete dir2_2 and dir3_2.

cp (copy) command

The cp command is for copying files within the same directory or to other directories.
However, this command is only possible if the user is authorized to do so.

Format:

cp source_name destination_name

Where:

cp is the name of the copy command

source_name is the name of the file to be copied. Absolute and


relative path names may be used.

destination_name is the name of the destination file. Absolute and


relative path names may be used. If
destination_name already exists, it will be deleted and
replaced with the new copy.

Examples:

1. If the user is currently in /usr/mitch and wants to copy the file michelle.let and call
the new copy cynthia.let, then

$ cp michelle.let cynthia.let

Linux Files and Directories Page 6 of 9


Operating Systems

2. If the user is currently in /usr/mitch and wants to copy the file rikki.let to
subdirectory memos and call the new copy lito.let, then

$ cd memos
$ cp ../rikki.let lito.let

or

$ cp rikki.let memos/lito.let

3. If the user is currently in /usr/mitch and wants to copy the file rico.let to
subdirectory memos and call the new copy rico.let, then

$ cd memos or $ cp rico.let memos/.


$ cp ../rico.let .

Use the vi text editor to create a text file in your home directory called file_original.txt.
Type the first page of this document (just type the contents and ignore any formatting).
Then use the cp command to copy this file to dir1 and call the new copy file1.txt.
Repeat this procedure to copy the file to dir2 and dir3 (the filenames should be file2.txt
and file3.txt respectively). Move around the directories and use the ls -l command to
verify your work.

mv (move) command

The move command is for renaming files or moving files from one directory to another.
However, this command is only possible if the user is authorized to do so.

Format:

mv source_name destination_name

Where:

mv is the name of move command

source_name is the name of the file to be renamed/moved.


Absolute and relative path names may be used.

destination_name is the name of the destination file/new file. If


destination_name already exists, it will be deleted and
replaced with the new copy.

Examples:

1. If the user is currently in /usr/mitch and wants to rename the file denise.let and call
the new copy clarence.let, then

Linux Files and Directories Page 7 of 9


Operating Systems

$ mv denise.let clarence.let

2. If the user is currently in /usr/mitch and wants to move the file olive.let to
subdirectory memos (which is under /usr/mitch) and call the new copy minette.let,
then

$ cd memos
$ mv ../olive.let minette.let

or

$ mv olive.let memos/minette.let

3. If the user is currently in /usr/mitch and wants to move the file michelle.let to the
root directory and call the new copy nico.doc, then

$ mv michelle.let /nico.let

Use the mv command to rename file_original.txt to original_file.txt. Then move file1.txt


to dir1_1 and call the new copy file1_1.txt. Finally, move file2.txt to dir 2_1 and call the
new copy file 2_1.txt. Move around the directories and use the ls –l command to verify
your work.

rm (remove) command

The rm command is for removing or deleting files. However, this command is only possible if
the user is authorized to do so.

Format:

rm filename(s)

Where:

rm is the name of the remove command

filename(s) is(are) the name(s) of the file(s) to be


removed/deleted. Absolute and relative path names
may be used.

Note: The rm command does not ask for any confirmation. Users should be careful in
the use of this command. As an alternative, a user may use the del command. It
is very much the same as rm except that there is confirmation.

Examples:

1. If the user is currently in /usr/mitch and wants to remove the file cynthia.let, then

$ rm cynthia.let

Linux Files and Directories Page 8 of 9


Operating Systems

2. If the user is currently in /usr/mitch and wants to remove the file love.let in
subdirectory memos, then

$ cd memos or $ rm memos/love.let
$ rm love.let

3. If the user is currently in /usr/mitch/memos and wants to remove the file nico.let in
/usr/mitch, then

$ cd .. or $ rm ../nico.let
$ rm nico.let

Use the rm command to delete file3.txt.

Linux Files and Directories Page 9 of 9


Operating Systems

Laboratory Exercise 3
Linux Text Files, Redirection, and Pipes
Objectives:
At the end of the session, the students are expected to:
 learn the Linux commands for manipulating files and directories

Materials:

 1 PC with Linux

Procedures:

Ask your instructor or lab facilitator to assist you in logging in a PC with Linux installed.
Practice again using the vi text editor by creating a new text file called file01.txt in your
home directory. Type the first 2 pages of this document (just type the contents and ignore
any formatting).

1. VIEWING TEXT FILES

There are three different commands in viewing the contents of text files in LINUX:

1. cat command

Format:

cat filename(s)

The cat command is for displaying the contents of a text file on-screen. It is very much
similar to the type command of DOS. However, cat will continuously display the contents
of the file without regard to whether the user is finished reading the contents or not. In
order for the user to catch up, he can freeze the screen by pressing <Ctrl-S>. To unfreeze
the screen, simply press <Ctrl-Q>.

The cat command is ideal for viewing short files.

Example:

$ cat mitch.let

Use the cat command to view the contents of file01.txt. Use <Ctrl-S> and <Ctrl-Q>
to freeze and unfreeze the screen.

2. pg command

Format:

Linux Text Files, Redirection, and Pipes Page 1 of 7


Operating Systems

pg filename

The pg command is more appropriate for viewing large files since it displays the contents
of text files one screen at a time. To view the next screen, the user simply presses the
Enter key and it displays the next screen of data. pg repeats this process until it reaches
the end of the file. The user may also press q instead of Enter to quit pg.

Example:

$ pg mitch.let

Use the pg command to view the contents of file01.txt. Don’t forget to press the
Enter key to view the file page by page.

3. more command

Format:

more filename

The more command is very much similar to the pg command in the sense that it also
displays the contents of a text file one screenful at a time. However, in order to view the
next screen, the user presses the spacebar key instead of Enter key. Furthermore, more
updates the user on the percentage of the file that was viewed already. The user may
also use the ENTER key if he wants to scroll-up the screen one line at a time.

Example:

$ more mitch.let

Use the more command to view the contents of file01.txt. Press the spacebar key to
view the file page by page. Use the Enter key to scroll-up the screen one line at a
time.

2. THE grep COMMAND

The grep command is primarily for pattern searching. Users can use this command to search
a set of files for one or more phrases or patterns. If the pattern exists, then grep will print all
the lines that contain the said pattern.

The general format of the grep command is:

grep pattern filename

Where:

pattern is the phrase or pattern the user wants to find

filename is the name of the target file

Linux Text Files, Redirection, and Pipes Page 2 of 7


Operating Systems

Example:

Assume that the file horror.story contains:

And he slowly entered the cell and went to the


sink. He reached out trembling and touched the
lather on a brush. It was real. It felt warm.
It smelled of soap. The water dripped into

$ grep the horror.story


And he slowly entered the cell and went to the
sink. He reached out trembling and touched the
lather on a brush. It was real. It felt warm.
$_

If the pattern does not exist, then LINUX will simply display the $ prompt again.

Notice that the third line matched since the is part of lather.

Use the grep command to print the lines that contain the word screen in file01.txt.

If the pattern consists of more than one word, then the user must enclose the pattern in double
quotes.

Example:

$ grep “of soap” horror.story


It smelled of soap. The water dripped into

Use the grep command to print the lines that contain the words for the in file01.txt.

Some of the options available for the grep command are:

1. The -v Option

The -v option will display all lines except those containing the pattern.

Example:

$ grep -v “of soap” horror.story


And he slowly entered the cell and went to the
sink. He reached out trembling and touched the
lather on a brush. It was real. It felt warm.

Use the grep command with the –v option to print the lines that do not contain the
words for the in file01.txt.

Linux Text Files, Redirection, and Pipes Page 3 of 7


Operating Systems

2. The -n Option

The -n option will display the lines together with their line number.

Example:

$ grep -n “of soap” horror.story


4: It smelled of soap. The water dripped into

Use the grep command with the –n option to print the lines with the corresponding
line numbers that contain the words for the in file01.txt.

3. The -c Option

The -c option will simply give the number of lines that contain the pattern.

Example:

$ grep -c the horror.story


3
$_

Use the grep command with the –c option to print the total number of lines that
contain the words for the in file01.txt.

4. The -y Option

The -y option causes Linux to ignore the difference between uppercase and lowercase
letters.

Example:

$ grep -y the horror.story


And he slowly entered the cell and went to the
sink. He reached out trembling and touched the
lather on a brush. It was real. It felt warm.
It smelled of soap. The water dripped into

Use the grep command to print the lines that contain the words the in file01.txt.
Then use grep command with the –y option to print the lines that contain the words
the in file01.txt.

3. REDIRECTION

Most Linux commands require input or produce output, or both. The shell plays an important
part in setting up a command’s input and output.

Before it starts a command, the shell sets up three default paths that the command can use for
receiving input or sending output:

Linux Text Files, Redirection, and Pipes Page 4 of 7


Operating Systems

1. Standard input is the default source of the input. Most commands unless told otherwise,
look at the standard input to get the data they need. By default, the standard input is the
keyboard.

2. Standard output is the default destination for output. By default, the standard output is
the screen.

3. Standard error is the default destination for error messages. By default, the standard
error is the screen.

Redirection allows LINUX users to change the default standard input, standard output, and
standard error.

The Output Redirection symbol (>) tells the shell to redefine the standard output.

Examples:

1. The command:

$ ls -l

gives a long listing of the files in the current directory. The shell will output the list
of the files on the screen (the default standard output).

The command:

$ ls -l > list.of.files
$_

tells the shell that the output of the ls -l command should be redirected to a file
called list.of.files. The shell will no longer print the list of files on the screen but
will instead create a new file list.of.files and store the results in it. The user may
view the contents of this file by using the cat command.

$ cat list.of.files
-r-xr-xr-x 1 mitch staff 4222 Jan 13 11:10 arithmetic
-rw-r----- 1 mitch staff 512 Dec 1 8:00 chapter1.doc
-rw-rw-r-- 1 mitch staff 1020 Feb 9 10:15 eric.let
drwx------ 2 mitch staff 2023 Jun 4 9:31 figures

Use the ls -l command to get a long listing of files in your current working
directory. But instead of outputting the list on the screen, send the output
to a file called list.of.files. Then use any of the text file viewing commands
mentioned earlier to see the contents of list.of.files.

2. The command:

$ cat file1

prints the contents of file1 on the screen.

Linux Text Files, Redirection, and Pipes Page 5 of 7


Operating Systems

The command:

$ cat file1 > file2


$_

tells the shell to redirect the output of the command cat file1 to a file file2. The shell
will therefore print or send the result of the command (the contents of file1) not to
the screen but to file2. The contents of file2 will therefore be the same as the
contents of file1. In effect, the command cat file1 > file2 copies the contents of file1
to file2. It is equivalent to:

$ cp file1 file2
$_

Use the cat command and the redirection concept to copy file01.txt to a new
file called file02.tx. Use any of the text file commands mentioned earlier to
verify the contents of the new file.

3. The command:

$ cat file1 file2



prints the contents of file1 followed by the contents of file2 on the screen.

The command:

$ cat file1 file2 > file3


$_

tells the shell to redirect the contents of file1 followed by file2 to file3. This allows
LINUX users to concatenate two or more files to a new file.

Use the cat command and the redirection concept to concatenate file01.txt
and file02.txt to a new file called file03.txt. Use any of the text file commands
mentioned earlier to verify the contents of the new file.

4. PIPES

Input and output redirection get input and send output to another file. Pipes allow users to
send input and output to another command.

The Linux pipe symbol is the vertical bar ( | ) which users type between commands. The
general usage of pipes is:

command1 | command2

LINUX will execute both commands simultaneously and redirects the standard output from
command1 directly into the standard input of command2. In other words, the output of
command1 becomes the input of command2.

Examples:

Linux Text Files, Redirection, and Pipes Page 6 of 7


Operating Systems

1. The command:

$ ls -l | pg

will produce a long listing of the files in the current working directory. But instead
of printing the list of files directly to the screen, the list (which is the output of the ls
-l command becomes input to the more command. LINUX will therefore print the
long listing of files by page.

Use the ls -l | pg command to view a long listing of the files in your current
working directory by page.

2. The command:

$ ls -l | grep Jan

will give a long listing of the files in the current directory that were recently modified
in January. The ls -l command produces a long listing of the files while the grep
Jan command selects the lines containing the phrase Jan.

Use the ls -l and grep commands to obtain a long listing of the files that
were recently modified in this month.

3. The command:

$ ls -l | grep Jan | pg



will print a long listing of the files in the current directory that were recently modified
in January by page.

Try out the ls -l | grep July | pg command.

Linux Text Files, Redirection, and Pipes Page 7 of 7

You might also like