Difference between revisions of "ULI101 Week 2"

From CDOT Wiki
Jump to: navigation, search
(Basic Commands)
 
(5 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 
= Linux File System =
 
= 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 file system is hierarchical, similar to other operating systems today
: 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 <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.
+
** Files are organized in directories
; Common root directory
+
** Directories may contain sub-directories
: 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.
+
* What is different (from Windows) is that there are no drive letters (such as C:, or D:)? All files and directories appear under a single root, even if multiple storage devices are used.
; Everything is a file
+
* Learning command-line navigation of the file system is essential for efficient system usage
: 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.
+
== Hierarchical File System? ==
  
=== What is a Hierarchical File System? ===
+
* In the Linux (Unix) OS, the ''root directory'' <code>/</code> is the starting directory, and other ''child directories'', ''grandchild directories'', etc. are created.
 
+
* The hierarchical structure resembles an ''upside-down tree''. There is actually a command called <code>tree</code> that can display a ''tree diagram''!
In Linux (or any Unix for that matter) the root directory (denoted by <code>/</code>) 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 <code>tree</code> that can display an upside-down 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
 
<source lang="bash"># display files and directories below root (/) with the tree command
 
$ tree /
 
$ tree /
 
/
 
/
|-home
+
|- home
| |-les
+
| |- user1
| |-mark
+
| |- user2
| |-uli101
+
| |- user3
| ...
+
| ...
 
|
 
|
|-bin
+
|- public
| |-nled
+
| |- ipc144
 +
|- nled
 
|
 
|
 
....
 
....
Line 36: Line 34:
 
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.
 
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 =
+
== Typical Unix/Linux 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.
 
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.
Line 42: Line 40:
 
{|
 
{|
 
| <code>/</code>
 
| <code>/</code>
| Root directory (ancestor to all directories)
+
| Root directory (ancestor to all directories).
 
|-
 
|-
 
| <code>/home</code>
 
| <code>/home</code>
| Used to store usersf home directories
+
| Used to store usersf home directories.
 
|-
 
|-
 
| <code>/bin</code>
 
| <code>/bin</code>
| Common system binaries (commands)
+
| Common system binaries (commands).
 +
|-
 +
| <code>/usr/bin</code>
 +
| Common utilities (commands) for users.
 
|-
 
|-
| <code>/dev</code>
+
| <code>/usr/sbin</code>
| Device files (terminals, printers, etc.)
+
| Common utilities for user administration.
 
|-
 
|-
 
| <code>/etc</code>
 
| <code>/etc</code>
| General System Admin. Files (eg passwd)
+
| General System Admin. Files (eg passwd).
 
|-
 
|-
| <code>/usr/bin</code>
+
| <code>/var</code>
| Common utilities (commands) for users
+
| Dynamic files (log files).
|-
 
| <code>/usr/sbin</code>
 
| Common utilities for user administration
 
 
|-
 
|-
 
| <code>/tmp</code>, <code>/var/tmp</code>
 
| <code>/tmp</code>, <code>/var/tmp</code>
| Temporary files for programs
+
| Temporary files for programs.
 
|-
 
|-
| <code>/var</code>
+
| <code>/dev</code>
| Dynamic files (log files)
+
| Device files (terminals, printers, etc.).
 
|}
 
|}
  
= Home directory =
+
== Home directory ==
  
* Almost every user in a Linux file system gets a 'home' directory when their account was created.
+
* Every user when receiving an account has a ''home'' directory created.
* This is where the users keep their personal files
+
* This is where you keep your personal files
* The <code>~</code> symbol represents the home directory of the logged in user.
+
* <code>~</code> represents your home
* The <code>~</code> symbol may be used in file pathnames. More about file pathnames later.
+
** You can use the <code>~</code> symbol in pathnames
* The <code>cd</code> command when used without any other parameters gets you into your home directory no matter where you previously were in the Linux file system.
+
* A <code>cd</code> command without any argument will get you directly to your home directory
* As Linux is a multi-user filesystem, remember to keep your files private. You shall learn how to do this in a later class.
+
* Remember to keep your files private
  
= Types of Files =
+
== 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.
+
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 etc.
  
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 <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.
+
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. 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:
 
For Example:
Line 99: Line 97:
  
 
* Use the <code>-l</code> option of the <code>ls</code> command to get detailed information about a file.
 
* 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.
+
* 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:
 
* The first character in detailed listing determines the file type, so:
 
** <code>-</code> indicates a regular file
 
** <code>-</code> indicates a regular file
 
** b or c indicates a device file
 
** b or c indicates a device file
** d indicates a directory file
+
** d indicates a directory
 
</blockquote>
 
</blockquote>
 
+
== Hidden Files ==
= 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:
 
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:
Line 128: Line 123:
 
* <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>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</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. 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>') 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 all files including hidden.
* <code>ls -A</code> displays 'almost' all files, that is all hidden files like <code>.profile</code> except the first two items <code>.</code> and <code>..</code>. This means <code>.</code> and <code>..</code> are never shown within the <code>ls -A</code> output listing.
+
* '<code>.</code>' and '<code>..</code>' directories are hidden.
 +
* <code>ls -A</code> displays 'Almost' all files not including <code>.</code> and <code>..</code>
 
</blockquote>
 
</blockquote>
=== Why hide files? ===
+
* Why make files hidden?
 +
** To clean up directories.
 +
** To hide backups.
 +
** To protect important files from accidental deletion.
 +
* Remember: directories are really files, you can hide them as well.
  
* To clean up directories.
+
== Working With The File System ==
* 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
 
* Be very careful when working with files on the command line, as there is no undo command or a Trash/Recycling Bin
Line 146: Line 141:
 
* You will learn later additional ways to control file access through file permissions which will help you prevent accidental file damage or deletion
 
* 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 =
+
== Basic Commands ==
  
 
; <code>pwd</code>
 
; <code>pwd</code>
: 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
+
: Used to display the user’s present working directory. A user may need to know where they are located on the computer system in order to build directories, copy files, etc.
 
+
; <code>cd</code> ''directorypath''
; <code>cd path/to/dir</code>
+
: Used to change to a directory. Entering the cd command without a directory name will change to the user’s home directory.
: Changes pwd to <code>/path/to/dir</code>. Entering the cd command without the directory path will change to the userfs home directory.
 
 
 
 
; <code>ls</code>
 
; <code>ls</code>
: 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: <code>-a</code> short display of hidden &amp; non-hidden files <code>-l</code> detailed display of files excluding hidden files <code>-F</code> displays / after directory, * after executable file Options can be combined, for example: <code>ls -la</code> (or <code>ls -l -a</code>)
+
: 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 available with the <code>ls</code> command:
; <code>mkdir path/to/dir</code>
+
;* <code>-a</code> short display of hidden &amp; non-hidden files
: 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>-l</code> detailed display of files (excl. hidden files)
 +
;* <code>-d</code> combined with <code>-l</code> option, displays info about the directory itself instead of the files within it
 +
;* Options can be combined, for example: <code>ls -la</code> (or <code>ls -l -a</code>)
  
; <code>rmdir path/to/dir</code>
+
; <code>mkdir</code> ''directorypath''
: 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.
+
: 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''
; <code>mv path/to/source path/to/destination</code>
+
: Used to remove only empty directories (i.e. directories that contain no subdirectories or regular files). A user cannot remove a directory from within the directory 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 <code>-i</code> option asks for confirmation if the destination filename already exists.
+
; <code>mv</code> ''sourcepath'' ''directorypath''
 
+
: Used to move 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 <code>-i</code> option asks for confirmation if the destination filename already exists.
; <code>cp path/to/source path/to/destination</code>
+
; <code>cp</code> ''sourcepath'' ''directorypath''
: Copies 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> option allows copying of directories and their contents
+
: 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''
; <code>rm path/to/file</code>
+
: Used to remove a regular file.
: 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>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 path/to/file</code>
+
; <code>cat</code> ''filepath''
: To join files (i.e. to concatenate files). For example, <code>cat file1 file2 file3</code> 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 <code>cat .bash_profile</code> in your home directory would display the contents of your setup file.
+
: To display contents of one or more files (i.e. to 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 one after the other. 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''
; <code>more path/to/file</code>
+
;: 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:
: 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 next screen
 
|-
 
|-
Line 185: Line 180:
 
| Move to previous screen
 
| Move to previous screen
 
|-
 
|-
| enter
+
| &lt;ENTER&gt;
 
| Move to next line
 
| Move to next line
 
|-
 
|-
| /car
+
| /car&lt;ENTER&gt;
 
| Search for pattern &quot;car&quot;
 
| Search for pattern &quot;car&quot;
 
|-
 
|-
Line 194: Line 189:
 
| Exit to shell
 
| 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>
+
== The find Command ==
: Works like more 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/file</code>
 
: Determines a file type Useful when a particular file has no file extension or the extension is unknown/incorrect
 
  
= The find Command =
+
The <code>find</code> command allows searching for files by file name, size, and file attributes recursively throughout the file system. An optional action can be performed on matches
 
 
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
 
  
 
<source lang="bash">#Search for a file named bob:
 
<source lang="bash">#Search for a file named bob:
Line 220: Line 213:
 
find . -size +100M
 
find . -size +100M
 
</source>
 
</source>
= File Naming =
+
== File Naming ==
  
 
* Unix/Linux is case sensitive!
 
* Unix/Linux is case sensitive!
* Adopt a consistent file naming scheme . this will help you find your files later
+
* Adopt a consistent file naming scheme. this will help you find your files later
 
* Make your file and directory names meaningful
 
* 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 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
+
* Avoid using spaces in file names - consider periods, hyphens and underscores instead
 
* Feel free to use file name extensions to describe the file purpose
 
* Feel free to use file name extensions to describe the file purpose
  
= Getting Help with Commands =
+
== Getting Help with Commands ==
  
 
* A comprehensive online manual for common UNIX/Linux commands exists on your server
 
* A comprehensive online manual for common UNIX/Linux commands exists on your server
Line 242: Line 235:
  
 
-k provides short (one-line) explanation relating to the commands matching the character string.
 
-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.
+
This can be used if user doesn't know name of command, eg. man -k calendar
 
</pre>
 
</pre>
= Text Editing =
+
== Text Editing ==
  
 
Editing text files is an everyday activity for both users as well as administrators on a Unix and Linux system
 
Editing text files is an everyday activity for both users as well as administrators on a Unix and Linux system
Line 255: Line 248:
 
As the GUI may not always be available, knowing command-line text editors is a very valuable skill.
 
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:
+
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 unix2dos and dos2unix utilities to convert between the two 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 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
+
* 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 vi as one of many badges of a competent Unix/Linux user
+
Consider knowing <code>vi</code> as one of many badges of a competent Unix/Linux user
  
<ul>
+
* <code>vi</code> 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
<li><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>
 
  
vi is a powerful, interactive, visually-oriented text editor with these features:
+
<code>vi</code> is a powerful, interactive, visually-oriented text editor with these features:
  
 
* Efficient editing by using keystrokes instead of mouse.
 
* Efficient editing by using keystrokes instead of mouse.
Line 273: Line 264:
 
* Possibility to recover files after accidental loss of connection
 
* Possibility to recover files after accidental loss of connection
 
* Features for programmers (eg. line numbering, auto-indent, etc)
 
* 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
+
* 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 ===
+
== Starting vi Session ==
  
There are two ways to start an editing session with vi:
+
There are two ways to start an editing session with <code>vi</code>:
  
* <code>vi filename</code> is recommended when <code>filename</code> has already been assigned and changes will be saved to filename by entering ZZ while in vi.
+
– Enter <code>vi</code> ''filename'' -recommended since ''filename'' has already been assigned and changes will be saved to that ''filename'' when saving within <code>vi</code>, 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, therefore user has to type <code>:w filename&lt;ENTER&gt;</code> in order to save the file.
* <code>vi</code> to start a new file editing session, when <code>filename</code> is not assigned to a file. therefore user has to type &lt;ESC&gt; :w filename &lt;ENTER&gt; in order to save file.
 
  
=== Modes ===
+
== Modes ==
  
 
There are three operational modes while using the vi editor:
 
There are three operational modes while using the vi editor:
  
; Command Mode
+
Command Mode (default mode when starting) :: User presses letter(s) for a command
: User presses letter 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.
 
  
=== Moving in Command Mode ===
+
– 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:
 
You can move around to text in the screen by using the following keys:
Line 327: Line 318:
 
| $
 
| $
 
| end of line
 
| 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).
 
* 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 ===
+
== Getting into Input Mode ==
  
 
While in command mode, you can issue the following commands to input text:
 
While in command mode, you can issue the following commands to input text:
Line 346: Line 342:
 
| insert line below current line
 
| insert line below current line
 
|-
 
|-
| 0
+
| O
 
| insert line above current line
 
| insert line above current line
 
|-
 
|-
Line 356: Line 352:
 
|-
 
|-
 
| r
 
| r
| replace character
+
| replace character under cursor
 
|-
 
|-
 
| R
 
| R
| overwrite text
+
| overwrite text character-by-character
 
|}
 
