Changes

Jump to: navigation, search

Week 2 Notes

22,261 bytes added, 20:49, 30 August 2017
Created page with "= Linux File System = == Characteristics of a Linux File System. == The Linux file system has the following characteristics: * Linux shares file system characteristics with o..."
= Linux File System =
== Characteristics of a Linux File System. ==
The Linux file system has the following characteristics:

* Linux shares file system characteristics with operating systems like
Windows in some areas but differ from Windows in other aspects. For example: like Windows, a
Linux file system, has all its files and directories organized in a tree-like structure but
unlike Windows, there are no drive letters (C: or D:) on a Linux file system (instead all
removable and non-removable devices are available and accessible from the <tt>/dev</tt> directory on
Linux). Similarly, both Linux and Windows have files and directories, but Windows refers
to them as files and folders. So, a folder in Windows is a called a directory in Linux. We use
the Linux terminology in these notes.

* In a Linux file sytem all files and directories appear under a single
parent file (called root),however, unlike Windows, even multiple storage devices appear under
the same root file in a Linux file system.

* Everything within the Linux file system is considered a file. This means
directories in Linux are a special kind of file that can hold zero or more files (including
nested subdirectories) in them.

As mentioned above, all files within the Linux file system are organized in a tree-like structure
and this is why a Linux file system is commonly referred to as a '''hierarchical''' file system.

=== What is a Hierarchical File System? ===
In Linux (or any Unix for that matter) the root directory (denoted by <tt>/</tt>) is source (or uppermost)
directory at the top of all other directories and files. This means '''all''' other files, child, and
grandchild directories are shown as descendants of that single root directory. This hierarchical
structure resembles an 'upside-down tree'. There is actually a command called <tt>tree</tt> that can display
an upside-down tree-like diagram of the Linux (Unix) file system as shown below:

# display files and directories below root (/) with the tree command
$ tree /
/
|-home
| |-les
| |-mark
| |-uli101
| ...
|
|-bin
| |-nled
|
....

In the code shown above, the <tt>$</tt> refers to the prompt that waits for you to type something. That's
where you type the command <tt>tree</tt>; some sample output of the command <tt>tree</tt> is shown below (notice the
leading root directory, i.e. <tt>/</tt> at the top of the output following the <tt>tree</tt> command).

This course will teach you the skills you require to navigate the directory tree in a Linux file
system because learning command-line navigation of the file system is essential for efficient system
usage and administration.

= Some Unix/Linux directories directly below root =
A sample of some of the commonly found subdirectories that lie below the root directory (<tt>/</tt>) is shown
in the table below. The table includes a brief description of the purpose of that subdirectory.

{| class=wikitable



|-
|<tt>/</tt>
|Root directory (ancestor to all directories)

|-
|<tt>/home</tt>
|Used to store usersf home directories

|-
|<tt>/bin</tt>
|Common system binaries (commands)

|-
|<tt>/dev</tt>
|Device files (terminals, printers, etc.)

|-
|<tt>/etc</tt>
|General System Admin. Files (eg passwd)

|-
|<tt>/usr/bin</tt>
|Common utilities (commands) for users

|-
|<tt>/usr/sbin</tt>
|Common utilities for user administration

|-
|<tt>/tmp</tt>, <tt>/var/tmp</tt>
|Temporary files for programs

|-
|<tt>/var</tt>
|Dynamic files (log files)

|}

= Home directory =
* Almost every user in a Linux file system gets a 'home' directory when their account was created.

* This is where the users keep their personal files

* The <tt>~</tt> symbol represents the home directory of the logged in user.

* The <tt>~</tt> symbol may be used in file pathnames. More about file pathnames later.

* The <tt>cd</tt> command when used without any other parameters gets you into your home directory no matter
where you previously were in the Linux file system.

* As Linux is a multi-user filesystem, remember to keep your files private. You shall learn how to
do this in a later class.

