Linux Fundamentals

We cover several basic commands in Chapters 6 , 7 , and 8 that are fundamental to the way you navigate around Linux. Some commands allow you to manage files and directories, look through different files, or manipulate files. Other critical commands help you manage your partitions. Because the vi text editor may be all that you have available on a rescue floppy, understanding vi commands is also critical.

The format of this section is somewhat different, since the commands are well explained in their respective chapters.

Navigation

Basic navigational commands are briefly described in Table A.3. These commands are essential for anyone who wants to get around the Linux command-line interface. For more information on these commands, refer to Chapter 06 .

Table A.3: Navigational Commands

Command

Description

cd name

Changes the directory; the directory name can be in absolute format, such as /etc/xinetd.d , or relative format, such as usr/source .

dir

Lists files in the current directory in a specialized manner; a special version of ls; some Linux distributions set alias dir=ls -l .

ls

Lists the files in the current directory; many switches are available.

pwd

Lists the present working directory, so you know where you are.

Managing Files and Directories

Basic commands for managing files and directories are briefly described in Table A.4. Managing files and directories is another essential skill at the command-line interface. For more information on these commands, refer to Chapter 06 .

Table A.4: File Management Commands

Command

Description

cp file1 file2

Copies a file; with appropriate switches, this command can be used to copy the contents of a directory and its subdirectories.

file filename

Determines the file type of the file; options include text, image data, gzip compressed file, cpio archive, mail text, and more.

ln file1 file2

Links two different files. A hard link creates a full copy of the same file; a soft link creates a pointer.

mkdir name

Creates a new directory. The name can be in an absolute or relative path format; using the proper switches, you can even create multiple levels of subdirectories with one command.

mv file1 file2

Deletes and copies a file.

rm filename

Deletes a file; using the right switches, you can delete entire directories and subdirectories. This can be a very dangerous command, especially for the root user .

rmdir dirname

Deletes a directory; using the right switches, you can delete entire directories and subdirectories. By default, the directory must be empty.

touch filename

Creates an empty file with filename .

umask abcd

Sets default permissions for newly created files by that user.

Reading through Different Files

Table A.5 describes basic commands for searching through files. You need to be able to look through various files to manage your Linux operating system. For more information on these commands, see Chapter 06 .

Table A.5: File-Reading Commands

Command

Description

cat filename

Reads the contents of a file; also scrolls one or more text files to a screen. This command can also be used to redirect (>) the contents of an image file, such as the Red Hat installation bootdisk.img , to a floppy device.

head filename

Sends the first few lines of a file to the screen; the default is 10 lines.

less filename

Opens a file in a text-style reader; allows up and down scrolling one screen at a time. vi search commands are allowed.

more filename

Opens a file in a text-style reader; allows scrolling one screen at a time. vi search commands are allowed.

Manipulating Files

Basic commands for manipulating files are briefly described in Table A.6. You need to be able to examine the contents in order to manipulate files on your Linux operating system. For more information on these commands, refer to Chapter 06 .

Table A.6: File-Manipulation Commands

Command

Description

find / -name file -print

Finds the location of the file, starting the search in the root ( / ) directory. You can use wildcards, and start in other directories.

grep string file

Searches for a text string in the specified file. You can use wildcards to extend the search to a series of files.

locate file

Searches through the slocate database for the location of a specified file. Wildcards are assumed; for example, the locate abc command will also find the location of a file named abcd . The slocate database is refreshed as a default cron job every night.

wc file

Displays the number of lines, words, and characters in a text file.

Managing Partitions

Commands are available to help you manage partitions. Several of these commands are also related to the way you configure partitions ( fdisk , fips , parted ), and are covered earlier in this appendix. The commands shown in Table A.7 are addressed in more detail in Chapter 07 .

Table A.7: Partition-Management Commands

Command

Description

df

Reports partitions on the current hard disk.

du

Notes file-space usage of each file; includes files in subdirectories.

dumpe2fs device

Lists all information related to the device (e.g., /dev/sdb2 ).

e2label device label

If you just specify the device (e.g. /dev/hda1 ), this command returns the current label. Otherwise , it assigns a new label; this should correspond to an entry in /etc/fstab .

fsck device

Checks and repairs a Linux filesystem; you should specify the device associated with an unmounted partition. This command normally detects the filesystem and uses the correct format; otherwise, you can use a command such as fsck.minix device , fsck.ext2 device , or e2fsck device . The last two commands work for both the ext2 and ext3 formats.

mount filesystem device

Mounts a filesystem such as /home on a device such as /dev/hda2 .

tune2fs device

Converts a partition from ext2 to ext3.

umount filesystem

Unmounts the specified filesystem, such as /home .

The vi Editor

As discussed in Chapter 06 , three modes are available in vi : command mode, insert mode, and execute mode. For more information about the available commands, see the Linux Instant Command Reference .

Command Mode

In vi command mode, you can manipulate existing text in a file. Table A.8 shows some of the available commands. These commands affect the text file based on the current location of the cursor.

Table A.8: vi Command Mode Commands

Command

Function

cc

Deletes the current line and moves to insert mode so you can type in a replacement.

cw

Deletes the current word and moves to insert mode so you can type in a replacement.

dd

Deletes the current line.

dw

Deletes the current word.

D

Deletes the remainder of the line, starting with the current position of the cursor.

p

Copies the register , the current contents of the buffer, to the line after the cursor.

:reg

Shows the contents of the register.

u

Undoes the last change.

U

Restores the current line.

nY

Copies n lines into the register. See the p command.

Insert Mode

There are several ways to start insert mode in vi . Once in this mode, you can enter the desired text. Some vi command mode commands, such as cw , also automatically start insert mode. See Table A.9 for some of the available commands. These commands are based on the current location of the cursor.

Table A.9: vi Insert Mode Commands

Command

Function

a

Inserts new text after the current location of the cursor.

A

Inserts new text at the end of the current line.

Esc

Returns to command mode.

i

Inserts new text at the current cursor location.

I

Inserts new text at the beginning of the line.

o

Starts a new line after the current line.

O

Starts a new line before the current line.

Execute Mode

You can run standard shell commands from inside the vi editor; just start the command with an :! , followed by the shell command. A couple of special execute mode commands allow you to write and exit the file (see Table A.10). You can combine these commands; for example, :wq writes the file and exits the vi editor.

Table A.10: Execute Commands

Command

Function

:! command

Runs the specified shell command . Starts in the directory where you started the vi text editor.

q

Exits the file. If you ve made changes, you can leave vi without writing those changes with the q! command.

w

Writes the current file.

 


Mastering Red Hat Linux 9
Building Tablet PC Applications (Pro-Developer)
ISBN: 078214179X
EAN: 2147483647
Year: 2005
Pages: 220

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net