module-ii

45
MODULE 2 MCA-503 Linux Internals ADMN 2009-‘10 Files and File Structure 2.1 Linux File System All the files in Linux system are connected into one overall directory tree, parts of that tree may reside on different storage devices such as hard drives or CD-ROMs. Files on a particular storage device are organized into what is referred to as a file system. A file system is a formatted device, with its own tree of directories and files. Linux directory tree may encompass several file systems, each on different storage devices. On a hard drive with several partitions, there is a file system for each partition. The files themselves are organized into one seamless tree of directories, beginning from the root directory. A file system has its files organized into its own directory tree. One can think of this as a subtree that must be attached to the main directory tree. The tree remains separate from the system’s directory tree until you specifically connect it. Linux organizes its files and directories into one overall interconnected tree, beginning from the root directory and extending down to system and user directories. The organization and layout for the system directories are determined by the file system hierarchy standard (FHS). The FHS provides a standardized layout that all Linux distributions should follow in setting up their system directories. For example, there must be a /etc directory to hold Dept. of Computer Science And Applications, SJCET page 25

Upload: charlesbabu

Post on 13-Aug-2015

12 views

Category:

Documents


0 download

DESCRIPTION

JAVA Notes

TRANSCRIPT

Page 1: Module-II

MODULE 2 MCA-503 Linux Internals ADMN 2009-‘10

Files and File Structure

2.1 Linux File System

All the files in Linux system are connected into one overall directory tree, parts of that tree may reside on different storage devices such as hard drives or CD-ROMs. Files on a particular storage device are organized into what is referred to as a file system. A file system is a formatted device, with its own tree of directories and files. Linux directory tree may encompass several file systems, each on different storage devices. On a hard drive with several partitions, there is a file system for each partition. The files themselves are organized into one seamless tree of directories, beginning from the root directory. A file system has its files organized into its own directory tree. One can think of this as a subtree that must be attached to the main directory tree. The tree remains separate from the system’s directory tree until you specifically connect it.

Linux organizes its files and directories into one overall interconnected tree, beginning from the root directory and extending down to system and user directories. The organization and layout for the system directories are determined by the file system hierarchy standard (FHS). The FHS provides a standardized layout that all Linux distributions should follow in setting up their system directories. For example, there must be a /etc directory to hold configuration files and a /dev directory for device files.

Linux uses a number of specifically named directories for specialized administrative tasks. All these directories are at the very top level of your main Linux file system, the file system root directory, represented by a single slash, /.

Fig 2.1 Directory structure

Dept. of Computer Science And Applications, SJCET page 25

Page 2: Module-II

MODULE 2 MCA-503 Linux Internals ADMN 2009-‘10

Table 2.1 The File System Structure

Table 2.1 Directory struc

Table 2.1 shows various directories and its functions. There can be other directories also attached to the / depends on the distribution. Some distributions may contain a directory called /media which is used for mounting the additional devices attached to the system like pen-drive, DVD Drive etc.

Dept. of Computer Science And Applications, SJCET page 26

Directory Function

/ Begins the file system structure—called the root.

/boot stores kernel images and boot configuration files

/root Super user’s (root user’s) home directory

/home stores the home directories for the individual users

/etc Holds system configuration files and any other system files.

/mnt mount point for other storage devices

/proc Process directory, a memory-resident directory that contains files used to provide information about the system.

/sysHolds the sysfs file system for kernel objects, listing supported kernel devices and modules.

/dev stores special device files used to access hardware devices

/bin Holds the essential user commands and utility programs.

/sbinHolds administration-level commands and any commands used by the root user.

/lib stores library modules used by the commands

/usrused for programs, libraries, documentation, etc used by normal users

/varStores system data that varies or changes frequently such as system logs, mail and print spool files, etc

/tmp used for temporary files

/optHolds added software applications (for example, KDE on some distributions , oracle DBMS package on Red Hat)

Page 3: Module-II

MODULE 2 MCA-503 Linux Internals ADMN 2009-‘10

2.2 File Naming Convention

Following are the general rules for naming the files in both Linux and Unix.

1. All file names are case sensitive. So filename vivek.txt Vivek.txt VIVEK.txt all are three different files.

2. File name can be of maximum length 255.3. upper and lowercase letters, numbers, "." (dot), and "_"

(underscore) symbols can be used.4. special characters such as blank space can be used but they are hard

to use and it is better to avoid them.

5. Filenames may contain any character except / (root directory), which is reserved as the separator between files and directories in a pathname. But null character cannot be used.

6. No need to use . (dot) in a filename. Some time dot improves readability of filenames. And you can use dot based filename extension to identify file. For example:

.sh = Shell file and .tar.gz = Compressed archive

Any file/directory whose name starts with “.” is a hidden file/directory.

2.3 PATH

PATH is an environmental variable in Linux and other Unix-like operating systems that tells the shell which directories to search for executable files (i.e., ready-to-run programs) in response to commands issued by a user. It increases both the convenience and the safety of such operating systems and is widely considered to be the single most important environmental variable.

