Files Directories Vi

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 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
Linux File System Structure
• / - The root directory is the top of the file system hierarchy. The contents of the
root file system must contain all the files needed to boot, restore, recover, and
repair the system.
•/bin - The /bin directory contains commands used by the system administrator and
other users. These are required commands when no other filesystems are mounted.
•/boot - The /boot directory contains all files, except configuration files, needed to
boot the operating system. The /boot stores data that is used before the kernel
begins executing user-mode programs.
•/dev - The /dev directory contains file system entries which represent devices that
are part of the system.
•/etc - The /etc directory contains local configuration files. No binaries are
contained within /etc.
•/home - The /home directory contains user directories. The /home directory is
optional. Typically, the /home directory contains a subdirectory for each user added
to the system.
•/lib - The /lib directory contains shared library images need to boot the system
and run the commands in the root filesystem.
•/opt - The /opt directory contains add-on application software packages.
Linux File System Structure
•/proc - The /proc file system is used to handle storage and retrieval of process
information.
•/root - The /root directory is the home directory of the root user.
•/sbin - The /sbin directory contains utilities used for system administration. These
utilities are executed after /usr is known to be mounted and there are no problems.
•/tmp - The /tmp directory contains temporary files. These files are lost if the
system restarts.
Linux File System Structure
• /usr- The /usr directory contains files that can be shared across all
hosts. This directory is mounted read-only and contains the following
subdirectories 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
Linux File System Structure
• /var- The /var directory contains variable data files. This includes spool
directories, log files, and temporary files. Some directories within /var
are sharable and some are unsharable. The following directories are
required 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
Linux File System Structure
• The file system structure used by Linux is a hierarchal list of directories
called a directory tree. The top of the tree is the root directory indicated
by a /. The upper directories are called parent directories and the
directories beneath them are called child directories.
Changing and Viewing Directories from the
CLI 1
•When a user logs in to the Linux operating system the
directory that they will start in is their home directory.
 Most users will have /home/userid as their home directory.
 The root user has /root as it’s home directory.
 To view the contents of the current directory you can type:
ls –l
• The –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 –la
•You must have permission to read a directory before you
can view it.
Changing and Viewing Directories from the
CLI 2
•You can view specific files by including the filename or a filter of a group
of filenames. To view the file named install.log type:
ls –l install.log
•You can list all files that start with the letters install by using the wildcard
character (*). Type:
ls –l install*
•You can include a path instead of just a filename to list. For example to
view 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.
•To view the contents of the /var/log directory type:
ls /var/log
Changing and Viewing Directories from the
CLI 3
•Although you can view the contents of any directory from anywhere in
the file system hierarchy you can also change to the individual directories.
To change the current directory to the / directory type:
cd /
 To change to jsmith’s Documents directory within his home directory type:
cd /home/jsmith/Documents
•There are shortcut commands you can also use. For example, you can
use the shortcut .. change to the parent directory of the current directory
by typing:
cd ..
•You can change to your home directory with the shortcut ~ by typing:
cd ~ (or just cd without any options)
 The root user goes back to /root with this command.
•You can display the current directory by typing the pwd command.
Creating a Directory and File from the CLI
•You create a directory with the mkdir command. To create a directory
called 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. To create a directory called
cron in the /etc/skel directory type:
mkdir /etc/skel/cron
 You must have permission to write in a directory to create a subdirectory
in that directory.
•You already know you can create a file with vi and this is a preferred
method if you are including content. If you only want an empty file use
the touch command by typing:
touch myFile
Remove a Directory or File from the CLI
•To remove a directory use the rmdir command. To remove the scripts
directory you created in the current directory type:
rmdir scripts
•You must have write permission to remove a directory.
•To remove a directory that has other files within it use the rm command
with the –r option (recursive). The rm command is used to delete files
and, with the recursive option, directories and their contents as well. To
remove the /etc/skel/cron directory and any files and directories that may
be within it type:
rm –r /etc/skel/cron
•To remove one or more files use the rm command. To remove the file
Welcome from the /etc/skel directory type:
rm /etc/skel/Welcome
Copying a file from the CLI
•Copy files with the cp command.
 When you copy a file you duplicate the file and it’s contents to another
file.
 Filenames must be unique so if you copy the file to the same directory it
must have a different name.
 To copy /root/myFile to the / directory type:
cp /root/myFile /myFile
 To copy /root/myFile to the / directory with the name myFile2 type:
cp /root/myFile /myFile2
 You can use wildcards with filenames as you did with the ls command. To
copy /myFile and /myFile2 to the /root directory type:
cp /myfile* /root
 If you are replacing an existing file you will be prompted if it can be
overwritten.
Moving a File from the CLI
•To move a file use the mv command. When you move a file to another
file in the same directory you are essentially renaming the file.
 To move /root/myFile to /root/myFile2 type:
mv /root/myFile /root/myFile2
 To move /root/myFile2 to /myFile type:
mv /root/myFile2 /myFile
 To move /myFile to /root/myFile type:
mv /myFile /root/myFile
•You must have read and write permissions to move a file.
Using vi
•If you are using the CLI, you have to learn to use a text editor
to edit text files.
•Most Linux and Unix operating systems use a program called
vi. (Pronounced V-I, as two separate letters.)
 The vi program has been around nearly as long as Unix and
continues to be the most widely used text editor.
 vi is short for visual editor.
•In Linux, vi shows a window of 24 lines of text from a file.
•The 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.
Using vi
•There are other text editors but mostly they emulate vi and
usually offer a few improvements. The Fedora Core 5 Linux
uses vim (Vi IMproved) which is open source and an
improvement over vi.  
•To start vim you can just type vi and press Enter. If you are
creating a new file, this will open the editor and provide a
blank window to start. To edit an existing or to create a new
file, type vi filename.
•vi has three modes of operation:
 command mode
 insert mode
 last-line
Using vi
•When you first start vi you are in command mode.
 From command mode you can move around the screen, enter
commands to save the file, search of content in the file, or edit the
contents of the text file.
•Insert mode lets you edit the text file
 Press i to enter insert mode.
 To exit insert mode and return to command mode, press the Esc key
•Use Last-line mode to issue commands such as search and
replace 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
vi Commands 1
•Display Help with the F1 key or type typing :help and
then pressing Enter.
•Move around the screen with keys k, h, l, j. (There
must be a document present to use the move keys.)

•k moves up one line •$ moves to the end of a line

•j moves down one line •G moves to the end of the file

•l moves right one character •w moves forward one word

•h moves left one character •b moves back one word

•0 moves to the beginning of the line •<ctrl>f moves down one screen
•<ctrl>b moves up one screen
vi Commands 2
•To start typing in a new file, press i to enter insert mode.
 Anything typed after you are insert mode is displayed on the screen
and 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 cursor
 A to append text at the end of the line
 i to insert text at the current cursor position
 I to insert text at the beginning of the line
 o to insert text starting on a blank line below the current cursor
position
 O to insert text starting on a blank line above the current cursor
position
vi Commands 3
•Delete text from command mode with x or X.
 x deletes text under the cursor
 X deletes text to the left of the cursor
 dd deletes the line – 3dd deletes 3 lines
•Save file with :w or :w!.
 Pressing : enters last-line mode (you must me in command mode to
enter last-line mode)
 The :w command saves the file, whereas, the :w! saves the file even if
the file is read only.
 If the text you've type is not yet associated with a file name, then
type :w newfilename where newfilename is the name of the file to be
created on disk.
•Save and exit the file with ZZ from command mode.
•Exit vi with the :q or :q! command.
 The :q command exits the current file and the :q! forces exit of the
current file, whether or not the file has been saved.
vi Commands
•From command mode, you can type /pattern where
pattern is the string you are looking for.
 /pattern looks forward in the file and ?pattern looks
backward in the file.
 To repeat the search forward through the file type the letter n,
and to repeat the search backward through the file type the
letter N
 To find the id:3:initdefault: line in the /etc/inittab file you would
first open the file in vi:
vi /etc/inittab
• Then search for id:3:initdefault: or a portion of it:
/id:
• The cursor will jump to the first occurrence of id:
vi Commands 4 – Moving lines
•To move a single line, position the cursor on the line to be
moved and type dd to delete the line.
 You can move more than one line by prefixing the dd command
with the number of lines to be deleted.
 Type yy to “yank” or copy the line
•Position the cursor where you want the line to be and type p
to paste.
vi Commands 5 – Copy Lines
•To 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 type
the letter P. To copy multiple lines prefix the Y command
with the number of lines from the cursor to be copied.
vi Commands 6 - Settings
•Settings allow you to change the way vi works.
 To set vi to display line numbers type :set number.
 To view other set commands type :help set from vi command
mode.
Using vi to Change .bash_profile
•For the bash shell the user profile is a file in the user home
directory called .bash_profile.
 Any local environment variables for this user can be set within the
profile.
 Change directories to /home/jsmith and type:
vi .bash_profile
 Find the PATH line by typing:
/PATH
 Move to the end of the line by typing:
$
 Append text to the end of the line by typing:
a
 Then add to the PATH by typing:
:/home/IT250
Using vi to Change .bash_profile
 Press Esc to exit the insert mode.
 Move to the end of the file by typing:
G
 Open a blank to insert text by typing:
o
 Enter the lines:
export PROMPT_COMMAND=date
PS1=“[\u - \h\w]\n\$”
 Save the file by typing:
:wq
 And pressing enter
Using vi to Change .bash_profile
 Test your profile change by logging in as the user jsmith. Type:
su – jsmith
 Notice the prompt for jsmith has changed to display the date
above the prompt (export PROMPT_COMMAND=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).

You might also like