= Types of Files =
On a Unix/Linux file system a 'file' can be anything. To an average computer user a file is a
container for: a text document, video, music, photo and so on.

A directory is a special type of file (index file) containing references to other file locations on
the physical disc or to other file related information. Even devices like a terminal, a scanner, or
a printer are also files. You will learn more details about these types of files later in this
course. Any file (or directory) name starting with a period (such as <tt>.</tt> or <tt>..</tt>) is considered a hidden
file (or directory). You can use the <tt>ls -l</tt> command to determine the type of file.

For Example:

$ ls -l /dev/tty
crw-rw-rw- 1 root root 5, 0 2003-03-14 08:07 /dev/tty

$ ls -l monday.txt w1.c
-rw-r--r-- 1 someuser users 214 2006-01-23 14:20 monday.txt
-rw-r--r-- 1 someuser users 248 2005-10-12 13:36 w1.c

$ ls -ld uli101
drwxr-xr-x 2 someuser users 4096 2006-01-17 16:43 uli101

<blockquote>Notes for listing above:

* Use the <tt>-l</tt> option of the <tt>ls</tt> command to get detailed information about a file.

* Use the <tt>-ld</tt> option of the <tt>ls</tt> command to get detailed information for just the directory.

* The first character in detailed listing determines the file type, so:

* - indicates a regular file

* b or c indicates a device file

* d indicates a directory file
<blockquote>

= Hidden Files =

=== What are hidden files? ===
A filename that begins with a '<tt>.</tt>' is a hidden file. So, if a filename name starts with a '<tt>.</tt>' as its
first character, like <tt>.profile</tt> for example, it is a considered to be a hidden file by Linux commands
and is suppressed from the normal listing of files. See the examples shown below:

$ pwd
/home/murray

$ ls
uli101.txt

$ ls -a
. .. .profile uli101.txt

$ ls -A
.profile uli101.txt

<blockquote>In the file listing shown above

* <tt>pwd</tt> displays the present working directory. This command is used to display where on in the Linux
file system the logged in user is presently working in. In the example shown above, that location
is <tt>/home/murray</tt>

* <tt>ls</tt> displays the normal listing of file, i.e. all non-hidden files in the present working directory
(this is usually abbreviated to pwd in these notes).

* <tt>ls -a</tt> displays all files, i.e. hidden and non-hidden files, in the present working directory. In
the listing of <tt>ls -a</tt> the first two items (i.e. '<tt>.</tt>' and '<tt>..</tt>') are hidden directories to indicate
the present working directory (the item: '<tt>.</tt>') and the item '<tt>..</tt>' indicates the parent directory of
the present working directory. Both (<tt>.</tt> and <tt>..</tt>) are found in every directory in the Linux file
system. They cannot be removed, by any user, because both these directories are required for the
internal working of the Linux file system.

* <tt>ls -A</tt> displays 'almost' all files, that is all hidden files like <tt>.profile</tt> except the first two
items <tt>.</tt> and <tt>..</tt>. This means <tt>.</tt> and <tt>..</tt> are never shown within the <tt>ls -A</tt> output listing.<blockquote>

=== Why hide files? ===
* To clean up directories.

* To hide backups.

* To protect important files from accidental deletion.

* Since directories are really files, you can hide them as well.

= Working With The File System =
* Be very careful when working with files on the command line, as there is no undo command or a
Trash/Recycling Bin

* A single command can wipe out your entire account

* Changes are instant and permanent

* Make backups of important files, preferably outside of your account - USB storage is a good option

* You will learn later additional ways to control file access through file permissions which will help you prevent accidental file damage or deletion

= Basic Commands =
* Displays the user's present working directory to show where the user is located on the
computer system in order to build directories, copy files, etc

* Changes pwd to <tt>/path/to/dir</tt>. Entering the cd command without the directory path
will change to the userfs home directory.