Environmental variables are a class of variables (i.e., items whose values can be changed) that tell the shell how to behave as the user works at the command line (i.e., in a text-only mode) or with shell scripts. A shell is a program that provides the traditional, text-only user interface for Unix-like operating systems; its primary function is to read commands that are typed in at the command line and then execute (i.e., run) them.

Dept. of Computer Science And Applications, SJCET page 27

Page 4: Module-II

MODULE 2 MCA-503 Linux Internals ADMN 2009-‘10

PATH (which is written with all upper case letters) should not be confused with the term path (lower case letters). The latter is a file's or directory's address on a files system (i.e., the hierarchy of directories and files that is used to organize information stored on a computer). A relative path is an address relative to the current directory (i.e., the directory in which a user is currently working). An absolute path (also called a full path) is an address relative to the root directory (i.e., the directory at the very top of the file-system and which contains all other directories and files).

A user's PATH consists of a series of colon-separated absolute paths that are stored in plain text files. Whenever a user types in a command at the command line that is not built into the shell or that does not include its absolute path and then presses the Enter key, the shell searches through those directories, which constitute the user's search path, until it finds an executable file with that name.

2.4 File Types and Users

“In Linux everything is a file but not all the files are of the same type.”

In Linux everything is considered a file. The processes, the CPU, RAM, and even the Linux kernel memory can be viewed as files under the /proc directory. Devices like monitors, hard drives, modems, keyboards, CD-ROM and printers are files. Surprisingly, even any error messages that might result from what we type is a file. Hence anything that is physically or logically attached to the machine, and that the Linux kernel can see, is represented as a file in Linux.

Linux treats hardware devices as files. Every device has a file corresponding to it (found in the /dev directory) and a program can open this file and read from or write to it just like with any other file. Each time that happens, the kernel intervenes and writes to or reads from the hardware, then passes back the result to the program which is a huge advantage if we are writing programs because we don’t need to know how to actually access the hardware.

For example, the hard disks will be files into /dev, like /dev/hda, or /dev/sdb. The partitions will also be files there: /dev/sda1, /dev/sda2… and so on.

At the time a new file is created, it gets a free inode (index node) which contains the following information :a) Owner and group owner of the file.b) File type (regular, directory, …)

Dept. of Computer Science And Applications, SJCET page 28

Page 5: Module-II

MODULE 2 MCA-503 Linux Internals ADMN 2009-‘10

c) Permissions on the file( read , write etc )d) Date and time of creation, last read and change.e) Date and time this information has been changed in the inode.f) Number of links to this file ( soft / hard )g) File size

The only information not included in an inode, is the file name and directory. These are stored in the special directory files.

There are seven basic types of file types in Linux.

Regular Files Directories Character  Device Files Block Device Files Local Domain Sockets Named Pipes Symbolic Links

The file type can be determined by using the command ls –l For example$ ls –ltotal 8-rw-r–r–     1 root   root       22 Oct  6 15:33 anormalfilebrw-rw—-     2 root   disk  41,   0 May  5 2008 blockdevcrw-rw-rw-     2 root   root   5,   0 May  5 2008 characterdevdrwxr-xr-x     2 root   root     4096 Oct  6 15:33 subdirlrwxrwxrwx     1 root   root       11 Oct  6 15:35 symbolic -> anormalfile

Ordinary files begin with a dash (-), directories begin with d, symbolic links begin with the character l, block devices are prefaced with the character b, and character devices begin with the letter c. Ordinary files

An ordinary file can consist of any kind of data, including executable programs. Most of the files in the Linux file system are of this type.

DirectoriesA directory is a file that contains information about other files and

directories, and provides pointers to them. It performs a similar function to a folder in a filing cabinet, in that it enables you to group related files in an organized fashion. However, whereas folders can normally contain files only,

Dept. of Computer Science And Applications, SJCET page 29

Page 6: Module-II

MODULE 2 MCA-503 Linux Internals ADMN 2009-‘10

directories can contain additional directories, often referred to as subdirectories.

Symbolic linksA symbolic – or soft – link points to the name and location of a

completely separate file. So when you open, copy, move or otherwise refer to the link, the operation is in fact performed on the referenced file. This distinction is usually invisible to the user. If the referenced file is removed or renamed, the link is broken and an error occurs if you try to open it.

We can also create hard links. A hard link points to the actual data in a file in exactly the same way as an ordinary file does. Therefore, other than the name, there is no difference between the original file and a hard link that points to the same data, and both can be regarded as ordinary files. We can distinguish a hard link from any other ordinary file only by the number of links that each one has. The number of links is displayed in the second field of an ls -l listing. If this number is greater than one, then there are additional hard links to the data.

Device filesAll the physical devices that Linux uses are represented by device

files. Device files can be classified as character special or block special. Character-special files represent devices that interact with Linux on a character-by-character, or serial, basis. Printers and terminals are examples of this type of device. Block-special files represent devices such as hard or floppy disks and CD-ROMs, which interact with Linux using blocks of data.

