Professional Documents
Culture Documents
Itec 65 PDF
Itec 65 PDF
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.
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.
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
. . . . .
. . . . .
. . . . .
Column Explanation
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:
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.
Example:
$ ls arithmetic
↵ arithmetic
$ ls *.c ↵
sample.c test.program.c
$ ls -l sample.c ↵
-rw-r--r-- 2 mitch staff 20432 Oct 25 10:30 sample.c
Separation of Components
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:
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 ↵
Example:
$ ls -al ↵
Take note that there should be no space between the a and the l.
Also, the ordering of the options is not important. Therefore, ls -l -a and ls -la will also work.
The rule of separation is also applicable in case of multiple arguments. A space separates one
argument from another.
Example:
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.
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).
Once the information is displayed, the following keys may be used to move through the man
pages:
Don’t forget to press q to exit the man page and return to the command prompt.
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.
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.
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.
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.
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
If the user types a number before certain commands, it will cause the cursor to move several
units. Examples are:
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
dw delete a word
dd delete a line
If the user types a number before any of these commands, it will cause the deletion of several
units. Examples are:
EDITING TEXT IN vi
Command Action
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
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.
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.
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:
Example:
Special characters such as slash (/), asterisk (*), ampersand (&), pipe (|), quote (“ “), and dollar
sign ($) should not be used.
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:
− 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
− 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
mitch cynthia
docs letters
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
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:
../letters/letter1
../letters/letter2
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
If a user wants to move from one directory to another, he should use the cd command.
Format:
cd directory_name
Where:
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 ../..
$ cd /usr or $ cd ..
$ cd /usr/rikki or $ cd ../rikki
$ cd /usr/mitch/letters or $ cd letters
$ 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.
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:
Examples:
$ mkdir memos
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.
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:
Examples:
$ 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
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:
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
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
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.
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:
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
$ 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
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:
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
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
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).
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>.
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:
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.
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.
Where:
Example:
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:
Use the grep command to print the lines that contain the words for the in file01.txt.
1. The -v Option
The -v option will display all lines except those containing the pattern.
Example:
Use the grep command with the –v option to print the lines that do not contain the
words for the in file01.txt.
2. The -n Option
The -n option will display the lines together with their line number.
Example:
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:
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:
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:
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.
The command:
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:
The command:
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:
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: