Changes

Jump to: navigation, search

ULI101 Week 2

1,462 bytes removed, 19:01, 5 September 2019
no edit summary
= Linux File System =
== Characteristics of a Linux File System. ==
The Linux file system has the following characteristics:== Unix File System. ==
; Tree-like structure: * The Unix/Linux shares file system characteristics with is hierarchical, similar to other operating systems like Windows today** Files are organized 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** Directories may contain sub-like structure but unlike directories* What is different (from Windows, ) is that there are no drive letters (such as C: , or D:) on a Linux file system (instead all removable and non-removable devices are available and accessible from the <code>/dev</code> 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.; Common root directory: In a Linux file sytem all ? All files and directories appear under a single parent file (called root),however, unlike Windows, even if multiple storage devices appear under the same root file in a Linux file systemare used.; Everything is a file: Everything within * Learning command-line navigation of 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.essential for efficient system usage
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.== Hierarchical File System? ==
=== What is a Hierarchical File System? === * In the Linux (or any Unix for that matter) OS, the ''root directory (denoted by '' <code>/</code>) is source (or uppermost) the starting directory at the top of all , and other ''child directories and files. This means '', 'all'grandchild directories'' other files, child, and grandchild directories etc. are shown as descendants of that single root directorycreated. This * The hierarchical structure resembles an ''upside-down tree''. There is actually a command called <code>tree</code> that can display an upside-down a ''tree-like diagram of the Linux (Unix) file system as shown below:''!
<source lang="bash"># display files and directories below root (/) with the tree command
$ tree /
/
|-home| |-lesuser1| |-markuser2| |-uli101user3| ...
|
|-binpublic| |- ipc144| |-nled
|
....
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 = Typical Unix/Linux directories directly below root Directories ==
A sample of some of the commonly found subdirectories that lie below the root directory (<code>/</code>) is shown in the table below. The table includes a brief description of the purpose of that subdirectory.
{|
| <code>/</code>
| Root directory (ancestor to all directories).
|-
| <code>/home</code>
| Used to store usersf home directories.
|-
| <code>/bin</code>
| Common system binaries (commands).|-| <code>/usr/bin</code>| Common utilities (commands) for users.
|-
| <code>/devusr/sbin</code>| Device files (terminals, printers, etcCommon utilities for user administration.)
|-
| <code>/etc</code>
| General System Admin. Files (eg passwd).
|-
| <code>/usr/binvar</code>| Common utilities Dynamic files (commandslog files) for users|-| <code>/usr/sbin</code>| Common utilities for user administration.
|-
| <code>/tmp</code>, <code>/var/tmp</code>
| Temporary files for programs.
|-
| <code>/vardev</code>| Dynamic Device files (log filesterminals, printers, etc.).
|}
== Home directory ==
* Almost every Every user in a Linux file system gets when receiving an account has a ''home'' directory when their account was created.* This is where the users you keep their your personal files* The <code>~</code> symbol represents the your home directory of the logged in user.* The * You can use the <code>~</code> symbol may be used in file pathnames. More about file pathnames later.* The A <code>cd</code> command when used without any other parameters gets argument will get you into directly to your home directory no matter where you previously were in the Linux file system.* As Linux is a multi-user filesystem, remember 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 onetc.
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 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 <code>.</code> or <code>..</code>) is considered a hidden file (or directory). You can use the <code>ls -l</code> command to determine the type of file.
For Example:
* Use the <code>-l</code> option of the <code>ls</code> command to get detailed information about a file.
* Use the <code>-ld</code> option of the <code>ls</code> command to get detailed information for just the directory.itself, not the filenames within it
* The first character in detailed listing determines the file type, so:
** <code>-</code> 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 '<code>.</code>' is a hidden file. So, if a filename name starts with a '<code>.</code>' as its first character, like <code>.profile</code> 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:
* <code>pwd</code> 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 <code>/home/murray</code>
* <code>ls</code> 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).
* <code>ls -a</code> displays all files, i.e. including hidden and non-hidden files, in the present working directory. In the listing of <code>ls -a</code> the first two items (i.e. * '<code>.</code>' and '<code>..</code>') directories are hidden directories to indicate the present working directory (the item: '<code>.</code>') and the item '<code>..</code>' indicates the parent directory of the present working directory. Both (<code>.</code> and <code>..</code>) 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.* <code>ls -A</code> displays 'almostAlmost' all files, that is all hidden files like <code>.profile</code> except the first two items <code>.</code> and <code>..</code>. This means not including <code>.</code> and <code>..</code> are never shown within the <code>ls -A</code> output listing.
</blockquote>
=== * Why hide make fileshidden? === ** To clean up directories.** To hide backups.** To protect important files from accidental deletion.* Since Remember: 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
* 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 ==
; <code>pwd</code>
: Displays Used to display the user's user’s present working directory . A user may need to show know where the user is they are located on the computer system in order to build directories, copy files, etc.; <code>cd path/to/dir</code>''directorypath'': Changes pwd Used to <code>/path/change to/dir</code>a directory. Entering the cd command without the a directory path name will change to the userfs user’s home directory. 
; <code>ls</code>
: Displays Used to display 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 available with the <code>ls </code> command: ;* <code>-a</code> short display of hidden &amp; non-hidden files ;* <code>-l</code> detailed display of files excluding (excl. hidden files );* <code>-Fd</code> displays / after directory, * after executable file Options can be combined, for example: with <code>ls -la</code> (or <code>ls -l -a</code>) ; <code>mkdir path/to/dir</code>: 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. ; <code>rmdir path/to/dir</code>: Removes only empty directories (i.e. directories that contain no subdirectories or regular files). A user cannot remove a directory from within , displays info about the directory location itself.instead of the files within it; <code>mv path/to/source path/to/destination</code>: Moves a file from one location to another and/or rename the file. The mv command * Options can be used to move directories as well as files. The <code>-i</code> option asks combined, for confirmation if the destination filename already exists. ; <code>cp path/to/source path/to/destination</code>example: Copies a file from one location to another. The cp command can be used to backup important files. The <code>ls -ila</code> option asks for confirmation if the destination filename already exists. The (or <code>ls -l -r</code> option allows copying of directories and their contents ; <code>rm path/to/file</code>: Removes a regular file. The <code>-r</code> 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!)
; <code>cat pathmkdir</code> ''directorypath'': Used to create a directory. Multiple arguments can be used to create multiple directories. The option <code>–p</code> (parent) allows multiple directory levels to be created.; <code>rmdir</code> ''directorypath'': Used toremove only empty directories (i.e. directories that contain no subdirectories or regular files). A user cannot remove a directory from within the directory itself.; <code>mv</code> ''sourcepath'' ''directorypath'': Used to move a filefrom one location to another and/or rename the file. The mv command can be used to move directories as well as files. The <code>-i</code> option asks for confirmation if the destination filename already exists.; <code>cp</code>''sourcepath'' ''directorypath'': Used to copy a file from one location to another. The cp command can be used to backup important files. – The <code>-i</code> option asks for confirmation if the destination filename already exists. – The <code>-r</code> (recursive) option allows copying of directories and their contents.; <code>rm</code> ''filepath'': Used to remove a regular file.; <code>rm -r</code> ''filepath'': 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 their contents are also removed. Note: it is a good idea to include the <code>-i</code> option to confirm deletion of subdirectories and their contents!; <code>cat</code> ''filepath'': To join display contents of one or more files (i.e. to concatenate catenate files). For example, <code>cat file1 file2 file3</code> will display the contents of <code>file1 </code> and <code>file2 </code> and <code>file3 </code> on the screen at one after the same timeother. To display the contents of small files (files longer than the screen will scroll to the end). For example, issuing the command <code>cat .bash_profile</code> in your home directory would display the contents of your setup file.;; <code>more</code> ''filepath'';: Used to display the contents of large regular files one screen at a time. The user can navigate throughout the file by pressing keys such as:
; <code>more path/to/file</code>: Displays the contents of large regular files one screen at a time. The user can navigate throughout the file by pressing keys such as:<blockquote>{|| spacebar&lt;SPACEBAR&gt;
| Move to next screen
|-
| Move to previous screen
|-
| enter&lt;ENTER&gt;
| Move to next line
|-
| /car&lt;ENTER&gt;
| Search for pattern &quot;car&quot;
|-
| q
| Exit to shell
|}</blockquote>; <code>less</code> ''filepath'': Works like more command, but contains more navigation features.; <code>touch</code> ''path'': Used to update the date and time of existing files. The <code>touch</code> 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; <code>file</code> ''path'': Determines a file type. Useful when a particular file has no file extension or the extension is unknown or incorrect.
; <code>less path/to/file</code>: Works like more command.== The find Command ==
; <code>touch path/to/file</code>: 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. ; <code>file path/to/filefind</code>: 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 , and file attributes recursively throughout the file system. An optional action can be performed on matches
<source lang="bash">#Search for a file named bob:
find . -size +100M
</source>
== 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
-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, eg.man -k calendar
</pre>
== Text Editing ==
Editing text files is an everyday activity for both users as well as administrators on a Unix and Linux system
As the GUI may not always be available, knowing command-line text editors is a very valuable skill.
Although Please note that 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 <code>unix2dos </code> and <code>dos2unix </code> 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 <code>vi </code> editor, as it is most likely to be present on all Unix-like systems, especially when installed with a minimum software complement
Consider knowing <code>vi </code> as one of many badges of a competent Unix/Linux user
* <ulcode>vi<li/code><p>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</p><p>(Visual) Editor</p></li></ul>
<code>vi </code> is a powerful, interactive, visually-oriented text editor with these features:
* Efficient editing by using keystrokes instead of mouse.
* 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 <code>nano </code> or <code>nled</code>), knowing <code>vi </code> 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 <code>vi</code>:
* – Enter <code>vi filename</code> is ''filename'' -recommended when <code>since ''filename</code> '' has already been assigned and changes will be saved to that ''filename by entering ZZ while in vi.* '' when saving within <code>vi</code> to start a new file editing session, when for example <code>:w&lt;ENTER&gt;</code>– If the ''filename'' exists, it will be edited. If the ''filename'' doesn't exist, it will be created. – Enter <code>vi</code> - filename is not assigned to a file. , therefore user has to type &lt;ESC&gt; <code>:w filename &lt;ENTER&gt; </code> in order to save the file.
=== Modes ===
There are three operational modes while using the vi editor:
; Command Mode(default mode when starting) :: User presses letter (s) for a command. for example, input text, delete text, append text, etc.; Input Mode: Input Mode allows user to enter or edit text. Press ESC to return to command mode.; Last-line 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.
=– for example to input text, delete text, append text, etc. Does NOT require <code>&lt;ENTER&gt;</code> key, the keystrokes are used individually. – Input Mode :: Input Mode allows user to enter or edit text. Press <code>&lt;ESC&gt;</code> to return to command mode. – Last-line Mode :: Pressing colon &quot;:&quot; opens a prompt at the bottom of the screen to enter more complex commands, such as search and replace. Requires <code>&lt;ENTER&gt;</code> key to execute command. == Moving in Command Mode ===
You can move around to text in the screen by using the following keys:
| $
| end of line
|-
| G
| go to last line in file
|-
| 237G
| go to line 237 in file
|}
* 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:
| insert line below current line
|-
| 0O
| insert line above current line
|-
|-
| r
| replace characterunder cursor
|-
| R
| overwrite textcharacter-by-character
|}
=Don't forget to hit &lt;ESC&gt; to return to command mode. == Common Editing Commands ===
{|
| x
| Delete single characterunder the cursor
|-
| d
| Delete
|-
|
| eg. dw - delete from the current position to the next word or special character
|-
|
| eg. d$ - delete from the current position to the end of the line
|-
|
| eg. dd - delete the entire current line
|-
| c
| Change
|-
|
| eg. cw - change from the current position to the next word or special character
|-
|
| eg. c$ - change from the current position to the end of the line
|-
|
| eg. cc - change the entire current line
|-
| y
| Yank (copy)
|-
|
| eg. yw - copy from the current position to the next word or special character
|-
|
| eg. y$ - copy from the current position to the end of the line
|-
|
| eg. yy - copy the entire current line
|-
| p
| paste deleted or copied text after or below cursor
|-
| P
| paste deleted or copied text before or above cursor
|-
| u
<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)
| 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 <code>&lt;ESC&gt;</code>* To save your changes and exit, enter type <code>ZZ </code> (i.e. two capital zfsz’s). You could perform the same operation in last line mode by :x* You can also save without exiting by entering use either <code>:w* To save your vi session, you must make sure you are in command mode by pressing x&lt;ESCENTER&gt;* To save your changes and exit, enter ZZ (i.e. two capital zfs). You could perform the same operation in last line mode by </code> or <code>:xwq&lt;ENTER&gt;</code>* You can also save without exiting by entering typing <code>:w === Aborting Editing Session ===&lt;ENTER&gt;</code>
* 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 &lt;ESC&gt; :q! &lt;ENTER&gt;== Aborting Editing Session ==
= File utilities discussed If you make a mistake in this section =your editing session that undo cannot easily solve, you can abort your session without modifying the contents of your file by using the following last-line command: <code>:q!&lt;ENTER&gt;</code>
There are many utilities you will discover as you learn ULI101. Here are some of the most common ones discussed here.
<source lang="bash">
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)
 
</source>
[[Category:ULI101]]
[[Category:ULI101-2018]]
221
edits

Navigation menu