All the device files are contained in the /dev directory. Device files are extremely powerful because they enable users to access hardware devices such as disk drives, modems, and printers as though they were data files. Therefore, we can move, copy, and transfer data between such devices easily, often without having to use special commands or syntax.

2.5 Directory Commands in LinuxDirectory commands include the command for create, delete and

various other operations like change directory, rename ..etc.

Dept. of Computer Science And Applications, SJCET page 30

Page 7: Module-II

MODULE 2 MCA-503 Linux Internals ADMN 2009-‘10

2.5.1 cd

Change Directory - change the current working directory to a specific Folder.

Syntax cd [Options] [Directory]

Key -P Do not follow symbolic links -L Follow symbolic links (default)

If directory is not given, the value of the HOME shell variable is used. That is, the command cd without any argument will change to the home directory of the user by default.If the shell variable CDPATH exists, it is used as a search path.

If directory begins with a slash, CDPATH is not used.

The return status is zero if the directory is successfully changed, non-zero otherwise.

Examples

Move to the sybase folder$ cd /usr/local/sybase$ pwd/usr/local/sybase

Change to another folder$ cd /var/log$ pwd/var/log

Quickly get back$ cd - $ pwd/usr/local/sybase

move up one folder$ cd ..$ pwd/usr/local/

Dept. of Computer Science And Applications, SJCET page 31

Page 8: Module-II

MODULE 2 MCA-503 Linux Internals ADMN 2009-‘10

$ cd (Back to your home folder)

2.5.2 pwd

Present Working Directory ( Current Working Directory).Syntax pwd [-LP]Options -P : The pathname printed will not contain symbolic links. -L : The pathname printed may contain symbolic links.

$pwd/var/log

The default action is to show the current folder as an absolute path.All components of the path will be actual folder names - none will be symbolic links.

2.5.3. mkdir Create new folder(s), if they do not already exist.SYNTAX mkdir [Options] folder... mkdir "Name with spaces"

OPTIONS -m, --mode=MODE set permission mode (as in chmod), not rwxrwxrwx - umask -p, --parents no error if existing, make parent directories as needed --verbose print a message for each created directoryExamples:

$ mkdir my_files

This command creates a directory called my_files in the current working directory mkdir creates the standard entries . (dot) for the current folder and .. (dot dot) for its parentAny folder name that includes spaces may appear in some applicationsas "Name%20with%20spaces"

Dept. of Computer Science And Applications, SJCET page 32

Page 9: Module-II

MODULE 2 MCA-503 Linux Internals ADMN 2009-‘10

2.5.4. rmdir

Remove directory, this command will only work if the folders are empty.Syntax rmdir [options]... folder(s)...

Options --ignore-fail-on-non-empty Ignore each failure that is solely because the directory is non-empty.

-p, --parents Remove explicit parent directories if being emptied

--verbose Output a diagnostic for every directory processed

--help Display help and exit

--version Output version information and exitExample$ rmdir myfolder This command removes ‘myfolder’ from the current directory if it is empty.

2.5.5 rm

Remove files (delete/unlink) or directories.Syntax rm [options]... file...

Options -d, --directory unlink directory, even if non-empty (super-user only)

-f, --force ignore nonexistent files, never prompt

-i, --interactive prompt before any removal

-r, -R, --recursive remove the contents of directories recursively

-v, --verbose explain what is being done

Dept. of Computer Science And Applications, SJCET page 33

Page 10: Module-II

MODULE 2 MCA-503 Linux Internals ADMN 2009-‘10

--help display this help and exit

--version output version information and exit

To remove a file you must have write permission on the file and the folder where it is stored. $rm -rf will recursively remove folders and their contents.

The OWNER of a file does not need rw permissions in order to rm it.

Undeletable filesThe rm command accepts the `--' option which will cause it

to stop processing flag options from that point forward. This allows the removal of file names that begin with a dash (`-'). rm -- -filenameAlternatively use an absolute or relative path reference. rm /home/user/-filenamerm ./-filename

2.6 File commands File commands are used for creating, deleting and renaming files. The operations like copy and copy as a file also done using various file commands.

2.6.1 cp

Copy one or more files to another location.Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.

Syntax cp [options]... Source Dest cp [options]... Source... Directory

Key

-a, --archive same as -dpR

-b, --backup make backup before removal

-d, --no-dereference preserve links

Dept. of Computer Science And Applications, SJCET page 34

Page 11: Module-II

MODULE 2 MCA-503 Linux Internals ADMN 2009-‘10

-f, --force remove existing destinations, never prompt

-i, --interactive prompt before overwrite

-l, --link link files instead of copying

-p, --preserve preserve file attributes if possible

-P, --parents append source path to DIRECTORY

-r copy recursively, non-directories as files

--sparse=WHEN control creation of sparse files

-R, --recursive copy directories recursively

-s, --symbolic-link make symbolic links instead of copying

-S, --suffix=SUFFIX override the usual backup suffix

-u, --update copy only when the SOURCE file is newer than the destination file or when the destination file is missing -v, --verbose explain what is being done -V, --version-control=WORD override the usual version control -x, --one-file-system stay on this file system --help display this help and exit --version output version information and exit.

