Vous êtes sur la page 1sur 11

HOLY ANGEL UNIVERSITY

COLLEGE OF ENGINEERING AND ARCHITECTURE


COMPUTER ENGINEERING DEPARTMENT

Topic Linux Command Using Essential Tools


Laboratory # M2
To learn basic shell skills
To learn Vi Editor
Objectives
To understand the shell environment
To learn about Help command

Name : Date Submitted :

Grade : College Faculty : Engr. Lucky Padilla


Linux Command Using Essential Tools 1

I. INTRODUCTION

The shell is the default working environment for a Linux administrator. It is the environment where users and administrators enter
commands that are executed by the operating system. Different shells for Linux are available, but bash is the common shell.

Linux Shell Options

As with many key software components, Linux provides a range of options for shells. A complete list would be quite long, but the more
common choices include the following:

bash : The GNU Bourne Again Shell (bash) is based on the earlier Bourne shell for Unix but extends it in several ways. In Linux,
bash is the most common default shell for user accounts.

bsh : The Bourne shell upon which bash is based also goes by the name bsh. Its not often used in Linux, although the bsh
command is usually a symbolic link to bash.

tcsh : This shell is based on the earlier C shell (csh). Its a fairly popular shell in some circles, but no major Linux distributions
make it the default shell. Although its similar to bash in many respects, some operational details differ. For instance, you dont
assign environment variables in the same way in tcsh as in bash.

csh : The original C shell isnt much used on Linux, but if a user is familiar with csh, tcsh makes a good substitute.

ksh : The Korn Shell (ksh) was designed to take the best features of the Bourne shell and the C shell and extend them further.
Its got a small but dedicated following among Linux users.

zsh : The Z shell (zsh) takes shell evolution further than the Korn Shell, incorporating features from earlier shells and adding
still more.

The file /bin/sh is a symbolic link to the systems default shellnormally /bin/bash for Linux.

Using a Shell

Linux shell use is straightforward for anybody whos used a text-mode OS before: You type a command, possibly including options to it,
and the computer executes the command. For the most part, Linux commands are externalthat is, theyre separate programs from the
shell. A few commands are internal to the shell, though, and knowing the distinction can be important. You should also know some of
the tricks that can ease use of the command shellhow to have the computer complete a long command or filename, or retrieve a
command youve recently run, or edit a command youve recently used (or havent yet fully entered)

Editing Files with Vi

Vi was the first full-screen text editor written for Unix. Its designed to be small and simple. Vi is small enough to fit on tiny, floppy-based
emergency boot systems. For this reason alone, Vi is worth learning; you may need to use it in an emergency recovery situation. Vi is,
however, a bit strange, particularly if youre used to GUI text editors.

To use Vi, you should first understand the three modes in which it operates. Once you understand those modes, you can begin learning
about the text-editing procedures Vi implements. This lab also examines how to save files and exit from Vi.

Most Linux distributions ship with a variant of Vi known as Vim, or Vi Improved. As the name implies, Vim supports more features than
the original Vi does. The information presented here applies to both Vi and Vim. Most distributions that ship with Vim support launching
it by typing vi, as if it were the original Vi.
Linux Command Using Essential Tools 2

II. PROCEDURES

1. USING ESSENTIAL TOOLS

1.1. BASIC SHELL SKILLS:

1.1.1. Executing Commands

Aliases. These commands that a user can define as needed


To print the list of aliases
[root@localhost ~] #alias -p

To define an alias
[root@localhost ~] #alias newcommand=oldcommand
[root@localhost ~] #alias getipadd=ip address show

To permanent set alias, edit .bashrc


[root@localhost ~] #vi .bashrc
Then add vi=vim

Internal Commands. These are commands that is a part of the shell itself. They are available when the shell is loaded
and can be executed from memory without any lookup from disk
Example: cd, echo

To find out whether a command is a bash internal, or an executable


file on disk, you can use
[root@localhost ~] #type cd
[root@localhost ~] #type type
[root@localhost ~] #type exit