|}
  
=== Common Editing Commands ===
+
Don't forget to hit &lt;ESC&gt; to return to command mode.
 +
 
 +
== Common Editing Commands ==
  
 
{|
 
{|
 
| x
 
| x
| Delete single character
+
| Delete single character under the cursor
 
|-
 
|-
 
| d
 
| d
 
| Delete
 
| 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
 
| c
 
| Change
 
| 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
 
| y
 
| Yank (copy)
 
| 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
 
| p
| paste below cursor
+
| paste deleted or copied text after or below cursor
 
|-
 
|-
 
| P
 
| P
| paste above cursor
+
| paste deleted or copied text before or above cursor
 
|-
 
|-
 
| u
 
| u
Line 392: Line 417:
 
<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>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>
 
</blockquote>
=== Searching ===
+
== Searching ==
  
 
Search for text (in command mode)
 
Search for text (in command mode)
Line 405: Line 430:
 
| n
 
| n
 
| Display next match
 
| Display next match
|-
 
| //
 
| Repeat previous search
 
 
|}
 
|}
  
=== Saving Edited File ===
+
== Saving Edited File ==
  
 
* Work performed during vi session is stored in a Work Buffer (temporary storage) until the user saves their work.
 
* 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, make sure you are in command mode by pressing <code>&lt;ESC&gt;</code>
* To save your vi session, you must make sure you are in command mode by pressing &lt;ESC&gt;
+
* To save your changes and exit, type <code>ZZ</code> (two capital z’s). You can also use either <code>:x&lt;ENTER&gt;</code> or <code>:wq&lt;ENTER&gt;</code>
* 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 save without exiting by typing <code>:w&lt;ENTER&gt;</code>
* 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 &lt;ESC&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 :x
 
