Files Directories Vi

26
Linux File System Structure In order to find files within the file system, a standard exists that identifies where certain types of files go. This standard is the file system Hierarchy Standard (FHS) maintained by the file system Hierarchy Standard Group. The standard contains a set of requirements and guidelines for file and directory placement under UNIX-like operating systems. Use of the FHS provides interoperability of applications, system administration tools , development tools, and scripts. The FHS defines many common directories used in UNIX-like operating systems. The FHS document includes other directories not listed here. Refer to the latest FHS document for a complete set of directories (http://www.pathname.com/fhs/ ). The Linux Standards Base (LSB) is using in Linux systems http://www.freestandards.org/en/LSB

Transcript of Files Directories Vi

8/8/2019 Files Directories Vi

http://slidepdf.com/reader/full/files-directories-vi 1/26

Linux File System StructureIn order to find files within the file system, a standard exists

that identifies where certain types of files go.T his standard is the file system Hierarchy Standard (FHS) maintainedby the file system Hierarchy Standard Group.T he standard contains a set of requirements and guidelines for fileand directory placement under UNIX-like operating systems.Use of the FHS provides interoperability of applications, systemadministration tools, development tools, and scripts.T he FHS defines many common directories used in UNIX-likeoperating systems.T he FHS document includes other directories not listed here.Refer to the latest FHS document for a complete set of directories(http://www.pathname.com/fhs/ ).

T he Linux Standards Base (LSB) is using in Linux systemshttp://www.freestandards.org/en/LSB

8/8/2019 Files Directories Vi

http://slidepdf.com/reader/full/files-directories-vi 2/26

Linux File System Structure / - T he root directory is the top of the file system hierarchy. T he contents of the

root file system must contain all the files needed to boot, restore, recover, andrepair the system. /bin - T he /bin directory contains commands used by the system administrator andother users. T hese are required commands when no other filesystems are mounted. /boot - T he /boot directory contains all files, except configuration files, needed toboot the operating system. T he /boot stores data that is used before the kernelbegins executing user-mode programs. /dev - T he /dev directory contains file system entries which represent devices that are part of the system. /etc - T he /etc directory contains local configuration files. No binaries arecontained within /etc. /home - T he /home directory contains user directories. T he /home directory isoptional. T ypically, the /home directory contains a subdirectory for each user addedto the system. /lib - T he /lib directory contains shared library images need to boot the systemand run the commands in the root filesystem. /opt - T he /opt directory contains add-on application software packages.

8/8/2019 Files Directories Vi

http://slidepdf.com/reader/full/files-directories-vi 3/26

Linux File System Structure /proc - T he /proc file system is used to handle storage and retrieval of processinformation. /root - T he /root directory is the home directory of the root user. /sbin - T he /sbin directory contains utilities used for system administration. T heseutilities are executed after /usr is known to be mounted and there are no problems. /tmp - T he /tmp directory contains temporary files. T hese files are lost if thesystem restarts.

8/8/2019 Files Directories Vi

http://slidepdf.com/reader/full/files-directories-vi 4/26

Linux File System Structure /usr - T he /usr directory contains files that can be shared across allhosts. T his directory is mounted read-only and contains the followingsubdirectories or symbolic links to directories:

/usr/bin contains most user commands /usr/include contains header files included by C programs

/usr/lib

contains libraries /usr/local local hierarchy /usr/sbin contains non-vital system binaries /usr/share contains architecture-independent data

8/8/2019 Files Directories Vi

http://slidepdf.com/reader/full/files-directories-vi 5/26

Linux File System Structure /var - T he /var directory contains variable data files. T his includes spooldirectories, log files, and temporary files. Some directories within /varare sharable and some are unsharable. T he following directories arerequired in /var:

/var/cache contains application cache data /var/lib contains variable state information /var/local contains data for /usr/local /var/lock contains lock files /var/log contains log files and directories /var /opt - contains variable data for /opt /var/run contains data relevant to running processes /var/spool contains application spool data /var/tmp contains temporary files preserved between system reboots

8/8/2019 Files Directories Vi

http://slidepdf.com/reader/full/files-directories-vi 6/26

Linux File System StructureT he file system structure used by Linux is a hierarchal list of directoriescalled a directory tree. T he top of the tree is the root directory indicatedby a /. T he upper directories are called parent directories and thedirectories beneath them are called child directories.

8/8/2019 Files Directories Vi

http://slidepdf.com/reader/full/files-directories-vi 7/26

C hanging and Viewing Directories from theC

LI 1When a user logs in to the Linux operating system thedirectory that they will start in is their home directory.

Most users will have /home/userid as their home directory.T he root user has /root as it s home directory.

T o view the contents of the current directory you can type:ls l

T he l option says to give a long list which includes file permissions,owner, group, size (in bytes), date created, and filename.You can view the hidden system files by including the a option:

ls laYou must have permission to read a directory before you

can view it.

8/8/2019 Files Directories Vi

http://slidepdf.com/reader/full/files-directories-vi 8/26

C hanging and Viewing Directories from theC

LI 2You can view specific files by including the filename or a filter of a groupof filenames. T o view the file named install.log type:

ls l install.logYou can list all files that start with the letters install by using the wildcard

character (*). T ype:ls l install*

You can include a path instead of just a filename to list. For example toview the contents of the / directory you can type:

ls /Notice the l option was not used so the directory will display only the file

and directory names, not the properties.T o view the contents of the /var/log directory type:

ls /var/log

8/8/2019 Files Directories Vi

http://slidepdf.com/reader/full/files-directories-vi 9/26

C hanging and Viewing Directories from theC

LI 3Although you can view the contents of any directory from anywhere inthe file system hierarchy you can also change to the individual directories.T o change the current directory to the / directory type:

cd /T o change to jsmith s Documents directory within his home directory type:

cd /home/jsmith/DocumentsT here are shortcut commands you can also use. For example, you can

use the shortcut .. change to the parent directory of the current directoryby typing:

cd ..

You can change to your home directory with the shortcut ~ by typing:cd ~ (or just cd without any options)

T he root user goes back to /root with this command.You can display the current directory by typing the pwd command.

8/8/2019 Files Directories Vi

http://slidepdf.com/reader/full/files-directories-vi 10/26

C reating a Directory and File from the C LI

You create a directory with the mkdir command. T o create a directorycalled scripts in the current directory type:

mkdir scripts You can create a directory anywhere in the Linux file system hierarchy by

including the full path in the directory name.T

o create a directory calledcron in the /etc/skel directory type:mkdir /etc/skel/cron

You must have permission to write in a directory to create a subdirectoryin that directory.

You already know you can create a file with vi and this is a preferredmethod if you are including content. If you only want an empty file usethe touch command by typing:

touch myFile

8/8/2019 Files Directories Vi

http://slidepdf.com/reader/full/files-directories-vi 11/26

R emove a Directory or File from the C LI

T o remove a directory use the rmdir command. T o remove the scriptsdirectory you created in the current directory type:

rmdir scriptsYou must have write permission to remove a directory.T o remove a directory that has other files within it use the rm command

with the r option (recursive). T he rm command is used to delete filesand, with the recursive option, directories and their contents as well. T oremove the /etc/skel/cron directory and any files and directories that maybe within it type:

rm r /etc/skel/cronT o remove one or more files use the rm command. T o remove the file

Welcome from the /etc/skel directory type:rm /etc/skel/Welcome

8/8/2019 Files Directories Vi

http://slidepdf.com/reader/full/files-directories-vi 12/26

8/8/2019 Files Directories Vi

http://slidepdf.com/reader/full/files-directories-vi 13/26

8/8/2019 Files Directories Vi

http://slidepdf.com/reader/full/files-directories-vi 14/26

U sing vi

If you are using the CLI, you have to learn to use a text editorto edit text files.

Most Linux and Unix operating systems use a program calledvi. (Pronounced V-I, as two separate letters.)

T he vi program has been around nearly as long as Unix andcontinues to be the most widely used text editor.vi is short for vi sual editor.

In Linux, vi shows a window of 24 lines of text from a file.T he vi text editor lets you add, change and delete text only.vi does not have any formatting capabilities such as bold,

underline, or line centering.

8/8/2019 Files Directories Vi

http://slidepdf.com/reader/full/files-directories-vi 15/26

U sing viT here are other text editors but mostly they emulate vi and

usually offer a few improvements. T he Fedora Core 5 Linuxuses vim (Vi I Mproved) which is open source and animprovement over vi.

T o start vim you can just type vi and press Enter. If you arecreating a new file, this will open the editor and provide ablank window to start. T o edit an existing or to create a newfile, type vi filename .

vi has three modes of operation:command modeinsert modelast-line

8/8/2019 Files Directories Vi

http://slidepdf.com/reader/full/files-directories-vi 16/26

U sing vi

When you first start vi you are in command mode.From command mode you can move around the screen, entercommands to save the file, search of content in the file, or edit thecontents of the text file.

Insert mode lets you edit the text filePress i to enter insert mode.T o exit insert mode and return to command mode, press the Esc key

Use Last-line mode to issue commands such as search andreplace or to insert the results of a BASH command into the

file. Pressing : enters last-line mode from command mode.ESC gets back to command mode

8/8/2019 Files Directories Vi

http://slidepdf.com/reader/full/files-directories-vi 17/26

vi C ommands 1Display Help with the F1 key or type typing :help and

then pressing Enter.M ove around the screen with keys k, h, l, j. ( T here

must be a document present to use the move keys.)

$ moves to the end of a line

G moves to the end of the file

w moves forward one word

b moves back one word

<ctrl>f moves down one screen

<ctrl>b moves up one screen

k moves up one line

j moves down one line

l moves right one character

h moves left one character

0 moves to the beginning of the line

8/8/2019 Files Directories Vi

http://slidepdf.com/reader/full/files-directories-vi 18/26

vi C ommands 2T o start typing in a new file, press i to enter insert mode.

Anything typed after you are insert mode is displayed on the screenand can be saved with the file.While in insert mode press Enter to type text on the next line.

Insert mode commands are used to enter text in a file.a to append text to the right of the cursorA to append text at the end of the linei to insert text at the current cursor positionI to insert text at the beginning of the lineo to insert text starting on a blank line below the current cursorpositionO to insert text starting on a blank line above the current cursor

position

8/8/2019 Files Directories Vi

http://slidepdf.com/reader/full/files-directories-vi 19/26

vi C ommands 3Delete text from command mode with x or X.

x deletes text under the cursorX deletes text to the left of the cursordd deletes the line 3 dd deletes 3 lines

Save file with :w or :w! .Pressing : enters last-line mode (you must me in command mode toenter last-line mode)T he :w command saves the file, whereas, the :w! saves the file evenif the file is read only.If the text you've type is not yet associated with a file name, thentype :w newfilename where newfilename is the name of the file to becreated on disk.

Save and exit the file with ZZ from command mode.Exit vi with the :q or :q! command.

T he :q command exits the current file and the :q! forces exit of thecurrent file, whether or not the file has been saved.

8/8/2019 Files Directories Vi

http://slidepdf.com/reader/full/files-directories-vi 20/26

vi C ommandsFrom command mode, you can type /pattern where

pattern is the string you are looking for. /pattern looks forward in the file and ? pattern looksbackward in the file.T o repeat the search forward through the file type the letter n ,and to repeat the search backward through the file type theletter NT o find the id: 3 :initdefault: line in the /etc/inittab file you wouldfirst open the file in vi:

vi /etc/inittabT hen search for id: 3 :initdefault: or a portion of it:

/id:T he cursor will jump to the first occurrence of id:

8/8/2019 Files Directories Vi

http://slidepdf.com/reader/full/files-directories-vi 21/26

vi C ommands 4 M oving lines

T o move a single line, position the cursor on the line to bemoved and type dd to delete the line.

You can move more than one line by prefixing the dd commandwith the number of lines to be deleted.T ype yy to yank or copy the line

Position the cursor where you want the line to be and type p

to paste.

8/8/2019 Files Directories Vi

http://slidepdf.com/reader/full/files-directories-vi 22/26

vi C ommands 5 C opy LinesT o copy a single line of text, position the cursor on the

line to be copied and press the letter Y.Y copies the current line to the working buffer of vi.

Position the cursor where you want the line to be and typethe letter P . T o copy multiple lines prefix the Y commandwith the number of lines from the cursor to be copied.

8/8/2019 Files Directories Vi

http://slidepdf.com/reader/full/files-directories-vi 23/26

vi C ommands 6 - Settings

Settings allow you to change the way vi works.T o set vi to display line numbers type :set number .T o view other set commands type :help set from vi commandmode.

8/8/2019 Files Directories Vi

http://slidepdf.com/reader/full/files-directories-vi 24/26

U sing vi to C hange . bash_profile

For the bash shell the user profile is a file in the user homedirectory called . bash_profile .

Any local environment variables for this user can be set withinthe profile.Change directories to /home/jsmith and type:

vi .bash_profileFind the PA T H line by typing: /PA T H

Move to the end of the line by typing:$

Append text to the end of the line by typing:a

T hen add to the PA T H by typing::/home/I T 250

8/8/2019 Files Directories Vi

http://slidepdf.com/reader/full/files-directories-vi 25/26

8/8/2019 Files Directories Vi

http://slidepdf.com/reader/full/files-directories-vi 26/26

U sing vi to C hange . bash_profile

T est your profile change by logging in as the user jsmith. T ype:

su jsmithNotice the prompt for jsmith has changed to display the dateabove the prompt (export PRO MPT _COMM AND=date), the

username and host are separated with a instead of the @ ([\u -\h\w]), and the $ prompt is on a line below the rest (\n).