External Commands. These are commands that exists as an executable file on disk of the computer

To look up external commands:


[root@localhost ~] #$PATH

To find out which exact command the shell will be using, you can use the
which command:
[root@localhost ~] #which ls
Linux Command Using Essential Tools 3

1.1.2. I/O Redirection and Pipes

[root@localhost ~] #ls /
[root@localhost ~] #ls / > /dev/null
[root@localhost ~] #ls abcdef > /dev/null
[root@localhost ~] #ls abcdef 2> /dev/null
[root@localhost ~] #ls abcdef 2> /dev/null > output
[root@localhost ~] #cat output
[root@localhost ~] #echo hello
[root@localhost ~] #ls >> output
[root@localhost ~] #ls -R /
[root@localhost ~] #ls -R / | less

1.1.3. History. Bash is configured to keep the last 1,000 commands you have used (and if shell session is never closed, the ex-
act number can grow even much beyond that). When a shell session is closed, the history of that session is updated to
the history file.

[root@localhost ~] #history

Type !number to execute a command with a specific number from history


[root@localhost ~] #!number

To wipe all history that is currently in memory. Close this terminal session as well
[root@localhost ~] #history -c
To delete the history file
[root@localhost ~] #history -c
[root@localhost ~] #history -w

1.1.4. Bash Completion. Bash completion is used most on commands. Just type the beginning of a command and press the
Tab key on your computers keyboard.
Linux Command Using Essential Tools 4

1.2. EDITING WITH VIM

1.2.1. At any given moment, Vi is running in one of three modes:

Command mode: This mode accepts commands, which are usually entered as single letters. For instance, i and a both
enter insert mode, although in somewhat different ways.

Ex mode: To manipulate files (including saving your current file and running outside programs), you use ex mode. You
enter ex mode from command mode by typing a colon (:), typically directly followed by the name of the ex mode
command you want to use. After you run the ex mode command, Vi returns automatically to command mode.

Insert mode: You enter text in insert mode. Most keystrokes result in text appearing on the screen. One important
exception is the Esc key, which exits from insert mode back to command mode.

1.2.2. Inserting Text

Command Description
a Append text after the cursor.
A Append text at the end of the line.
i Insert text before the cursor.
I Insert text before the first non-blank in the line.
o Begin a new line below the cursor and insert text, repeat [count] times.
O Begin a new line above the cursor and insert text, repeat [count] times.

1.2.3. How to Exit

Command Description
Esc + :q Quit Vim. This fails when changes have been made.
Esc + :q! Quit without writing
Esc + :w Write the current file
Esc + :wq Write the current file and exit.
Esc + :wq! Write the current file and exit always.
Esc + wq {file} Write to {file}. Exit if not editing the last
Esc + wq! Write to {file} and exit always.
{file}
ZZ Write current file, if modified, and exit.
ZQ Quit current file and exit (same as ":q!").

1.2.4. Deleting a Text

Command Description
<Del> or x Delete [count] characters under and after the cursor
X Delete [count] characters before the cursor
d{motion} Delete text that {motion} moves over
dd Delete [count] lines
D Delete the characters under the cursor until the end of the line
{Visual}x or Delete the highlighted text
{Visual}d
Linux Command Using Essential Tools 5

1.2.5. Changing or Replacing a Text

Command Description
r{char} Replace the character under the cursor with {char}.
R Enter Insert mode, replacing characters rather than inserting
~ Switch case of the character under the cursor and move the cursor to the right. If a [count] is given,
do that many characters.
{Visual}~ {Visual}~ Switch case of highlighted text

1.2.6. Copying and Moving a Text