* You can also save without exiting by entering :w
 
  
=== Aborting Editing Session ===
+
== 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)
+
If you make a mistake in 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>
* To abort the current editing session, press &lt;ESC&gt; :q! &lt;ENTER&gt;
 
  
= 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.
 
  
<source lang="bash">
+
[[Category:ULI101]]
pwd    #  print working directory tells you where you are in the filesystem
+
[[Category:ULI101-2018]]
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>
 

Latest revision as of 14:21, 6 September 2019

Linux File System

Unix File System.

  • The Unix/Linux file system is hierarchical, similar to other operating systems today
    • Files are organized in directories
    • Directories may contain sub-directories
  • What is different (from Windows) is that there are no drive letters (such as C:, or D:)? All files and directories appear under a single root, even if multiple storage devices are used.
  • Learning command-line navigation of the file system is essential for efficient system usage

Hierarchical File System?

  • In the Linux (Unix) OS, the root directory / is the starting directory, and other child directories, grandchild directories, etc. are created.
  • The hierarchical structure resembles an upside-down tree. There is actually a command called tree that can display a tree diagram!
# display files and directories below root (/) with the tree command
$ tree /
/
|- home
|  |- user1
|  |- user2
|  |- user3
|  ...
|
|- public
|  |- ipc144
|  |- nled
|
....

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