ExamplesCopy demofile to demofile.bak : $ cp demofile demofile.bakThe command creates a file called demofile.bak in the current working directory, which is similar to demofile.

$cp /home/biju/test.c /home/biju/Cprog/test.cThis program copies test.c to another location. Here the absolute path is given.

2.6.2 mv

Dept. of Computer Science And Applications, SJCET page 35

Page 12: Module-II

MODULE 2 MCA-503 Linux Internals ADMN 2009-‘10

Move or rename files or directories.SYNTAX mv [options]... Source Dest mv [options]... Source... DirectoryIf the last argument names an existing directory, `mv' moves each other given file into a file with the same name in that directory. Otherwise, if only two files are given, it renames the first as the second. It is an error if the last argument is not a directory and more than two files are given.

OPTIONS

-b--backup Make a backup of each file that would otherwise be overwritten or removed.

-f--force Remove existing destination files and never prompt the user.

-i--interactive Prompt whether to overwrite each existing destination file, regardless of its permissions. If the response does not begin with `y' or `Y', the file is skipped.

-S SUFFIX--suffix=SUFFIX Append SUFFIX to each backup file made with `-b'. The backup suffix is ~, unless set with SIMPLE_BACKUP_SUFFIX.

-u--update Do not move a nondirectory that has an existing destination with the same or newer modification time.

-v --verbose Print the name of each file before moving it.

-V METHOD--version-control=METHOD'

Dept. of Computer Science And Applications, SJCET page 36

Page 13: Module-II

MODULE 2 MCA-503 Linux Internals ADMN 2009-‘10

Change the type of backups made with `-b'. METHOD can be: t, numbered make numbered backups nil, existing numbered if numbered backups exist, simple otherwise never, simple always make simple backups

--help display help and exit --version output version information and exit

Examples

Rename the file apple as orange.doc:$mv apple orange.doc Move orange.doc to the Documents folder:mv orange.doc ~/Documents/orange.doc

Rename a bunch of file extensionse.g. change *.txt into *.htm $ for f in *.txt; do mv ./"$f" "${f%txt}htm"; done

`mv' can move only regular files across file systems.If a destination file exists but is normally un writable, standard

input is a terminal, and the `-f' or `--force' option is not given, `mv' prompts the user for whether to replace the file. (You might own the file, or have write permission on its directory.) If the response does not begin with `y' or `Y', the file is skipped.

2.7 TEXT EDITORSA text editor is a computer program that lets a user enter,

change, store, and usually print text (characters and numbers, each encoded by the computer and its input and output devices, arranged to have meaning to users or to other programs). Typically, a text editor provides an "empty" display screen (or "scrollable page") with a fixed-line length and visible line numbers. You can then fill the lines in with text, line by line. A special command line lets you move to a new page, scroll forward or backward, make global changes in the document, save the document, and perform other actions. After saving a document, you can then print it or display it. Before printing or displaying it, you may be able to format it for some specific output device or

Dept. of Computer Science And Applications, SJCET page 37

Page 14: Module-II

MODULE 2 MCA-503 Linux Internals ADMN 2009-‘10

class of output device. Text editors can be used to enter program language source statements or to create documents such as technical manuals. In UNIX systems, the two most commonly used text editors are emacs and vi .

2.7.1 The JOE Editor joe - Joe's Own Editor Description:

JOE is a powerful ASCII-text screen editor. It has a "mode-less" user interface which is similar to many user-friendly PC editors. Users of Micro-Pro's WordStar or Borland's "Turbo" languages will feel at home. JOE is a full featured UNIX screen-editor though, and has many features for editing programs and text.

JOE also emulates several other editors. JSTAR is a close imitation of WordStar with many "JOE" extensions. JPICO is a close imitation of the Pine mailing system's PICO editor, but with many extensions and improvements. JMACS is a GNU-EMACS imitation. RJOE is a restricted version of JOE, which allows you to edit only the files specified on the command line.

Although JOE is actually five different editors, it still requires only one executable, but one with five different names. The name of the editor with an "rc" appended gives the name of JOE's initialization file, which determines the personality of the editor.

JOE is free software; you can distribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation. I have no plans for turning JOE into a commercial or share-ware product. Synopsis:joe [global-options] [ [local-options] filename ]

jstar [global-options] [ [local-options] filename ]

jmacs [global-options] [ [local-options] filename ]

rjoe [global-options] [ [local-options] filename ]

jpico [global-options] [ [local-options] filename ]

Usage:To start the editor, type joe followed by zero or more names of files

you want to edit. Each file name may be preceded by a local option setting (see

Dept. of Computer Science And Applications, SJCET page 38

Page 15: Module-II

MODULE 2 MCA-503 Linux Internals ADMN 2009-‘10

the local options table which follows). Other global options, which apply to the editor as a whole, may also be placed on the command line (see the global options table which follows). If you are editing a new file, you can either give the name of the new file when you invoke the editor, or in the editor when you save the new file. A modified syntax for file names is provided to allow you to edit program output, standard input/output, or sections of files or devices. See the section Filenames below for details. Once you are in the editor, you can type in text and use special control-character sequences to perform other editing tasks. To find out what are the control-character sequences, type ^K H in the editor.

