Sunteți pe pagina 1din 29

LINUX LAB V o l-I Prepared by

Deptt. Of Information Technology Assam University, Silchar


Introduction The purpose of this document is to provide the student
with a fast and simple introduction to using the Linux command shell
and some of its basic utilities. It is assumed that the students has
zero or very limited exposure to the Linux command prompt. This
document is designed to accompany an instructor-led tutorial on this
subject, and therefore some details have been left out. Explanations,
practical examples, and references to DOS commands are made, where
appropriate. What is a command shell? * A program that interprets
commands *Allows a user to execute commands by typing them manually at
a terminal, or automatically in programs called shell scripts. *A shell
is not an operating system. It is a way to interface with the
operating system and run commands.
What is BASH? *BASH = B ourne A gain Sh ell *Bash is a shell written as a free
replacement to the standard Bourne Shell (/bin/sh) originally written by Steve Bourne for
UNIX systems. *It has all of the features of the original Bourne Shell, plus additions
that make it easier to program with and use from the command line . *Since it is Free
Software, it has been adopted as the default shell on most Linux systems. How is BASH
different from the DOS command prompt? *Case Sensitivity : In Linux/UNIX,
commands and filenames are case sensitive, meaning that typing “EXIT” instead of the
proper “exit” is a mistake. “ \ ” vs . “/”: In DOS, the forward-slash “/” is the command
argument delimiter, while the backslash “\” is a directory separator. In Linux/UNIX,
the “/” is the directory separator, and the “\” is an escape character. More about
these special characters in a minute! Filenames : The DOS world uses the “eight dot
three” filename convention, meaning that all files followed a format that
allowed up to 8 characters in the filename, followed by a period (“dot”), followed
by an option extension, up to 3 characters long (e.g. FILENAME.TXT). In
UNIX/Linux, there is no such thing as a file extension. Periods can be placed at any
part of the filename, and “extensions” may be interpreted differently by all
programs, or not at all.
Special Characters Before we continue to learn about Linux shell
commands, it is important to know that there are many symbols and
characters that the shell interprets in special ways. This means that
certain typed characters: a) cannot be used in certain situations, b)
may be used to perform special operations, or, c) must be “escaped” if
you want to use them in a normal way. Character Description \
Escape character. If you want to reference a special character, you
must “escape” it with a backslash first. Example: touch /tmp/filename\*
/ Directory separator, used to separate a string of directory
names. Example: /usr/src/linux . Current directory. Can also “hide”
files when it is the first character in a filename. .. Parent
directory ~ User's home directory * Represents 0 or more
characters in a filename, or by itself, all files in a directory.
Example: pic*2002 can represent the files pic2002, picJanuary2002,
picFeb292002,etc. ? Represents a single character in a filename.
Example: hello?.txt can represent hello1.txt, helloz.txt, but not
hello22.txt
[ ] Can be used to represent a range of values, e.g. [0-9], [A-Z],
etc. Example: hello[0-2].txt represents the names hello0.txt,
hello1.txt, and hello2.txt | “Pipe”. Redirect the output of one
command into another command. Example: ls | more > Redirect
output of a command into a new file. If the file already exists, over-
write it. Example: ls > myfiles.txt >> Redirect the output of
a command onto the end of an existing file. Example: echo .Mary 555-
1234. >> phonenumbers.txt < Redirect a file as input to a program.
Example: more < phonenumbers.txt ; Command separator. Allows you
to execute multiple commands on a single line. Example: cd
/var/log ; less messages && Command separator as above, but only runs
the second command if the first one finished without errors.
Example: cd /var/logs && less messages & Execute a command in
the background, and immediately get your shell back. Example: find /
-name core > /tmp/corefiles .txt &
Executing Commands The Command PATH : * Most common commands are
located in your shell's “PATH”, meaning that you can just type the
name of the program to execute it. Example : Typing “ ls” will
execute the “ ls” command. *Your shell's “PATH” variable includes the
most common program locations, such as /bin, /usr/bin, /usr/X11R6/bin,
and others. *To execute commands that are not in your current PATH,
you have to give the complete location of the command .
Examples: /home/bob/myprogram ./program (Execute a program in
the current directory) ~/bin/program (Execute program from a
personal bin directory) Command Syntax *Commands can be run by
themselves, or you can pass in additional arguments to make them
do different things. Typical command syntax can look something like
this: command [-argument] [-argument] [--argument]
[file] *Examples: ls List files in current directory ls -l
Lists files in “long” format ls -l --color As above,
with colourized output cat filename Show contents of a file
cat -n filename Show contents of a file, with line
numbers
Getting Help When you're stuck and need help with a Linux command,
help is usually only a few keystrokes away! Help on most Linux
commands is typically built right into the commands themselves,
available through online help programs (“man pages” and “info pages”),
and of course online. Using a Command's Built - In Help Many
commands have simple “help” screens that can be invoked with special
command flags. These flags usually look like “-h” or “--help”. Example:
grep --help Online Manuals : “ Man Pages ” The best source of
information for most commands can be found in the online manual pages,
known as “man pages” for short. To read a command's man page, type “man
command”. Examples: man ls Get help on the “ls” command. man
man A manual about how to use the manual! To search for a
particular word within a man page, type “/word”. To quit from a man
page, just type the “Q” key. Sometimes, you might not remember the name
of Linux command and you need to search for it. For example, if you
want to know how to change a file's permissions, you can search the
man page descriptions for the word “permission” like this: man
-k permission If you look at the output of this command, you will
find a line that looks something like: chmod (1) - change file
access permissions Now you know that “chmod” is the command you were
looking for. Typing “man chmod” will show you the chmod command's
manual page!
Info Pages Some programs, particularly those released by the Free
Software Foundation, use info pages as their main source of online
documentation. Info pages are similar to man page, but instead of being
displayed on one long scrolling screen, they are presented in shorter
segments with links to other pieces of information. Info pages are
accessed with the “info” command, or on some Linux distributions,
“pinfo” (a nicer info browser ). For example: info df Loads the “df” info
page. Navigating the Linux Filesystem The Linux filesystem is a
tree-like hierarchy hierarchy of directories and files. At the base of
the filesystem is the “/” directory, otherwise known as the “root” (not
to be confused with the root user). Unlike DOS or Windows filesystems
that have multiple “roots”, one for each disk drive, the Linux
filesystem mounts all disks somewhere underneath the / filesystem. The
following table describes many of the most common Linux directories.
The Linux Directory Layout
Commands for Navigating the Linux Filesystems The first thing you
usually want to do when learning about the Linux filesystem is take some time
to look around and see what's there! These next few commands will: a) Tell you
where you are, b) take you somewhere else, and c) show you what's there. The
following table describes the basicoperation of the pwd, cd, and ls
commands, and compares them to certain DOS commands that you might already
be familiar with.
Piping and Re - Direction Before we move on to learning even more commands,
let's side-track to the topics of piping and re-direction. The basic UNIX
philosophy, therefore by extension the Linux philosophy, is to have many small
programs and utilities that do a particular job very well. It is the
responsibility of the programmer or user to combine these utilities to make
more useful command sequences. 4 . 1 Piping Commands Together The pipe
character, “|”, is used to chain two or more commands together. The output of
the first command is “piped” into the next program, and if there is a second
pipe, the output is sent to the third program, etc. For example: ls
-la /usr/bin | less In this example, we run the command “ls -la /usr/bin”, which
gives us a long listing of all of the files in /usr/bin. Because the output of
this command is typically very long, we pipe the output to a program called
“less”, which displays the output for us one screen at a time. 4 . 2
Redirecting Program Output to Files There are times when it is useful to
save the output of a command to a file, instead of displaying it to the screen.
For example, if we want to create a file that lists all of the MP3 files in a
directory, we can do something like this, using the “>” redirection character:
ls -l /home/vic/MP3/*.mp3 > mp3files.txt A similar command can be
written so that instead of creating a new file called mp3files.txt, we can
append to the end of the original file: ls -l
/home/vic/extraMP3s/*.mp3 >> mp3files.txt
Filesystem Formatting and Checking Three basic tools are
available to manage the filesystem on various partitions: fdisk, mkfs,
and fsck. They can help you configure partitions as well as create, and
then check and repair, different filesystems. As with the rest of this
chapter, this section covers only the very basics; for more
information, see the man page associated with each respective command
tool Media Device Device File Floppy drive First
floppy (Microsoft A: drive) = /dev/fd0 Second floppy (Microsoft B:
drive) = /dev/fd1 IDE hard drive First IDE drive = /dev/ hda IDE
CD/DVD drive Second IDE drive = /dev/hdb Third IDE
drive = /dev/hdc Fourth IDE drive = /dev/hdd SCSI hard drive
First SCSI drive = /dev/sda SCSI CD/DVD drive Second SCSI
drive = /dev/sdb … Twenty-seventh SCSI drive
= /dev/sdaa and so on Parallel port drives First IDE drive =
/dev/pd1 First tape drive: /dev/pt1 USB drives Varies
widely IEEE 1394 drives IEEE 1394 (aka FireWire, iLink) is
actually a SCSI standard, so these are controlled in Linux as
SCSI devices
fdisk The Linux fdisk utility is a lot more versatile than its Microsoft
counterpart. But to open it, you need to know the device file associated with
the hard drive that you want to change. Identifying the hard disk device file
is covered above. Assuming you want to manage the partitions on the first SCSI
hard disk, enter / sbin / fdisk / dev / sda . As you can see in the fdisk utility is
flexible. mkfs To format a Linux partition, apply the mkfs command. It allows
you to format a partition to a number of different filesystems. To format a
typical partition such as /dev/hda2 to the current Red Hat standard, the third
extended filesystem, run the following command: # mkfs -t ext3 /dev/hda2
fdisk Command Description a Allows you to specify the bootable
Linux partition (with /boot). l Lists known partition types; fdisk can
create partitions that conform to any of these filesystems. n Adds a new
partition; works only if there is unpartitioned space on the disk. q Quits
without saving any changes. t Changes the partition filesystem.
FILE COMMANDS ls
Directory listing ls - al Formatted
listing with hidden files ls - lt
Sorting the Formatted listing by time
modification cd dir Change directory
to dir cd Change to home
directory pwd Show current working
directory mkdir dir Creating a directory dir
cat > file Places the standard input into
the file more file Output the contents of
the file head file Output the first 10
lines of the file tail file Output the
last 10 lines of the file tail - f file
Output the contents of file as it
grows,starting with the last 10 lines
touch file Create or update file
rm file Deleting the file rm - r
dir Deleting the directory rm - f file
Force to remove the file rm - rf dir
Force to remove the directory dir cp file1
file2 Copy the contents of file1 to file2 cp
- r dir1 dir2 Copy dir1 to dir2;create dir2 if not
present mv file1 file2 x Copy dir1 to dir2;create
dir2 if not present
directory ln - s file link Create symbolic
link link to file FILE PERMISSION chmod
octal file Change the permission of file to
octal,which can be
found separately for user,group,world by
adding, • 4-read(r)
• 2-write(w) • 1-execute(x)
SYSTEM INFORMATION date
Show the current date and time cal Show
this month's calender uptime Show
current uptime w Display who is on
line whoami Who you are logged in as
finger user Display information about
user uname - a Show kernel
information cat / proc / cpuinfo Cpu
information cat proc / meminfo Memory
information man command Show the
manual for command df Show the disk
usage du Show directory space usage
free Show memory and swap usage
whereis app Show possible locations of
app which app Show which applications
will be run by default Compression
tar cf file . tar file Create tar named
file.tar containing file tar xf
file . tar Extract the files from file.tar
tar czf file . tar . gz files Create a tar
with Gzip compression tar xzf
file . tar . gz Extract a tar using Gzip tar
cjf file . tar . bz2 Create tar with Bzip2
compression tar xjf file . tar . bz2
Extract a tar using Bzip2 gzip file
Compresses file and renames it to
file.gz gzip - d file . gz Decompresses file.gz
back to file
NETWORK ping host Ping
host and output results whois domain
Get whois information for
domains dig domain Get DNS
information for domain dig - x
host Reverse lookup host wget
file Download file wget - c file
Continue a stopped download
PROCESS MANAGEMENT ps To display
the currently working processes top
Display all running process kill pid Kill
the process with given pid killall proc
Kill all the process named proc pkill
pattern Will kill all processes matching
the pattern bg List stopped or
background jobs,resume a stopped job in
the background fg Brings the most recent
job to foreground fg n Brings job n to
the foreground
SHORT CUTS Ctrl + A Move cursor to the
beginning of the command line Ctrl + C End a
running program and return the prompt, Ctrl + D
Log out of the current shell session, equal to
typing exit or logout. Ctrl + E Move cursor to
the end of the command line. Ctrl + H Generate
backspace character. Ctrl + L Clear this terminal.
Ctrl + R Search command history/ Type to bring up
a recent command ctrl + z Stops the current
command, resume with fg in the foreground or bg
in the background ctrl + w Erases one word in the
current line ctrl + u Erases the whole line !!
Repeats the last command exit Logout the
current session
ArrowLeft and Move the cursor one place to the left
or right on ArrowRight the command line, so that you
can insert characters at other places than
just at the beginning and the end. ArrowUp
and Browse history. Go to the line that you want to
ArrowDown repeat, edit details if necessary, and
press Enter to save time. Shift + PageUp
Browse terminal buffer (to see text that has and
"scrolled off" the screen). Shift + PageDown Tab
Command or filename completion; when multiple
choices are possible, the system will either signal
with an audio or visual bell, or, if too many
choices are possible, ask you if you want to
see them all. Tab Tab Shows file or command
completion possibilities
GETTING HELP yourname@yourcomp ~> man man The documentation for
man will be displayed on your screen after you press
Enter:

man(1) man(1)
NAME
man - format and display the on-line manual pages
manpath - determine user's search path for man pages
SYNOPSIS
man [-acdfFhkKtwW] [--path] [-m system] [-p string] [-C config_file]
[-M pathlist] [-P pager] [-S section_list] [section] name ...
DESCRIPTION
man formats and displays the on-line manual pages. If you specify
section, man only looks in that section of the manual.
name is normally the name of the manual page, which is typically the
name of a command, function, or file. However, if name contains a
slash (/) then man interprets it as a file specification, so that you
can do man ./foo.5 or even man /cd/foo/bar.1.gz.
See below for a description of where man looks for the manual
page files.
OPTIONS
-C config_file
lines 1-27
Get.info
File: info started
, Nodeby
: Toptyping
, Getting info
Next: info Started,in aUp:
(dir)
terminal window :
Info: An Introduction
*********************
Info is a program, which you are using now, for reading
documentation of computer programs. The GNU Project distributes
most
of its on-line manuals in the Info format, so you need a program
called
"Info reader" to read the manuals. One of such programs you are
using
now.
If you are new to Info and want to learn how to use it, type
the
command `h' now. It brings you to a programmed instruction
sequence.
To learn advanced Info commands, type `n' twice. This brings
you to
`Info for Experts', skipping over the `Getting Started' chapter.
* Menu:
* Getting Started:: Getting started using an Info reader.
* Advanced Info:: Advanced commands within Info.
* Creating an Info File:: How to make your own Info file.

--zz-Info: (info.info.gz)Top, 24 lines --Top-------------------------------


The -- help option Most GNU commands support the --help, which
gives a short explanation about how to use the command and a
list of available options. Below is the output of this option
with the cat command:

userprompt@host: cat – help

Usage: cat [OPTION] [FILE]...


Concatenate FILE(s), or standard input, to standard output.
-A, --show-all equivalent to -vET
-b, --number-nonblank number nonblank output lines
-e equivalent to -vE
-E, --show-ends display $ at end of each line
-n, --number number all output lines
-s, --squeeze-blank never more than one single blank line
-t equivalent to -vT
-T, --show-tabs display TAB characters as ^I
-u (ignored)
-v --show-nonprinting use ^ and M- notation,
except for LFD and TAB
--help display this help and exit
--version output version information and exit
With no FILE, or when FILE is -, read standard input.
The whatis and apropos commands A short index
of explanations for commands is available using the
whatis command, like in the examplesbelow:
[your_prompt] whatis ls ls (1) - list directory
contents
apropos gives more information. Say that you don't
know how to start a browser, then you could enter the
following command
another prompt :
> apropos browser
Galeon [galeon](1) - gecko-based GNOME web browser
Lynx (1) - a general purpose distributed
information browser
for the World Wide Web
ncftp (1) - Browser program for the File Transfer
Protocol
opera (1) - a graphical web browser
pilot (1) - simple file system browser in the
style of the
Pine Composer
Pinfo (1) - curses based lynx-style info
browser
pinfo [pman] (1) - curses based lynx-style info browser
viewres (1x) - graphical class browser for Xt
Graphical help
Name cal - displays a calendar Synopsis cal [-smjy13 ] [[month] year]
Description Cal displays a simple calendar. If arguments are not specified,
the current month is displayed. The options are as follows: -1 Display single
month output. (This is the default.) -3 Display prev/current/next month output.
-s Display Sunday as the first day of the week. (This is the default.) -m
Display Monday as the first day of the week. -j Display Julian dates (days one-
based, numbered from January 1). -y Display a calendar for the current year.
-V Display version information and exit. A single parameter specifies the
year (1 - 9999) to be displayed; note the year must be fully specified: “cal
89” will not display a calendar for 1989. Two parameters denote the month
(1 - 12) and year. If no parameters are specified, the current month’s
calendar is displayed. A year starts on Jan 1. The Gregorian Reformation is
assumed to have occurred in 1752 on the 3rd of September. By this time, most
countries had recognized the reformation (although a few did not recognize it
until the early 1900’s.) Ten days following that date were eliminated by
the reformation, so the calendar for that month is a bit unusual. History A
cal command appeared in Version 6 AT&T UNIX.
Name arch - print machine architecture Synopsis arch Description
arch is deprecated command since release util-linux 2.13. Use uname
-m or use arch from the coreutils package. On current Linux systems,
arch prints things such as “i386", “i486", “i586", “alpha", “sparc", “arm",
“m68k", “mips", “ppc". See Also uname(1) , uname(2)
Name cat - concatenate files and print on the standard output Synopsis cat
[OPTION] [FILE]... Description Concatenate FILE(s), or standard input, to standard
output. -A, -- show - all equivalent to -vET -b, -- number - nonblank
number nonblank output lines -e equivalent to -vE -E, -- show -
ends display $ at end of each line -n, --number number all
output lines -s, -- squeeze - blank never more than one single blank line -t
equivalent to -vT -T, -- show - tabs display TAB
characters as ^I -u (ignored) -v, -- show - nonprinting use ^
and M- notation, except for LFD and TAB --help display this help
and exit --version output version information and exit With no FILE,
or when FILE is -, read standard input. Examples cat f - g Output f’s contents,
then standard input, then g’s contents. cat Copy standard input to standard
output. See Also The full documentation for cat is maintained as a Texinfo
manual. If the info and cat programs are properly installed at your site, the
command info cat should give you access to the complete manual.

S-ar putea să vă placă și