Typical Unix/Linux Directories

A sample of some of the commonly found subdirectories that lie below the root directory (/) is shown in the table below. The table includes a brief description of the purpose of that subdirectory.

/ Root directory (ancestor to all directories).
/home Used to store usersf home directories.
/bin Common system binaries (commands).
/usr/bin Common utilities (commands) for users.
/usr/sbin Common utilities for user administration.
/etc General System Admin. Files (eg passwd).
/var Dynamic files (log files).
/tmp, /var/tmp Temporary files for programs.
/dev Device files (terminals, printers, etc.).

Home directory

  • Every user when receiving an account has a home directory created.
  • This is where you keep your personal files
  • ~ represents your home
    • You can use the ~ symbol in pathnames
  • A cd command without any argument will get you directly to your home directory
  • Remember to keep your files private

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 etc.

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. 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 . or ..) is considered a hidden file (or directory). You can use the ls -l 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
Notes for listing above:
  • Use the -l option of the ls command to get detailed information about a file.
  • Use the -ld option of the ls 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:
    • - indicates a regular file
    • b or c indicates a device file
    • d indicates a directory

Hidden Files

A filename that begins with a '.' is a hidden file. So, if a filename name starts with a '.' as its first character, like .profile 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
In the file listing shown above
  • pwd 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 /home/murray
  • ls 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).
  • ls -a displays all files including hidden.
  • '.' and '..' directories are hidden.
  • ls -A displays 'Almost' all files not including . and ..
  • Why make files hidden?
    • To clean up directories.
    • To hide backups.
    • To protect important files from accidental deletion.
  • 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
    • 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