Dept. of Computer Science And Applications, SJCET page 39

Page 16: Module-II

MODULE 2 MCA-503 Linux Internals ADMN 2009-‘10

Pico Pico, short for Pine Composer, started life as the built-in editor for the Pine email program. Pico is designed to be a simple, easy-to-use text editor with a layout very similar to the pine mailer . The Pico text editor doesn't have a lot of fancy features, but it's a welcome alternative to the vi or Emacs editors because learning it is quick and easy. It is a command line program invoked by the command: pico filename where filename is replaced with the name of your file. PICO can be used at the machine console or it can be invoked via a remote connection

Navigating You can move your cursor by using the arrow keys; you may also use any of the following editing commands: Ctrl-a Beginning of current line Ctrl-f move Forward a character. Ctrl-e End of current line Ctrl-b move Backward a character. Ctrl-v Forward one screen Ctrl-p move to the Previous line. Ctrl-y Backward one screen Ctrl-n move to the Next line. Other keys: (F8) move forward a page of text. (F7) move backward a page of text. (F1) help

Dept. of Computer Science And Applications, SJCET page 40

Page 17: Module-II

MODULE 2 MCA-503 Linux Internals ADMN 2009-‘10

The Emacs Editor

Emacs is the extensible, customizable, self-documenting real-time display editor. This Info file describes how to edit with Emacs and some of how to customize it; it corresponds to GNU Emacs version21.3. The Emacs editor is a lot easier to use than vi. There are no silly modes to trip us up--when we want to enter text, we just position the cursor and type. See, what a great idea. It also has built-in help. But while Emacs is a vast improvement over vi, we'll still have to remember quite a few commands to be productive, and we'll probably get a little lost. If vi is a relic of the sixties, Emacs sports the cutting-edge technology of the early eighties. If you get into a situation where Emacs seems to be stuck, or if we don't know what to do, press ctrl-G and things will return to normal. (The ctrl-G key cancels the current operation in Emacs.) Saving a File

To save our work without leaving Emacs, press ctrl-X (for exit) and then ctrl-S (for save)

Positioning the Cursor

Move cursor one space right (also ctrl-N).

Move cursor one space left (also ctrl-P).

Move cursor up one line (also ctrl-F).

Move cursor down one line (also ctrl-B).

ctrl-V Move forward one screen (also pgup).

esc-V Move backward one screen (also pgdn).

esc-< Move to beginning of file.

esc-> Move to end of file.

esc-F Move cursor to next word.

esc-B Move cursor back one word.

home Move cursor to beginning of line (also ctrl-A).

end Move cursor to end of line (also ctrl-E).

del Delete character at cursor (also ctrl-D).

ctrl-K Delete to end of line.

ctrl-Y Undelete the last thing you deleted.

ctrl-X U Undo the last command.

ctrl-S Perform a search (forward).

ctrl-R Perform a search (backward).

ctrl-H View online help.

ctrl-H T View Emacs tutorial

Dept. of Computer Science And Applications, SJCET page 41

Page 18: Module-II

MODULE 2 MCA-503 Linux Internals ADMN 2009-‘10

2.7.4 The vi EditorAll Linux configuration files are written in plain English, easy to

read and to adapt. You use a text-editor to write or make changes to such files. The two most popular, powerful text editors, both of which are found in every Linux distro, are ‘vi’ and ‘emacs’.

Most GUI-based editors, such as Kedit, are easier to manage. But don't make the mistake of thinking that a GUI-based editor is all you need. There are situations that crop up with Linux that require a text-mode editor -- in other words, when you don't have the luxury of accessing a GUI desktop at all. vi and emacs are the only tools that come with every Linux distro that work in text mode.

vi was originally developed by William Joy at Berkeley University and first officially included in AT&T System 5 Unix. It started out as a line-oriented editor for dumb terminals. The vi editor, or vi iMproved (VIM) is an enhancement by Bram Moolenaar; it's the version that people use today.

Getting StartedTo start Vi, open a terminal or console and simply type "vi"

(without the quotation marks) followed by the name of any existing file or a new file you want to create.

Vi works in two main modes, one for editing text and the other for giving commands. To switch between the two modes you use the I and Esc keys. The program opens in the Command mode, which is used for cursor movements, delete, cut, copy, paste, and saving changes. The Insert mode is what you'll work in most of the time. You use it to make changes in an open file. Enter the Insert mode by pressing the I key. Newer Vi versions will display the word "INSERT' on the bottom line while you're in Insert mode.

Press the Esc key to switch vi back to Command mode. As soon as you hit the Esc key the text "INSERT" on the bottom line disappears.You save your changes to the open file from the Command mode. Press Shift-ZZ (or the key combination ‘:wq’) to save and quit.