Command Description
yy Yank [count] lines [into register x]
Y Yank [count] lines [into register x] (synonym for yy)
{Visual}["x]y Yank the highlighted text [into register x]
{Visual}["x]Y Yank the highlighted lines [into register x]
["x]p Put the text [from register x] after the cursor [count] times
["x]P Put the text [from register x] before the cursor [count] times

1.2.7. Undo/Redo/Repeat

Command Description
u Undo [count] changes
CTRL-R Redo [count] changes which were undone
U Undo all latest changes on one line. {Vi: while not moved off of it}
. Repeat last change, with count replaced with [count]

1.2.8. Moving around

Command Description
h or left [count] characters to the left
arrow
l or right [count] characters to the right
arrow
K or up arrow [count] lines upward
J or down [count] lines downward (linewise)
arrow
0 or <Home> To the first character of the line (exclusive)
$ or <End> To the end of the line and [count - 1] lines downward

1.2.9. Searching and Replace

Command Description
/{pattern} Search forward for the [count]'th occurrence of {pattern}
?{pattern} Search backward for the [count]'th previous occurrence of {pattern}
n Repeat the latest "/" or "?" [count] times
N Repeat the latest "/" or "?" [count] times in opposite direction
:s/pattern/replace/ Replacing one string with another string in the current line
:%s/pattern/replace/ Replacing every occurrence of a string in the entire tex
Linux Command Using Essential Tools 6

1.2.10. Selecting a Text

Command Description
v Start Visual mode per character
V Start Visual mode linewise.
<Esc> Exit Visual mode without making any changes

1.2.11. Insert text from a file

Command Description
:r (path directory of the file) Insert the content of filename where [cursor] is located.

Exercise
1. Type vim ~/testfile . This starts vim and opens a file with the name testfile in ~, which represents your current home
directory.
2. Press i to enter input mode and type the following text

cow
sheep
ox
chicken
snake
fish
oxygen

3. Press Esc to get back to command mode and type :w to write the file using the same filename.
4. Type :3 to go to line number 3.
5. Type dd to delete this line.
6. Type dd again to delete another line.
7. Type u to undo the last deletion.
8. Type o to open a new line.
9. Enter some more text at the current cursor position:

tree
farm

10. Press Esc to get back into command mode.


11. Type :%s/ox/OX/g.
12. Type :wq to write the file and quit. If for some reason that does not work,
13. use :wq! .
Linux Command Using Essential Tools 7

1.3. UNDERSTANDING THE SHELL ENVIRONMENT

[root@localhost ~] # env
[root@localhost ~] # echo $PATH

1.3.1. Environment Configuration Files

/etc/profile : This is the generic file that is processed by all users upon login.
/etc/bashrc : This file is processed when subshells are started.
~/.bash_profile : In this file, user-specific login shell variables can be defined.
~/.bashrc : In this user-specific file, subshell variables can be defined.

1.3.2. Using /etc/motd and /etc/issue

Using /etc/motd can be a convenient way for system administrators to


inform users.

Another way to send information to users is by using /etc/issue. The contents of this
file display before the user logs in. This provides an excellent means of specifying
specific login instructions to users who are not logged in yet.

############################################
# This system is for the use of authorized users only #
############################################

####################################################################
# ALERT! You are entering a secured area! Your IP, Login Time, Username has #
# been noted and has been sent to the server administrator! This service is #
# restricted to authorized users only. All activities on this system are logged. #
# Unauthorized access will be fully investigated and reported to the appropriate #
# law enforcement agencies #
####################################################################
Linux Command Using Essential Tools 8

1.4. FINDING HELP

To find the manual page names and descriptions


[root@localhost ~] # man -k copy

To display a short description of the item as found in the man database.


[root@localhost ~] # man -f cp

When using the man -k command, the mandb database is consulted. To update man database
[root@localhost ~] # mandb
Linux Command Using Essential Tools 9

III. LABORATORY QUESTIONS

1. Explain the purpose of environment variables.


2. Which command enables you to find the correct man page based on keyword usage?
3. What is the name of the file where bash stores its history?
4. What can you add to a command to make sure that it does not show any error message, assuming that you do not care about
the information that is in the error messages either?
5. How do you read the current contents of the PATH variable?
Linux Command Using Essential Tools 10

IV. REMARKS AND CONCLUSIONS

Vous aimerez peut-être aussi