pwd
Used to display the user’s present working directory. A user may need to know where they are located on the computer system in order to build directories, copy files, etc.
cd directorypath
Used to change to a directory. Entering the cd command without a directory name will change to the user’s home directory.
ls
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 available with the ls command
  • -a short display of hidden & non-hidden files
  • -l detailed display of files (excl. hidden files)
  • -d combined with -l option, displays info about the directory itself instead of the files within it
  • Options can be combined, for example: ls -la (or ls -l -a)
mkdir directorypath
Used to create a directory. Multiple arguments can be used to create multiple directories. The option –p (parent) allows multiple directory levels to be created.
rmdir directorypath
Used to remove only empty directories (i.e. directories that contain no subdirectories or regular files). A user cannot remove a directory from within the directory itself.
mv sourcepath directorypath
Used to move 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 -i option asks for confirmation if the destination filename already exists.
cp sourcepath directorypath
Used to copy a file from one location to another. The cp command can be used to backup important files. – The -i option asks for confirmation if the destination filename already exists. – The -r (recursive) option allows copying of directories and their contents.
rm filepath
Used to remove a regular file.
rm -r 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 -i option to confirm deletion of subdirectories and their contents!
cat filepath
To display contents of one or more files (i.e. to catenate files). For example, cat file1 file2 file3 will display the contents of file1 and file2 and file3 on the screen one after the other. To display the contents of small files (files longer than the screen will scroll to the end). For example, issuing the command cat .bash_profile in your home directory would display the contents of your setup file.
more 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:
<SPACEBAR> Move to next screen
b Move to previous screen
<ENTER> Move to next line
/car<ENTER> Search for pattern "car"
q Exit to shell
less filepath
Works like more command, but contains more navigation features.
touch path
Used to update 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
file path
Determines a file type. Useful when a particular file has no file extension or the extension is unknown or incorrect.

The find Command

The find command allows searching for files by file name, size, and 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 man
# 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, eg. man -k calendar

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.

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 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:

– Enter vi filename -recommended since filename has already been assigned and changes will be saved to that filename when saving within vi, for example :w<ENTER> – If the filename exists, it will be edited. If the filename doesn't exist, it will be created. – Enter vi - filename is not assigned, therefore user has to type :w filename<ENTER> 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 to input text, delete text, append text, etc. Does NOT require <ENTER> key, the keystrokes are used individually.

– Input Mode :: Input Mode allows user to enter or edit text. Press <ESC> to return to command mode.

– Last-line Mode :: Pressing colon ":" opens a prompt at the bottom of the screen to enter more complex commands, such as search and replace. Requires <ENTER> key to execute command.

Moving in Command Mode

You can move around to text in the screen by using the following keys:

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
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).

Getting into Input Mode

While in command mode, you can issue the following commands to input text:

i insert to left of cursor
I insert at beginning of line
o insert line below current line
O insert line above current line
a append to right of cursor
A append at end of current line
r replace character under cursor
R overwrite text character-by-character

Don't forget to hit <ESC> to return to command mode.

Common Editing Commands

x Delete single character under 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 undo previous edit
. repeat previous edit
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

Searching

Search for text (in command mode)

/pattern Search forward for pattern
 ?pattern Search backwards for pattern
n Display next match

Saving Edited File

  • Work performed during vi session is stored in a Work Buffer (temporary storage) until the user saves their work.
  • To save your vi session, make sure you are in command mode by pressing <ESC>
  • To save your changes and exit, type ZZ (two capital z’s). You can also use either :x<ENTER> or :wq<ENTER>
  • You can save without exiting by typing :w<ENTER>

Aborting Editing Session

If you make a mistake in 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: :q!<ENTER>