If you make a mistake when saving a file, such as pressing Ctrl-ZZ or closing Vi before saving the file, you'll end up with a swap file ,in addition to the original file. Usually the swap file will have the .swp extension. The original file will not contain the recent changes you made; attempting to reopen it will result in an error message. The swap file is not readable but can be recovered by typing a command like this at the $ prompt and pressing Enter:

Dept. of Computer Science And Applications, SJCET page 42

Page 19: Module-II

MODULE 2 MCA-503 Linux Internals ADMN 2009-‘10

vi -r {your file name}.In some extreme cases, recovery is not possible. But in most cases, such as closing vi before saving, a system crash, or a power failure, recovery works very well. After you recover, you must manually delete the swap file using a command like this at the $ prompt:$rm .{your file name}.swp

General StartupTo use vi: vi filenameTo exit vi and save changes: ZZ or :wqTo exit vi without saving changes: :q!To enter vi command mode: [esc]

Counts A number preceding any vi command tells vi to repeat

that command that many times.

Cursor Movement

h move left (backspace)

j move down

k move up

l move right (spacebar)

[return] move to the beginning of the next line

$ last column on the current line

0 move cursor to the first column on the current line

^ move cursor to first nonblank column on thecurrent line

w move to the beginning of the next word or punctuation mark

W move past the next space

b move to the beginning of the previous word

Dept. of Computer Science And Applications, SJCET page 43

Page 20: Module-II

MODULE 2 MCA-503 Linux Internals ADMN 2009-‘10

or punctuation mark

B move to the beginning of the previous word,ignores punctuation

e end of next word or punctuation mark

E end of next word, ignoring punctuation

H move cursor to the top of the screen

M move cursor to the middle of the screen

L move cursor to the bottom of the screen

Screen Movement

G move to the last line in the file

xG move to line x

z+ move current line to top of screen

z move current line to the middle of screen

z- move current line to the bottom of screen

^F move forward one screen

^B move backward one line

^D move forward one half screen

^U move backward one half screen

^R redraw screen ( does not work with VT100 type terminals )

Dept. of Computer Science And Applications, SJCET page 44

Page 21: Module-II

MODULE 2 MCA-503 Linux Internals ADMN 2009-‘10

^L redraw screen ( does not work with Televideo terminals )

Inserting

r replace character under cursor with next character typed

R keep replacing character until [esc] is hit

i insert before cursor

a append after cursor

A append at end of line

O open line above cursor and enter append mode

Deleting

x delete character under cursor dd delete line under cursor

dw delete word under cursor db delete word before cursor

Copying Code

yy (yank)'copies' line which may then be put bythe p(put) command. Precede with a count formultiple lines.

Put Command brings back previous deletion or yank of lines,

words, or characters

P bring back before cursor

p bring back after cursor

Dept. of Computer Science And Applications, SJCET page 45

Page 22: Module-II

MODULE 2 MCA-503 Linux Internals ADMN 2009-‘10

Find Commands

? finds a word going backwards

/ finds a word going forwards

f finds a character on the line under thecursor going forward

F finds a character on the line under thecursor going backwards

t find a character on the current line goingforward and stop one character before it

T find a character on the current line goingbackward and stop one character before it

; repeat last f, F, t, T

Miscellaneous Commands

. repeat last command

u undoes last command issued

U undoes all commands on one line

xp deletes first character and inserts aftersecond (swap)

J join current line with the next line

^G display current line number

% if at one parenthesis, will jump to its mate

mx mark current line with character x

'x find line marked with character x

Dept. of Computer Science And Applications, SJCET page 46

Page 23: Module-II

MODULE 2 MCA-503 Linux Internals ADMN 2009-‘10

NOTE: Marks are internal and not written to the file.

Line Editor ModeAny commands form the line editor ex can be issued upon entering line mode.To enter: type ':'To exit: press[return] or [esc]

READING FILEScopies (reads) filename after cursor in file currently editing:r filename

WRITE FILE:w saves the current file without quitting

MOVING:# move to line #:$ move to last line of file

2.8 Locating files

Files can be found under Linux in many different ways. Using the ‘find’ tool is one of the best ways to find files. The find tool has a huge number of parameters which can be set so that Linux finds exactly those files that you were searching for. Many users use the find tool with just the basic parameters. Another tool for locating files is ‘locate’. Its simpler than find command and only provide the exact location of the file.

2.8.1 find

Here are a few ways to use find

$ find / -name 'program.c' 2>/dev/null$ find / -name 'program.c' 2>errors.txt

Dept. of Computer Science And Applications, SJCET page 47

Page 24: Module-II

MODULE 2 MCA-503 Linux Internals ADMN 2009-‘10

/ Start searching from the root directory (i.e / directory)-name

Given search text is the filename rather than any other attribute of a file'program.c'

Search text that we have entered. Always enclose the filename in single quotes.

Note : 2>/dev/null is not related to find tool as such. 2 indicates the error stream in Linux, and /dev/null is the device where anything you send simply disappears. So 2>/dev/null in this case means that while finding for the files, in case any error messages pop up simply send them to /dev/null i.e. simply discard all error messages.