* Displays the contents of a directory (eg. regular files or sub-directories). By default, the
ls command displays non-hidden filenames only. The following are common
options associated with the ls command: <tt>-a</tt> short display of hidden & non-hidden
files <tt>-l</tt> detailed display of files excluding hidden files <tt>-F</tt> displays / after
directory, \* after executable file Options can be combined, for example: <tt>ls -la</tt>
(or <tt>ls -l -a</tt>)

* Creates a subdirectory with a directory. Multiple arguments can be used to
create many subdirectories. The option .p allows for parent directories to be created.

* Removes only empty directories (i.e. directories that contain no
subdirectories or regular files). A user cannot remove a directory from within the directory
location itself.

* Moves a file from one location to another and/or rename the file. The mv command
can be used to move directories as well as files. The <tt>-i</tt> option asks for confirmation if the
destination filename already exists.

* Copies a file from one location to another. The cp command can be used to backup
important files. The <tt>-i</tt> option asks for confirmation if the destination
filename already exists. The <tt>-r</tt> option allows copying of directories and their
contents

* Removes a regular file. The <tt>-r</tt> option is used to recursively remove a
directory and it's contents. Recursive means to descend to lower levels, which in this case,
indicates that subdirectories and it contents are also removed. Note: it is a good idea to include
the .i option to confirm deletion of subdirectories and its contents!

* To join files (i.e. to concatenate files). For example, <tt>cat file1 file2 file3</tt> will
display the contents of file1 and file2 and file3 on the screen at the same
time. To display the contents of small files (files longer than the screen
will scroll to the end). For example, issuing the command <tt>cat .bash_profile</tt> in
your home directory would display the contents of your setup file.

* Displays the contents of large regular files one screen at a time. The
user can navigate throughout the file by pressing keys such as:

{| class=wikitable



|-
|spacebar
|Move to next screen

|-
|b
|Move to previous screen

|-
|enter
|Move to next line

|-
|/car
|Search for pattern "car"

|-
|q
|Exit to shell

|}

* Works like more command.

* Updates the date and time of existing files. The touch command is also used
to create empty files. You will be using the touch command to create empty
files when you practice the file management on-line tutorial

* Determines a file type Useful when a particular file has no file extension or the
extension is unknown/incorrect

= The find Command =
The find command allows searching for files by file name, size as well as file attributes
recursively throughout the file system. An optional action can be performed on matches

#Search for a file named bob:
find / -name bob

# Delete empty files belonging to user alice:
find / -user alice -empty -delete

# Find all files modified less than 5 minutes ago:
find / -mmin -5

# Find large files
find . -size +100M

= File Naming =
* Unix/Linux is case sensitive!

* Adopt a consistent file naming scheme . this will help you find your files later

* Make your file and directory names meaningful

* Avoid non alphanumeric characters, as they have a special meaning to the system and will make your work more difficult

* Avoid using spaces in file names . consider periods, hyphens and underscores instead

* Feel free to use file name extensions to describe the file purpose

= Getting Help with Commands =
* A comprehensive online manual for common UNIX/Linux commands exists on your server

* The online manual is a command called <tt>man</tt>

# show man page of ls command
$ man ls

man [options] command

Options:

-k provides short (one-line) explanation relating to the commands matching the character string.
This can be used if user doesn't know name of command.


= Text Editing =
Editing text files is an everyday activity for both users as well as administrators on a Unix and
Linux system

* System configuration files

* Scripts and programs

* Documentation

* Web pages

As the GUI may not always be available, knowing command-line text editors is a very valuable
skill.

Although both Unix/Linux and Windows use ASCII to encode text files, there are small differences
that may cause problems (particularly with scripts) when copying files between different systems:

* If needed, use the unix2dos and dos2unix utilities to convert between the two systems

* A specific system may have many editors available and as you work with one for a while you will
probably pick a favourite one

* A traditional fall-back is the vi editor, as it is most likely to be present on all Unix-like
systems, especially when installed with a minimum software complement

