Week 2
The Filesystem
vi Editor Quick Reference
Type vi at the shell prompt to launch the vi editor.
You can also specify a file to load on the command line.
In vi parlance, the onscreen text is called the "buffer", just as "document" is used in most GUI word processors.
Unlike most any editor you're likely used to, vi has "modes" as described below.
You'll find that you switch back and forth between modes frequently.
Consult
Learning the vi Editor to more fully embrace the vi experience.
Commands
Commands are keystrokes entered while in Command Mode.
The case of the command is significant.
Some commands can be executed more than once by first typing the number of repetitions.
There are many, many more commands in vi than listed here.
The various distributions of Linux or UNIX tend to use slightly
different directory structures, or organize files into different
locations.
The following structure is typical of most Linux distributions, and Red
Hat in particular.
Linux uses the term "directory" instead of "folder" as is preferred by the Macintosh and recent versions of Windows.
While technically any character other than slash (/) or the ASCII NUL can be used in a filename (depending on the filesystem),
it's best to restrict yourself to alphanumerical characters (remember that Linux is case-sensitive),
underscore (_), hyphen (-), and period (.).
While
space characters are allowed, they're more trouble than they're worth
because you'll have to enclose the filename in quotes every time you
reference it.
There is no concept of a "file extension" in Linux as there is
in Windows.
It's common to append ".txt" to text files, or ".c" to C source files,
but doing so has no intrinsic meaning to the operating system.
Executable files are identified by their access permissions rather than
an ".exe" extension (see the File Listings section below).
File Types
Almost every "thing" in Linux can be considered some type of file.
The types of files are listed below (note that in this case "file type" does not distinguish a text file from a GIF image file,
but indicates the various "things" that Linux treats like files).
The Listing Character is the leftmost character in the detailed file listing that is displayed using the ls -l command.
The Listing Colour is the colour of the filename displayed by the ls command (in the default Red Hat installation at any rate).
It is possible to create a link to an existing file that, for all intents and purposes, appears to be the original file,
although it may have a different name or be in a different location.
Links are created using the ln command.
There are two types of link: hard and soft (symbolic).
Hard links cannot easily be identified as such, although the
file listing will reflect the number of times a file has been
hard-linked
(see the File Listings section below).
Hard links also have other limitations: directories cannot be
hard-linked (except by the root user), and links cannot cross
filesystem boundaries.
Soft, or symbolic, links are generally preferred because, even
though they act just like hard links, they can easily be identified for
what they are,
and they don't suffer from the same limitations as hard links.
Deleting a link (hard or soft) does not delete the file that is linked to.
When displaying files using the long output format (ls -l), a typical file might look as follows:
The first character is the file type, in this case "-" means a regular file.
The next 9 characters indicate the file permissions.
The permissions are split into three groups of three characters each: owner, group, and others.
An r means the file can be read (or a directory can be listed).
A w means the file can be written to, or modified.
An x means the file can be executed (or a directory can be navigated to); in other words it is a program of some sort.
In this case, the first three characters, rwx means that the owner of the file can read, write, and execute this file.
The second set of three characters, r-x means that members of the group can read and execute the file, but not write to it.
The final set of three characters, r-- means that any other user can read the file, but cannot write to it nor execute it.
The number 2 indicates the number of hard links to this file. If there are no hard links, a 1 will be displayed.
sally is the owner of the file.
sally belongs to the managers group,
and members of that group have the access permissions described above (the second set of three permission characters).
The file is 48712 bytes in size.
The file was last modified on January 4th (of this year) at 9:11AM. Older dates will display a year instead of the time.
The file is named calcpayroll.
Just as in MS-DOS, the Linux shell can use wildcards to specify filename patterns. This is also sometimes called "globbing".
Don't confuse Filename Expansion with Regular Expressions, which are far more powerful and use a more complex syntax.
Another important but subtle difference is that MS-DOS passes
the wildcard string to the command program for it to handle.
The Linux shell first evaluates the wildcard, expands it into a list of
all of the matching files, and passes this full list to the command
program.
This distinction becomes apparent when you write shell scripts that
accept filename parameters.
or virtual) that can be formatted to store information organized as
files.
Many different filesystems can, and are, mounted (loaded) into the
Linux directory hierarchy.
Some filesystems have limitations (e.g. MS-DOS's 8.3 filenames, and no
concept of access permissions) but the filesystem driver will attempt
to present
each filesystem in such a way that the user usually doesn't know or
care which filesystem he or she is using.
The following table lists the most common filesystems.
The Type column contains the name used when mounting a filesystem using the mount command.
The following table demonstrates only some of the options possible for each command.
Consult the man pages for the complete list, or a reference such as
Linux in a Nutshell.
The Filesystem
- The root hierarchy
- Navigation
- Permissions
- More shell commands
- Text editors
Miscellaneous Info
Handouts
- Most of this week's notes are included in the handouts. Make sure to read them closely, especially the examples.
Pronunciation
- Pronunciation of punctuation can be troublesome when
describing shell command syntax, so the following is a list of the
common names (most common first): - ! = bang
- * = splat, star
- @ = at, strudel
- . = dot, point, period
- # = hash, crunch, pound, pound sign
- ~ = twiddle, tilde
- | = pipe, bar
- - = dash, minus, hyphen
- _ = underscore
- / = slash
- \ = backslash, escape
- & = amp, ampersand
- $ = dollar
- % = percent
- ^ = caret, hat, control
- ? = question
- ( = prance, parens, parenthesis
- [ = bracket, square bracket
- { = brace, curly
- ' = quote
- ` = back quote
- " = double quote
Shell Tips
- Ctrl+C breaks (stops) a running program (eventually/usually)
- Ctrl+\ stops a running program (use if Ctrl+C didn't work)
- Ctrl+D
signals End of File (EOF). If you run the cat program without any
parameters, it will expect input from the keyboard instead. Use Ctrl+D
to exit. - Ctrl+S suspends output to screen. Use Ctrl+Q to restart.
- Ctrl+U erase command line
- Ctrl+Z suspends the current job. Use fg to restart.
- Use
the TAB key to autocomplete file names when typing commands. Hit TAB
again to show list of possible matches if it beeps the first time. - Use the up and down arrows to go back and forward in the command line history. Return will execute the command again.
vi Editor
- Seems like stone age tool, but useful for 3 reasons: fits on
a floppy, comes with every UNIX/Linux distro you can think of, runs
without GUI so it can be used remotely through telnet or ssh (with less
keyboard map fiddling as usually in the case of Emacs) - Actually quite powerful once you're used to it: Running Linux book was written using vi (and Emacs).
- At the very least, you should be able to open a file, make simple additions or changes, and save the file again using vi.
vi Editor Quick Reference
vi Basics
Type vi at the shell prompt to launch the vi editor.
You can also specify a file to load on the command line.
In vi parlance, the onscreen text is called the "buffer", just as "document" is used in most GUI word processors.
Unlike most any editor you're likely used to, vi has "modes" as described below.
You'll find that you switch back and forth between modes frequently.
Consult
Learning the vi Editor to more fully embrace the vi experience.
Modes
Mode | Description |
---|---|
Command | Keystrokes are interpreted as commands for movement, entry into the Insert or Replace mode, deletions, search, replace, file manipulation, and so on. There are no indications that vi is in Command Mode. Press ESC once or twice until the terminal beeps. The beep means Command Mode (sometimes called "Beep Mode"). |
Insert, Replace | Keystrokes are inserted (Insert Mode) into the buffer, or replace existing buffer text (Replace Mode). Either --INSERT-- or --REPLACE-- will appear at the bottom of the screen as a reminder of the current mode. Press ESC to return to Command Mode. |
Commands
Commands are keystrokes entered while in Command Mode.
The case of the command is significant.
Some commands can be executed more than once by first typing the number of repetitions.
There are many, many more commands in vi than listed here.
Insert Commands | Description | Examples |
---|---|---|
i, a | Enter Insert Mode and begin inserting text before, or after the cursor, respectively. | |
I, A | Enter Insert Mode and begin inserting text at the beginning, or end of the current line, respectively. | |
o, O | Enter Insert Mode and begin inserting text on a new blank line inserted below, or above the current line, respectively. | |
r | Remain in Command Mode but replace the character under the cursor with the next keystroke. | rQ replace the character under the cursor with the character Q |
R | Enter Replace Mode and begin replacing text at the current cursor position. | |
cw | Delete the word to the right of the cursor, and enter Insert Mode. | 3cw delete the 3 words to the right of the cursor and enter Insert Mode |
cc | Delete the current line, and enter Insert Mode. | |
ESC | Return to Command Mode. | |
Movement Commands | Description | Examples |
h, j, k, l | Move the cursor one position left, down, up, or right, respectively. On most terminals, the keyboard arrow keys will also work, even in Insert Mode. | j move one line down, 13l move thirteen characters to the right |
w, b | Move the cursor one word forward, or backward respectively. Use capital W or B to move by word, including punctuation. | 4w move 4 words forward, 3B move 3 words backward treating punctuation as part of the words |
e | Move the cursor to the end of the word. Use capital E to end of word, including punctuation. | |
0, $ | Move the cursor to the first or last position of the current line, respectively. | |
G | Move to the specified line number in the buffer. If no line number is given, move to the end of the buffer. | 1G move to the beginning of the buffer, 13G move to 13th line of the buffer, G move to the end of the buffer |
Ctrl+f, Ctrl+b | Scroll forward, or backward one screen, respectively. On most terminals, the keyboard PageUp and PageDown keys will also work, even in Insert Mode. | 3Ctrl+b scroll up 3 screens |
Ctrl+g | Display the current line number and other buffer information at the bottom of the screen. | |
Ctrl+l | Redraw the screen without changing anything. This may be necessary if the terminal displays a message on the vi screen. | |
Editing Commands | Description | Examples |
x, X | Delete character under, or before the cursor, respectively. The deleted text can be restored with the p command. | 5X delete the five characters before the cursor |
dw | Delete one word to the right of the cursor. The deleted text can be restored with the p command. | 6dw delete the 6 words to the right of the cursor |
dd | Delete the current line. The deleted text can be restored with the p command. | 10dd delete the next 10 lines, starting with the current line |
D | Delete to the end of the line. The deleted text can be restored with the p command. | |
p, P | Put deleted text after, or before the cursor, respectively. | |
yw | Yank (copy) the word to the right of the cursor. The yanked text can be restored with the p command. | 2yB yank the two previous words, including punctuation |
yy | Yank (copy) the current line. The yanked text can be restored with the p command. | 4yy yank the next 4 lines, starting with the current line |
J | Join the current line with the following line. | |
u | Undo the last edit command. | |
. | Repeat the last edit command. | |
Search Commands | Description | Examples |
/pattern, ?pattern | Search forward, or backward for pattern respectively. Type Return to begin the search. The pattern can be a text fragment or a regular expression. | /root search forward for "root" ?T[io]m search backward for "Tim" or "Tom" |
n, N | Repeat last search in same, or different direction, respectively. | |
:%s/find_pattern/ replace_pattern/g | Globally replace find_pattern with replace_pattern in the buffer. Type Return to begin the replacement. Either pattern may be a text fragment or a regular expression. | :%s/usr/user/g replace every occurrance of "usr" with "user" |
File Commands | Description | Examples |
:w | Write (save) the buffer to the file on disk. | |
:wq | Write (save) the buffer and quit vi. | |
:x, ZZ | Write (save) the buffer, if changes have been made, and quit vi. ZZ is a handy shortcut. | |
:q! | Quit vi without saving any changes, even if the buffer has been modified. | |
:e! | Restore the buffer to the version at the time of the last write (save). |
Linux Directory Structure |
The various distributions of Linux or UNIX tend to use slightly
different directory structures, or organize files into different
locations.
The following structure is typical of most Linux distributions, and Red
Hat in particular.
Linux uses the term "directory" instead of "folder" as is preferred by the Macintosh and recent versions of Windows.
Path | Description |
---|---|
/ | The root directory. Don't confuse this with the root user directory (/root). The parent directory (..) of / is / itself. |
/bin | Essential user commands, such as ls or bash. |
/boot | Location of the kernel itself, as well as the boot manager (e.g. LILO or GRUB). |
/dev | Files that represent hardware devices. For instance /dev/hda2 corresponds to the second partition of the first IDE hard disk, and /dev/fd0 represents the first floppy drive. A special device, /dev/null, is like a black hole: anything that is copied or streamed to /dev/null (also known as the "bit bucket") disappears forever. Another special device, /dev/zero, is like the opposite: it spews out an infinite stream of zeros (technically, ASCII NUL). There are usually many more files in this directory than there are hardware devices installed. You'll rarely need to work directly with this directory. |
/etc | System configuration files. Similar in concept to Windows' Registry. |
/etc/rc.d | The hierarchy for startup configuration scripts. |
/etc/rc.d/init.d | Startup configuration scripts. |
/etc/rc.d/rcN.d | Runlevel directories. Each of the rcN.d directories corresponds to runlevel N. The scripts in each directory (typically links to the scripts in /etc/rc.d/init.d) specify which services should start, or stop, for that particular runlevel, and in which order. |
/home | Home directories for user accounts (other than the root user). Each user's directory will be directly beneath this directory: /home/user_name. |
/lib | System libraries shared by various programs. These library files are similar in intent to Windows' DLL files. The /lib/modules/kernel_version directory contains the kernel modules, such as device drivers. |
/mnt | The preferred location of temporarily-mounted device filesystems, such as the floppy drive (/mnt/floppy) or the CDROM (/mnt/cdrom) drive. Depending on system settings, these device filesystems may be automatically mounted when a disk is inserted into the drive, or you may have to mount themselves using the mount command. |
/proc | The mount point for a virtual filesystem that contains files and directories that represent current system settings and statistics. For instance, /proc/meminfo shows the current status of the system memory, and /proc/version displays the current kernel version. There are also subdirectories for each of the running processes, identified by the process ID (pid) numbers. |
/root | The root user directory. |
/sbin | Essential privileged commands, usable only by the root user, such as shutdown. |
/tmp | Temporary files used by the system or users. Don't store important files in this directory as they may be erased without notice. |
/usr | The primary hierarchy for programs, data, and documentation on the system. The /usr directory may actually be located on a separate system, but accessed remotely. Therefore the /bin and /sbin directories must contain all of the utilities necessary to launch the system before any network connections are made. |
/usr/bin | The bulk of the user commands. |
/usr/local | Programs, data, and documentation installed locally on this system. This directory is the preferred location for additional software packages that were not part of the initial Linux installation. The /usr/local/bin and /usr/local/sbin directories parallel those higher up in the hierarchy. |
/usr/sbin | The bulk of the privileged commands, usable only by the root user. |
/usr/src | Source code files for various programs on the system, possibly including the kernel itself. |
/var | Variable (changing) administrative files, such as log files. |
Linux Filesystem Basics | |
File Names
While technically any character other than slash (/) or the ASCII NUL can be used in a filename (depending on the filesystem),
it's best to restrict yourself to alphanumerical characters (remember that Linux is case-sensitive),
underscore (_), hyphen (-), and period (.).
While
space characters are allowed, they're more trouble than they're worth
because you'll have to enclose the filename in quotes every time you
reference it.
There is no concept of a "file extension" in Linux as there is
in Windows.
It's common to append ".txt" to text files, or ".c" to C source files,
but doing so has no intrinsic meaning to the operating system.
Executable files are identified by their access permissions rather than
an ".exe" extension (see the File Listings section below).
File Types
Almost every "thing" in Linux can be considered some type of file.
The types of files are listed below (note that in this case "file type" does not distinguish a text file from a GIF image file,
but indicates the various "things" that Linux treats like files).
The Listing Character is the leftmost character in the detailed file listing that is displayed using the ls -l command.
The Listing Colour is the colour of the filename displayed by the ls command (in the default Red Hat installation at any rate).
Listing Character | Listing Colour | File Type | Description |
---|---|---|---|
- | normal text colour, or green for executable files | regular file | A normal text, data, or program file. |
d | dark blue | directory | A directory, containing possibly other files or directories. |
l | light blue | symbolic link | A symbolic (soft) link to another file. See the Links section below. |
b | yellow | block-oriented device | A device that reads and writes data in blocks, such as a hard disk. Typically only found in the /dev directory. |
c | yellow | character-oriented device | A device that reads and writes data in characters, such as a terminal or a serial port. Typically only found in the /dev directory. |
p | brown | pipe | An interprocess communication (IPC) mechanism. Also used in memory when piping (|) the output of one program into the input of another using the shell. Typically only found in the /dev directory. |
s | pink | socket | An interprocess communication (IPC) mechanism. Not exactly equivalent to TCP/IP sockets. Typically only found in the /dev directory. |
Links
It is possible to create a link to an existing file that, for all intents and purposes, appears to be the original file,
although it may have a different name or be in a different location.
Links are created using the ln command.
There are two types of link: hard and soft (symbolic).
Hard links cannot easily be identified as such, although the
file listing will reflect the number of times a file has been
hard-linked
(see the File Listings section below).
Hard links also have other limitations: directories cannot be
hard-linked (except by the root user), and links cannot cross
filesystem boundaries.
Soft, or symbolic, links are generally preferred because, even
though they act just like hard links, they can easily be identified for
what they are,
and they don't suffer from the same limitations as hard links.
Deleting a link (hard or soft) does not delete the file that is linked to.
File Listings
When displaying files using the long output format (ls -l), a typical file might look as follows:
-rwxr-xr-- 2 sally managers 48712 Jan 4 09:11 calcpayroll
The first character is the file type, in this case "-" means a regular file.
The next 9 characters indicate the file permissions.
The permissions are split into three groups of three characters each: owner, group, and others.
An r means the file can be read (or a directory can be listed).
A w means the file can be written to, or modified.
An x means the file can be executed (or a directory can be navigated to); in other words it is a program of some sort.
In this case, the first three characters, rwx means that the owner of the file can read, write, and execute this file.
The second set of three characters, r-x means that members of the group can read and execute the file, but not write to it.
The final set of three characters, r-- means that any other user can read the file, but cannot write to it nor execute it.
The number 2 indicates the number of hard links to this file. If there are no hard links, a 1 will be displayed.
sally is the owner of the file.
sally belongs to the managers group,
and members of that group have the access permissions described above (the second set of three permission characters).
The file is 48712 bytes in size.
The file was last modified on January 4th (of this year) at 9:11AM. Older dates will display a year instead of the time.
The file is named calcpayroll.
Filename Expansion
Just as in MS-DOS, the Linux shell can use wildcards to specify filename patterns. This is also sometimes called "globbing".
Don't confuse Filename Expansion with Regular Expressions, which are far more powerful and use a more complex syntax.
Another important but subtle difference is that MS-DOS passes
the wildcard string to the command program for it to handle.
The Linux shell first evaluates the wildcard, expands it into a list of
all of the matching files, and passes this full list to the command
program.
This distinction becomes apparent when you write shell scripts that
accept filename parameters.
Wildcard | Description | Examples |
---|---|---|
? | Matches exactly one character. | T?m matches Tim, Tom, T7m, etc. |
* | Matches zero, one, or many characters. | *.* matches any filename that includes a period (note difference from MS-DOS!), * matches all filenames, ??* matches all filenames with at least two characters |
[set] | Matches any one character in set. A hyphen can be used to indicate sequences. | s[aeI]t matches sat, set, and sIt; *[0-9] matches all filenames that end in a digit; [a-zA-Z]?* matches all filenames that begin with a letter, and are at least two characters in length |
[!set] | Matches any one character not in set. A hyphen can be used to indicate sequences. | T[!i]m matches Tom, Tum, T_m, and so forth, but not Tim |
Filesystems
A filesystem is a device (realor virtual) that can be formatted to store information organized as
files.
Many different filesystems can, and are, mounted (loaded) into the
Linux directory hierarchy.
Some filesystems have limitations (e.g. MS-DOS's 8.3 filenames, and no
concept of access permissions) but the filesystem driver will attempt
to present
each filesystem in such a way that the user usually doesn't know or
care which filesystem he or she is using.
The following table lists the most common filesystems.
The Type column contains the name used when mounting a filesystem using the mount command.
Filesystem | Type | Description |
---|---|---|
Second Extended | ext2 | The most common Linux filesystem (prior to ext3). |
Third Extended | ext3 | The latest Linux standard filesystem as of the 2.2.19 kernel but not included in many distributions until very recently (Red Hat 7.2). ext3 features "journalling" which can help prevent file corruption during catastrophic system failure (e.g. power failure). |
Network File System | NFS | Used for remote access to files on other *NIX systems on the network. |
DOS-FAT | msdos | Used mainly for floppy disks. Filenames are limited to the old 8.3 convention. |
VFAT | vfat | The filesystem used by Windows 9x that supports long filenames. Not to be confused with FAT32 though. It's common to use this filesystem on multi-boot machines to share files between Windows and Linux. |
/proc | proc | The virtual filesystem used by the /proc directory to display process information. |
ISO 9660 | iso9660 | The filesystem used by CD-ROMs. Current drivers include the Joliet extensions that allow long or Unicode filenames. |
SMB | smbfs | Used for remote access to files on Windows machines (Samba). |
Week 2 Linux Commands |
The following table demonstrates only some of the options possible for each command.
Consult the man pages for the complete list, or a reference such as
Linux in a Nutshell.
Help Commands | Description | Options | Examples |
---|---|---|---|
apropos | Display all of the man pages that match the search string | none | apropos filesystem displays all of the man pages relating to filesystems |
info | The GNU hypertext reader. Displays online documentation for most GNU Project commands. Type q to exit. | see documentation | info egrep displays the documentation for the egrep utility |
man | Display the online reference manuals. Type q to exit. The manual pages are divided into sections: 1. executable programs or shell commands, 2. kernel system functions, 3. library functions, 4. special files (devices), 5. file formats and conventions, 6. games, 7. macros and miscellaneous, 8. system administration commands, 9. kernel routines. Usually you will be interesting in sections 1 and 8. | see documentation | man man displays the online manual for the man(1) command |
Navigation Commands | Description | Options | Examples |
cd | Change directory. | none | cd changes to the user's home directory; cd ~ changes to the user's home directory; cd .. changes to the parent directory (unlike MS-DOS, space must be between cd and ..); cd - changes to the previous directory |
find | Searches for a file or files that match the specified conditions. File expansion wildcard patterns must be enclosed in quotes ('). | -name pattern find filenames that match pattern; -type find files of a specific Linux file type | find / -name httpd.conf displays any copy of the file httpd.conf, beginning the search at the root directory; find /etc -name 'rc*d' -type d displays any directories that begin with "rc" and end with "d" under the /etc directory |
ls | List files. If no parameters are given, list all of the files in the current directory. It's common to have an alias for ls -l called ll, and an alias for ls -d .[a-zA-Z]*, which displays just the hidden files in a directory, called l. ("el dot"). | -l long format; -d do not descend into subdirectories; -a display all files, including hidden ones; -F append file type flags to the listing (/=directory, *=executables, @=symbolic links, |=FIFOs or pipes, "="=sockets); -R recursively list subdirectories; -s display the size of the files in blocks | ls -laFsR ~ recursively lists all of the contents the user's home directory and all of its subdirectories, displaying block sizes for each file and appending a file type flag to the filename if warranted; ls -d /etc/rc* displays all of the files beginning with "rc" in the /etc directory without descending into any directories that begin with "rc" |
pwd | Print the full pathname of the current directory. | none | pwd displays, for example: /usr/local/bin |
File Display Commands | Description | Options | Examples |
cat | Display a file or files on the standard output (terminal window). Short for "catenate". Often used with redirection operators to combine files. | see documentation | cat httpd.conf displays the httpd.conf file contents cat one two three > all copies the contents of one, two, and three, into all |
file | Displays the type of a file according to its contents. | -L follow symbolic links | file index.html displays: index.html: HTML document text |
less | Display the contents of a file one screenful at a time. A "more" capable version of more. Type q to exit. | see documentation | less /etc/profile displays the contents of the /etc/profile file one screen at a time |
more | Display the contents of a file one screenful at a time. A "less" capable version of less. Type q to exit. | see documentation | more /etc/passwd displays the contents of the /etc/passwd file one screen at a time |
File Manipulation Commands | Description | Options | Examples |
cp | Copy files from one location to another. It's common to create an alias to cp -i called cp to prevent accidents. | -i prompt before overwriting; -f force the copy, even if the target file exists; -r recursive | cp /tmp/foo* . copies every file in the /tmp directory that begins with "foo" to the current directory (.) |
ln | Create a link for a file. Hard links cannot cross filesystem boundaries, nor can they be created for directories unless you are the root user. The name or the location of the link can be different from the source file. | -s create a symbolic, or soft, link | ln -s /var/log/dmesg kernel.msg creates a symbolic link in the current directory called kernel.msg that points to the file /var/log/dmesg |
mkdir | Create a directory. | see documentation | mkdir foo creates the directory foo under the current working directory |
mv | Move one or more files from one location to another, or rename a file. It's common to create an alias to mv -i called mv to prevent accidents. | -i prompt before overwriting; -f force the move, even if the target file exists; -r recursive | mv foo.txt /tmp/foo.txt moves the file foo.txt from the current directory to the /tmp directory; mv -i foo.txt quux.txt renames foo.txt to quux.txt, but asks first if quux.txt already exists |
rm | Delete (remove) files. Deleted files cannot be recovered It's common to create an alias to rm -i called rm to prevent accidents. | -i prompt before overwriting; -f force the deletion; -r recursive | rm -fr / tmp/* completely erases the entire system, and anything under the tmp subdirectory of the current directory if it exists (this is why habitually logging on as the root user is a very bad idea); rm -fr /tmp/* erases just the contents of the /tmp directory (notice subtle difference from the previous example) |
rmdir | Removes a directory, only if it contains no files or subdirectories. Use rm -r (carefully!) to erase populated directories. | see documentation | rmdir ~/tempdir removes the empty directory tempdir under the user's home directory |
Filesystem Commands | Description | Options | Examples |
fdformat | Low-level format a floppy disk. Run on an unmounted floppy before installing the filesystem. A non-root user must have write permissions on the floppy device. | -n skip verification step | fdformat /dev/fd0 formats the floppy disk |
mkfs | Construct a filesystem on a device. mkfs is a front-end for other programs specific to the given filesystem. Run on an unmounted device. A non-root user must have write permissions on the floppy device (and may not have mkfs in their default path). | -t type specifies the filesystem type | mkfs -t msdos /dev/fd0 creates an MS-DOS filesystem on the floppy device; mkfs.msdos /dev/fd0 same as above, using the specific filesystem mkfs program |
mount | Mounts a filesystem to a point in the directory hierarchy. The mount point (directory) must exist and should be empty. The /etc/fstab file lists which filesystem mounts are known to the system, and which can be mounted by non-root users. If non-root users are permitted to mount certain devices (floppies and CD-ROMs typically) they do not have any choice over the mount point or the filesystem used. Otherwise, only the root user is permitted to execute this command. | -t type specifies the filesystem type to mount; -r mounts the filesystem read-only (required for CD-ROM drives) | mount displays the current mounts (root & non-root); mount -t msdos /dev/fd0 /mnt/floppy mounts an MS-DOS-formatted floppy in the first floppy disk drive at the location /mnt/floppy (root only); mount -t iso9660 -r /dev/cdrom /mnt/cdrom mounts a CD-ROM in the CD-ROM drive at the location /mnt/cdrom (root only), mount /mnt/floppy mounts the floppy using the filesystem specified in the /etc/fstab file (root & non-root); mount /dev/cdrom mounts the CD-ROM at the location specified in the /etc/fstab file (root & non-root) |
umount | Unmounts a filesystem from the directory hierarchy previously mounted using the mount command. Always unmount a removable-media device (e.g. floppy, CD-ROM) before ejecting the media. This precaution is necessary because Linux buffers its writes to devices and your most recent changes may not have been committed to the media. Even with CD-ROMs, it's a good idea to unmount the disk so that Linux doesn't become confused. Under certain circumstances a non-root user may unmount some filesystems (floppies and CD-ROMs typically) but otherwise only the root user can execute this command. | see documentation | umount /mnt/floppy unmount the floppy disk; umount /dev/fd0 unmount the floppy disk (same as above); umount /mnt/cdrom unmount the CD-ROM |
Session Commands | Description | Options | Examples |
exit | Exits the current (sub)shell. Equivalent to logout if run from the parent shell. | see documentation | exit exits the current shell |
logout | Logs the user out of the system. Use exit if you are currently within a subshell. | none | logout logs the user out of the system |
Week 2 Class Exercises |
Perform all of these exercises through the shell terminal window.
Change your user password on the nfs2 server:
Use the telnet program to remotely log in to the nfs2 server: telnet nfs2
Your user name is the first letter of your first name followed by your complete last name.
Your password is your student number without any leading zeros.
Change your password: passwd
You will have to enter your old password first, and then your new password twice.
Exit the telnet program: exit
Try telnet-ing back into the nfs2 server using your new password to make sure it works.
Experiment with the apropos, man, and info commands:
Use the commands whenever you're unsure about the syntax of a command.
For instance, look up any man pages that mention links.
Then look at the man page and the info page for the ln command.
Find an example of each of the Linux filetypes on your system:
Use the cd command to move around, and the ls -l command to view the full listing of a directory.
Try to find examples of: normal files, directories, links, block devices, character devices, pipes, and sockets.
Hint: the last four can be found in the /dev directory.
Use the -d and -F options for ls, together with wildcards, to help identify some of these file types.
Look for files on the system using the find command:
Search for the following: the ls command, directories under /etc that begin with "rc", the Pine configuration file: pine.conf.
Use the cat, more, and less commands to view files:
Take a look at the following: /etc/fstab, /etc/passwd, and the pine.conf file from the previous step.
The first two are relatively short, while the last is probably many screens long.
Create a symbolic link:
Create a symbolic link in your home directory to the file /etc/passwd.
Experiment with viewing the file through the link.
View the listing of the link using the ls -l command.
Try to modify it.
Create and delete new files and directories:
In your home directory, create a new directory called "scratch".
In that directory create a new file using vi called "erase.me".
You can type whatever you'd like into the file.
Exit vi and use cat, or less, or more, to view the contents of the erase.me file.
Try to delete the scratch directory using the rmdir command.
Delete the erase.me file first and then try deleting the directory again.
View the filesystems currently mounted on your system:
Run the mount command without any options to view the current mounts. Are there any that you can't explain?
If you have a floppy disk handy, put it into the machine and see if the mount then shows different information.
If not, try mount /dev/fd0 first. Don't forget to umount.
Add a HTML home page to your account on the nfs2 server:
Telnet into the nfs2 server as in the first step.
Change to the public_html directory.
Using vi, create a new file called "index.html".
Enter the following into the file: This is your_name_here's home page..
Save the file and try viewing it through your systems browser by going to http://nfs2/~your_username/.
If you want to view it outside of the College, use the URL http://netlab2.yukoncollege.yk.ca/~your_username/.
You can use this site for the duration of the course for whatever legitimate and non-offensive purposes you'd like.
The sites are monitored by College administrators for inappropriate content.
Logout and shut down the system:
Exit the terminal shell program using the exit command.
Log out of the system through the graphical interface (click the bottom-left icon then choose logout from the menu).
Shut down the system by selecting the shutdown option from one of the login dialog box's menus.