CS 279 - Week 13 Lecture 1 - 2022-11-14

TODAY WE WILL
*   announcements/reminders
*   a little more history [command]
*   how files are stored in UNIX/Linux
*   a few words on hard and soft [symbolic] links
*   [if time] start intro to Bash functions
*   prep for next class

*   Can start working on Homework 8!
    1st attempts due by 11:59 pm this Friday, November 18

=====
a little more on the history command,
    and how you can use its list
=====
*   history
    *   can see list of previously-done commands

    *   arrow up and down to "scroll" through those commands
        to easily repeat previous commands

*   some other ways to make use of this list of previous commands

    *   !! - redoes the previous command

    *   !str - redo the most-previous command that starts with str

    *   !?str - redo the most-previous command that includes that str

    *   !num - redo the command that has the number (based on the
        history command numbering)

        reminder:
	history | tail    # shows the last 10 lines of the history command output

        history | tail -n 15    # shows the last 15 lines of the history output

    *   !-num - redo the command num commands ago

        !-2 - redoes the command 2 commands ago

    *   a former student mentioned that ^r lets you search history
        interestingly...
	*   (and start typing a command, and type return when you
	    get to the one you want to redo)

    *   ^patt^repl^

        ...redo the most-previous command with patt replaced with repl

        *   emacs looky2.txt
	    ...
	    ^2^4^

	    ...now the command
	       emacs looky4.txt
	       ...is done

        *   are more substitution-options here;

            if you prefer, you can use
	    !!:s/patt/subst/

        *   note that ^patt^subst^ replaces the FIRST instance
	    of patt with subst --

	    there is a global replace:

            you CAN put a g before the s in the :s approach;
	    that is,
	    !!:gs/4/8/   # to replace all 4's in the previous command with 8's

            *   hey! and this works with other expressions besides !! --

	        for example,
		!-2:gs/8/9/    # to replace all 8s with 9s in the command done
		               #    2 commands ago

=====
how files are stored in UNIX/Linux
=====
*   a UNIX/Linux implementation tidbit, that sometimes shows up
    in documentation...!

*   A UNIX/Linux file system stores essential information about a file
    in an i-node (information node)
    *   where the file's contents are actually stored
    *   how long the file is
    *   how many LINKS there are to it <-- hard links, NOT symbolic links
    *   when it was created
    ...and more;

    *   each i-node has an i-number that identifies it (sometimes also
        called an i-node number)

        i-numbers are uniquie WITHIN file systems but not AMONG file systems,
	and, as a result, links across file systems have to be treated
	specially;

        *   hard links across file systems are not allowed,
	    but symbolic links CAN be;

    *   the ACTUAL data in a file is stored in a sequence of BLOCKS
        *   typical for a modern system: block size of 4096 bytes
	    [I hope that's up to date...]

        *   the file system stores files LONGER than one block
	    in SCATTERED data blocks,
	    and uses a SMALLER set of blocks to keep track of where
	    the file's data blocks are

            *   the blocks in this smaller set contain POINTERS to
	        the data blocks
		and are called indirect blocks because they provide
		access to the data indirectly

    *   small files? all the pointers can be stored in the i-node
        very large files? there may be a second level of indirect
	    blocks that point to the first-level blocks, or even
	    perhaps a THIRD level;

        *   and the UNIX utilities that show the number of blocks
	    in a file count INDIRECT blocks as well as the blocks
	    containinf the actual data of the file;

=====
a few words on hard links and symbolic links (soft links)
=====
*   [NOT the same as a hypertext link!!]

*   what is a UNIX/Linux link?
    *   a link to a file is a directory entry for that file,
        consisting of a filename that names that file within the directory,
	along with an i-node number (remember, an i-node number or i-number
	is a unique ID, essentially, for a file within one file system)

    *   a file CAN have multiple links
    *   the links to a file have the SAME i-node number but DIFFERENT
        names

        they can reside in different directories, also,
	(but hard links CANNOT be *across* file systems

    *   The ln command creates a link to an existing file
        (yes, this is something of an alias situation...)

    *   interesting fact: the rm command removes a LINK to a file,
        not the file itself;
	*   a file is not deleted from the system until its LAST link
	    is removed;

        *   with this style of link -- the system can't really tell
	    which is the "original" link, and which are "later"
	    links  - IF I understand correctly;

        *   (every file has at least one link, at least one name connected
	    with an i-inode...)

*   touch first-file.txt
    ln first-file.txt hard-link-to-1st.txt

    ...that should add a 2nd hard link to that file, so it now has
       hard links for the names first-file.txt and hard-link-to-first.txt

    ls -i filename    # to see the inode number!

    ls -i first-file.txt hard-link-to-1st.txt   # should see the same i-node number!

*   (note: if you just see "link", typically a hard link is meant...?)

=====
symbolic links (soft links)
=====
*   references a file indirectly;
*   a symbolic link specifies a PATHNAME;
    if that pathname designates an actual file, then the symbolic link
        refers to that file

    if is designates another symbolic link, then that link is followed in
        turn;

    *   see the difference?
        for a hard link, what is stored is the file name and an inode
	    number;
	for a soft/symbolic link, what is stored is the link name and
	    the PATHNAME to the linked-to file; it is clear, then,
	    which is the "original"

*   ln with the -s option creates a symbolic/soft link

    ln -s orig-file symbolic-link-to-orig-file

    try:
    ls -i orig-file symbolic-link-to-orig-file
    ...they have different i-node numbers

    ls -l orig-file symbolic-link-to-orig-file
    ...SEE that the symbolic-link entry SAYS what it refers to!
       AND its permissions string STARTS with an l !

    ls -li orig-file symbolic-link-to-orig-file
    ...does give both the i-node number along with the usual long-listing info

*   CAREFUL --
    if two names are hard links to the same i-node (have the same i-node
        number), removing one does not affect the other.

    BUT if a soft/symbolic link links to one of those hard links,
        and you remove the file it is linked TO, that soft link is
	now "broken" (complains about being a nonexistent file if used)