====== Consider knowing vi as one of many badges of a competent Unix/Linux user ======
* Vi has a relatively steep learning curve and is not user friendly, but it offers nice advanced
features which will be introduced later in the course

(Visual) Editor

vi is a powerful, interactive, visually-oriented text editor with these features:

* Efficient editing by using keystrokes instead of mouse.

* Use of regular expressions

* Possibility to recover files after accidental loss of connection

* Features for programmers (eg. line numbering, auto-indent, etc)

* Although you may prefer to use other editors (such as nano or nled), knowing vi is very useful, as
this is one editor that is present on all Unix-like systems

=== Starting vi Session ===
There are two ways to start an editing session with vi:

* <tt>vi filename</tt> is recommended when <tt>filename</tt> has already been assigned and changes will be saved
to filename by entering ZZ while in vi.

* <tt>vi</tt> to start a new file editing session, when <tt>filename</tt> is not assigned to a file. therefore user
has to type <ESC> :w filename <ENTER> in order to save file.

=== Modes ===
There are three operational modes while using the vi editor:

* User presses letter for a command. for example, input text, delete text, append text, etc.

* Input Mode allows user to enter or edit text. Press ESC to return to command mode.

* Pressing colon g:h opens a prompt to enter letter or word commands. More complex operations such as search and replace can be performed.

=== Moving in Command Mode ===
You can move around to text in the screen by using the following keys:

{| class=wikitable



|-
|h
|left

|-
|j
|down

|-
|k
|up

|-
|l
|right

|-
|w
|right one word to special character

|-
|W
|right one word including special characters

|-
|b
|left one word to special character

|-
|B
|left one word including special characters

|-
|0 (zero)
|beginning of line

|-
|$
|end of line

|}

* You may be able to move around by using the arrow keys (depends on version of vi).

* For more advanced editing, you can return to Command Mode and use appropriate editing commands.

=== Getting into Input Mode ===
While in command mode, you can issue the following commands to input text:

{| class=wikitable



|-
|i
|insert to left of cursor

|-
|I
|insert at beginning of line

|-
|o
|insert line below current line

|-
|0
|insert line above current line

|-
|a
|append to right of cursor

|-
|A
|append at end of current line

|-
|r
|replace character

|-
|R
|overwrite text

|}

=== Common Editing Commands ===
{| class=wikitable



|-
|x
|Delete single character

|-
|d
|Delete

|-
|c
|Change

|-
|y
|Yank (copy)

|-
|p
|paste below cursor

|-
|P
|paste above cursor

|-
|u
|undo previous edit

|-
|.
|repeat previous edit

|}

<blockquote>Editing commands can be preceded with a number, for example:
3x = delete the next three characters
2u = undo the last two edits
12dd = delete 12 lines<blockquote>

=== Searching ===
Search for text (in command mode)

{| class=wikitable



|-
|/pattern
|Search forward for pattern

|-
|?pattern
|Search backwards for pattern

|-
|n
|Display next match

|-
|//
|Repeat previous search

|}

=== Saving Edited File ===
* Work performed during vi session is stored in a Work Buffer (temporary storage) until the user
saves their work.

* When saving, changes in the work buffer are placed in a new file if creating a new file, or
changes in work buffer modify existing (previously created) file.

* To save your vi session, you must make sure you are in command mode by pressing <ESC>

* To save your changes and exit, enter ZZ (i.e. two capital zfs). You could perform the same
operation in last line mode by :x

* You can also save without exiting by entering :w

* To save your vi session, you must make sure you are in command mode by pressing <ESC>

* To save your changes and exit, enter ZZ (i.e. two capital zfs). You could perform the same
operation in last line mode by :x

* You can also save without exiting by entering :w

=== Aborting Editing Session ===
* If you make a mistake in your editing session (that undo cannot solve), you can abort your session
without modifying the contents of your file (dump the work buffer)