Alternatively you could use 2>error.txt where after the search is completed you would have a file named error.txt in the current directory with all the error messages in it.

$ find /home/biju -name 'index*'$ find /home/biju -iname 'index*'

The 1st command would find files having the letters index as the beginning of the file name. The search would be started in the directory /home/biju and carry on within that directory and its subdirectories only.The 2nd command would search for the same, but the case of the filename wouldn't be considered. So all files starting with any combination of letters in upper and lower case such as INDEX or indEX or index would be returned.

$ find -name met*The above command would start searching for the files that begin

with the letters 'met' within the current directory and the directories that are present within the current directory. Since the directory is not specified as the the second parameter, Linux defaults to using the current directory as the one to start the search in.

$ find /mp3collection -name '*.mp3' -size -5000k$ find / -size +10000k

The 1st command would find within a directory called /mp3collection, only those mp3 files that have a size less than 5000 Kilobytes

Dept. of Computer Science And Applications, SJCET page 48

Page 25: Module-II

MODULE 2 MCA-503 Linux Internals ADMN 2009-‘10

( < 5MB) .The 2nd command would search from the / directory for any file that is larger than 10000k (> 10MB)

$ find /home/biju -amin -10 -name '*.c'$ find /home/biju -atime -2 -name '*.c'$ find /home/biju -mmin -10 -name '*.c'$ find /home/biju -mtime -2 -name '*.c'

The 1st commmand searches for those files that are present in the directory /home/biju and its subdirectoires which end in .c and which have been accessed in the last 10 minutes. The 2nd command does the same but searches for those files that have been accessed in the last 10 hours. The 3rd and the 4th commands do the same as the 1st and 2nd commands but they search for modified files rather than accessed files. Only if the contents of the files have been modified, would their names be returned in the search results.

$ find / -mount -name 'win*'This command searches for files starting with the letters 'win' in

their filenames. The only difference is that the mounted filesystems would not be searched for this time. This is useful when you have your Windows partitions mounted by default. And a search for 'win' might return many files on those partitions, which you may not be really interested in. This is only one use of -mount parameter.

$ find /mp3-collection -name 'Metallica*' -and -size +10000k$ find /mp3-collection -size +10000k ! -name "Metallica*"$ find /mp3-collection -name 'Metallica*' -or -size +10000k

Boolean operators such as AND, OR and NOT make find an extremely useful tool.

The 1st command searches within the directory /mp3-collection for files that have their names beginning with 'Metallica' and whose size is greater than 10000 kilobytes (> 10 MB).

The 2nd command searches in the same directory as above case but only for files that are greater than 10MB, but they should not have 'Metallica' as the starting of their filenames.

The 3rd command searches in the same directory for files that begin with 'Metallica' in their names or all the files that are greater than 10 MB in size.

Dept. of Computer Science And Applications, SJCET page 49

Page 26: Module-II

MODULE 2 MCA-503 Linux Internals ADMN 2009-‘10

The exec option is probably the most important feature of the find tool. The exec command allows you to execute a particular command on the results of the find command. A simple demonstration of this feature is shown below. Suppose you wanted to see the details of the files (read, write, execute permission, file size, owner etc..) that have been returned as a search result you could do the following.

$ find / - name 'Metallica*' -exec ls -l {\}\ \;

This command would find all the files on your system that begin with the letters 'Metallica' and would then execute the 'ls -l' command on these files. So basically you would be able to see the details of the files that were returned according to your search criteria.

The words following the -exec option is the command that you want to execute i.e. ls -l in this case.{\}\ is basically an indicator that the filenames returned by the search should be substituted here.\; is the terminating string, and is required at the end of the command.

2.8.2 locate

The simplest way to find files under Linux is to use the locate program.

Here are various ways to use locate tool$ locate index.html

It would produce a list of the locations where you could find files that are named as index.html. This might produce results as follows

/home/pamela/index.html/usr/local/games/pam/index.html

The option -q could be used with locate command to suppress the error messages. Error messages would typically be messages stating that permission to access files were not allowed since you are only a user (not superuser).The -q option would suppress any other error messages as well.

$ locate "*.dat" -q

Dept. of Computer Science And Applications, SJCET page 50

Page 27: Module-II

MODULE 2 MCA-503 Linux Internals ADMN 2009-‘10

The option -n could be used with locate command to limit the number of returned results to a specific number. E.g. you could ask for only 10 search results by the following command

$ locate "*.c" -n 10

This would return the first 10 files that end in .c that Linux finds.

The option -i could be used with locate command to perform a case insensitive search. The case of the filenames would not be considered

$ locate INDEX.HTML -i

2.9 Redirection

2.9.1 Standard Output

Most command line programs that display their results do so by sending their results to a facility called standard output. By default, standard output directs its contents to the display. To redirect standard output to a file, the ">" character is used like this:

[biju@linuxbox biju]$ ls > file_list.txt

In this example, the ls command is executed and the results are written in a file named file_list.txt. Since the output of ls was redirected to the file, no results appear on the display.

Each time the command above is repeated, file_list.txt is overwritten (from the beginning) with the output of the command ls. If you want the new results to be appended to the file instead, use ">>" like this:

[biju@linuxbox biju]$ ls >> file_list.txt

When the results are appended, the new results are added to the end of the file, thus making the file longer each time the command is repeated. If the file does not exist when you attempt to append the redirected output, the file will be created.

2.9.2 Standard Input

Dept. of Computer Science And Applications, SJCET page 51

Page 28: Module-II

MODULE 2 MCA-503 Linux Internals ADMN 2009-‘10

Many commands can accept input from a facility called standard input. By default, standard input gets its contents from the keyboard, but like standard output, it can be redirected. To redirect standard input from a file instead of the keyboard, the "<" character is used like this:

[biju@linuxbox biju]$ sort < file_list.txt

In the above example we used the sort command to process the contents of file_list.txt. The results are output on the display since the standard output is not redirected in this example. We could redirect standard output to another file like this:

[biju@linuxbox biju]$ sort < file_list.txt > sorted_file_list.txt

As you can see, a command can have both its input and output redirected. Be aware that the order of the redirection does not matter. The only requirement is that the redirection operators (the "<" and ">") must appear after the other options and arguments in the command.

Different ways to use Redirection Operators

Suppose you want to create a text file quickly

$ cat > filenameThis is some text that I want in this file^D

That's it!! Once you type the ' cat ' command, use the Redirection operator and add a name for a file. Then start typing your line. And finally press Ctrl+D. You will have a file named ' filename ' in the same directory.

Suppose you want to add a single line to an existing file.

$ echo "this is a new line" >> exsisting_file

That would add the new line to the file named ' existing_file ' . Remember to use ' >> ' instead of ' > ' else you would overwrite the file.

Suppose you wanted to join 2 files

$ cat file2 >> file1

Dept. of Computer Science And Applications, SJCET page 52

Page 29: Module-II

MODULE 2 MCA-503 Linux Internals ADMN 2009-‘10

Wow!! That a much neater way then to open a text editor and copy paste. The contents of ' file2 ' would be added to ' file1 ' .

Suppose you want to join a couple of files

$ cat file1 file2 > file3

This would add the contents of ' file1 ' and ' file2 ' and then write these contents into a new file named ' file3 '.

Redirection works with many commands besides normal ones such as ' cat ' or ' ls ' . One example I could give you is in case you are programming using any language you could redirect the output messages of the compilation of your code so that you can view them later on. There are lots of commands where you can use Redirection.

2.9.3 Pipes

By far, the most useful and powerful thing you can do with I/O redirection is to connect multiple commands together with what are called pipes. With pipes, the standard output of one command is fed into the standard input of another.

Examples:

[biju@linuxbox biju]$ ls -l | more

In this example, the output of the ls command is fed into more. By using this "| more" trick, you can make any command have scrolling output.

Table 2.1

Examples of commands used together with pipes

Command What it does

ls -lt | headDisplays the 10 newest files in the current directory.

du | sort -nrDisplays a list of directories and how much space they consume, sorted from the largest to the smallest.

Dept. of Computer Science And Applications, SJCET page 53

Page 30: Module-II

MODULE 2 MCA-503 Linux Internals ADMN 2009-‘10

find . -type f -print | wc -lDisplays the total number of files in the current working directory and all of its subdirectories.

2.10 Filters

One class of programs you can use with pipes is called filters. Filters take standard input and perform an operation upon it and send the results to standard output. In this way, they can be used to process information in powerful ways. Here are some of the common programs that can act as filters:

Table 2.2

Common filter commands

Program What it does

sort Sorts standard input then outputs the sorted result on standard output.

uniqGiven a sorted stream of data from standard input, it removes duplicate lines of data (i.e., it makes sure that every line is unique).

grepExamines each line of data it receives from standard input and outputs every line that contains a specified pattern of characters.

prTakes text input from standard input and splits the data into pages with page breaks, headers and footers in preparation for printing.

headOutputs the first few lines of its input. Useful for getting the header of a file.

tailOutputs the last few lines of its input. Useful for things like getting the most recent entries from a log file.

tr

Translates characters. Can be used to perform tasks such as upper/lowercase conversions or changing line termination characters from one type to another (for example, converting DOS text files into Unix style text files).

sedStream editor. Can perform more sophisticated text translations than tr.

awkAn entire programming language designed for constructing filters. Extremely powerful.

Dept. of Computer Science And Applications, SJCET page 54

Page 31: Module-II

MODULE 2 MCA-503 Linux Internals ADMN 2009-‘10

2.11 Example for filters and pipes

[ biju@linuxbox biju] $ rpm –qa|grep mysqlThis command lists the rpm packages for mysql that have been installed in the system.[ biju@linuxbox biju] $ ls –ltr|grep sjcetThe above command displays all the files that matches ‘sjcet’ with all the descriptions.

*********************

Dept. of Computer Science And Applications, SJCET page 55