* To abort the current editing session, press <ESC> :q! <ENTER>


= Buzzwords =
CLI: command line interface
GUI: graphical user interface

File utilities discussed in class

----

pwd : print working directory tells you where you are in the filesystem
ls : list (files in current directory)
ls -l: long list files (show all file properties)
cd : change directory
find: find files
clear: clear the screen
rm : remove files (delete them - NO UNDO)

Symbols discussed in class

----

~ : home directory
/ : root directory (also path separator when not the first char)

Shortcuts discussed

----

ARROW UP/DN for moving forward or back through command history
CTRL+L: clears the screen
TAB: completes command (or directory/file name)
TAB TAB: shows all completions when not unique



LINUX/UNIX maxims discussed

----


* UNIX is an oral tradition

* UNIX is a humble servant but brutal master

* You got to know what you are looking for before you find it

* No news is good news.

* Everything is a file, yes even directories, disks, cdrom, usb, etc.

List of commands used so far

----

Begin by creating a '<tt>rough_work</tt>' directory, as shown below ($ indicates shell
prompt)

$ cd
$ mkdir uli101
$ cd uli101
$ pwd
/home/mark.fernandes/uli101

$ mkdir rough<sub>work</sub>
$ cd rough<sub>work</sub>
$ pwd
/home/mark.fernandes/uli101/rough<sub>work</sub>

Now you can continue as shown in the examples below


File management commands

----

All of file management stuff falls under the four sub headings:

# C: create

* to create a file use: touch, or a text editor like vi

* for example ($ indicates shell prompt)

$ touch file

# R: read (view/look)

* to look through a file you can use: less, cat

* for example ($ indicates shell prompt):

$ less file
$ cat file

(since there is nothing in the file you will not see anything)


# U: update (copy/move/rename)

* to copy a file use: cp

* for example ($ indicates shell prompt):

$ cp file file1
$ cp file1 file11
$ ls
file file1 file11

* to move a file use: mv

<code>~~</code>
$ mv file\* a
mv: target \`a' is not a directory
$ mkdir a
$ mv file\* a
$ tree
.
a
file
file1
file11

1 directory, 3 files
<code>~~</code>

* to rename a file use: mv

<code>~~</code>
$ cp a/\* .
$ ls
a file file1 file11
$ mv file1 file4
$ mv file11 file44
$ ls
a file file4 file44
<code>~~</code>


# D: delete

* to delete a file use: rm

Directory management commands

----

# C: create

* to create a directory use: mkdir

# R: read (view/look)

* to look through a directory you can use: ls, echo

# U: update (copy/move/rename)

* to copy a directory use: cp

* to move a directory use: mv

* to rename a directory use: mv

# D: delete

* to delete an empty directory use: rmdir

* to delete a non-empty directory use: rm -r


List of symbols used thus far

----

* all files (in shell globbing)

? any character (in shell globbing)
. current directory
.. parent directory
~ home directory
/ root directory, and path separator

This file is used for the week 2 practice quiz. The PDF file with all the questions are in:

/home/mark.fernandes/uli101/weekly<sub>notes</sub>/wk2practice.pdf

Complete all ten questions of the practice quiz to help you understand the main concepts of
week 2. You can ask your friends or the teacher for any of the answers to the quiz.

Remember Linux/UNIX is all about knowing the commands and using them correctly, so understand the
questions asked and the answers. It is always possible that there might be one or more ways of
answering some of the questions asked. Knowing all the ways to do something is not as important as
knowing at least one way correctly.

= File utilities discussed in this section =
There are many utilities you will discover as you learn ULI101. Here are some of the most common
ones discussed here.


pwd # print working directory tells you where you are in the filesystem
ls # list (files in current directory)
ls -l # long list files (show all file properties)
cd # change directory
# find # find files
# clear # clear the screen
# rm # remove files (delete them - NO UNDO)
221
edits

Navigation menu