**Command line interface** # File system structure I - Example file system ******************************************************* * / * * | * * +---------+---------+---------+---------+ * * | | | | | * * bin usr home etc tmp * * | | * * +-+-+ +-----+-----+-----+-----+-----+-----+ * * | | | | | | | | | * * ls cat john alice bill carl sam jane lisa * ******************************************************* - Virtually all modern operating systems have a _hierarchical file system_ structure. The structure looks like a tree, drawn upside down with its root at the top. - I assume you're using a UNIX-like system: linux, OS X, WSL2, etc. - A _directory_ (or _folder_) contains other files and/or directories. In the picture, every name is a directory except `ls` and `cat`, which are files. - The _root directory_ is the starting point of the file system hierarchy. It is denoted by the `/` character. # File system structure II - Example file system ******************************************************* * / * * | * * +---------+---------+---------+---------+ * * | | | | | * * bin usr home etc tmp * * | | * * +---+ +-----+-----+-----+-----+-----+-----+ * * | | | | | | | | | * * ls cat john alice bill carl sam jane lisa * ******************************************************* - At any point in time, the OS assumes you are located at some specific directory in the file system. This place is called your _current directory_, and is denoted by the `.` character. - Double dots `..` denotes your _parent directory_. It is the directory containing your current directory, unless you are at the root of the file system, in which case your parent directory is the root itself. E.g., `home` is the parent of `alice`. - Every user has a _home directory_. The `~` character denotes your home directory. It is where you are put when you start a terminal session. In general `~user` denotes the home directory of user `user`. So if `sam` is my home directory, then `~` refers to the `sam` directory, while `~alice` refers to alice's home directory. # Pathnames - Example file system ******************************************************* * / * * | * * +---------+---------+---------+---------+ * * | | | | | * * bin usr home etc tmp * * | | * * +-+-+ +-----+-----+-----+-----+-----+-----+ * * | | | | | | | | | * * ls cat john alice bill carl sam jane lisa * ******************************************************* - You can refer to a file or directory by writing its access path that either starts from the root or from your currect directory. This access path is called the `pathname` of the target. - The forward slash character `/` separates the nodes along the pathname. - A path that starts from the root is called an `absolute pathname`, e.g., `/home/bill` is an absolute pathname to `bill` - A path that starts from the current directory is called a `relative pathname`, e.g. `../../bin/ls` is a relative pathname from my current directory `sam` to `ls` - Every absolute pathname has `/` as its first character; no relative pathname has `/` as its first character. # Command line interface (CLI) - Through CLI the user interacts with the computer by typing in _commands_ on the keyboard, and the computer responds by printing out text messages on the user's terminal. - Compared to the Graphical User Interface (GUI), a CLI is + harder to learn + harder to master + not as pretty + faster + more powerful, in fact, some CLI functionality may not have a GUI equivalent # Shells - A _shell_ is a _command interpreter_. It interprets the commands you type in, executes the commands if possible, prints out the results of execution, or prints out error messages if it can't execute your commands, or if it encounters any problem. - Some common shells that are in general use: + `sh` --- the Bourne shell + `csh` --- the C shell + `tcsh` --- the Tenex C Shell + `ksh` --- the Korn Shell + `bash` --- the Bourne-again shell + `zsh` --- the Z shell # bash I - I assume you'll be using the Bourne-Again Shell (`bash`) since it is the default shell on linux machines and WSL2. It used to be the default shell on MacOS as well but MacOS has since switched to usig `zsh` as default. - The format of a simple `bash` command is > command [flags] [arguments] where white spaces (blanks or tabs) separate the components. e.g. ```bash $ ls $ cp *.txt novel ``` - However, `bash` is a full-blown scripting language as well as an interpreter. It has regular expressions, conditionals, loops, and functions, e.g., ```bash $ for f in ch[1-3].ps do ps2pdf $f done ``` # bash II - All commands entered interactively through the keyboard must end in a newline character by the user pressing the `enter` key. - A valid command is either _internal_ or _external_. The shell understands an internal command and is able to execute it immediately. Examples of internal commands are ```bash $ pwd $ alias l="ls -l" ``` - If the shell does not understand a command, it assumes that it is an external command, and will search all directories specified by the `PATH` variable for a file having that command as name. If the file is found and is executable, the shell loads it into memory and executes it. Examples of external commands are ```bash $ ls $ head file ``` # Useful unix commands I - `ls` --- list files and directories - `cd` --- change directory - `pwd` --- print working directory - `mkdir` --- make directoryies - `rmdir` --- remove directories - `rm` --- remove files and/or directories - `mv` --- move (or rename) files and/or directories - `cp` --- copy files and/or directories # Useful unix commands II - `cmp` --- check if two files are the same - `diff` --- list all differences between two files - `echo` --- echo command line arguments - `cat` --- concatenate files - `less` --- browse text files - `man` --- read manual page for external commands, system calls, and C API - `info` --- similar to `man` - `help` --- ask for help with internal commands # Text files & text editors - A _text file_ is meant to be read by humans. It contains characters that can be safely printed to the screen and printers. It doesn't contain formatting characters or control characters or other binary data. - A _text editor_ is an app that allows the user to conveniently read, write, modify, and save text files. It actually works on _buffers_ in RAM until such time when the user tells it to write those buffers onto secondary storage. - Examples of simple text editors are `nano` on a Unix machine, `TextEdit` on a Mac (in plain text mode), and `Notepad++` on Windows machine. - Some editors are meant to be used for writing program source code. They have extensive features useful to programmers. Examples are `TextWrangler`, `Sublime Text`, and `atom`. - Some advanced general-purpose text editors have features that are useful for editing all kinds of text files. Examples are `emacs` and `vim`. # Standard streams - A _process_ is a program in execution. - A _stream_ is a sequence of bytes. - Every process are allocated 3 _standard streams_ by the OS when it starts: + _standard input_ --- the keyboard + _standard output_ --- the computer screen (or `terminal` or `console`) + _standard error output_ --- the computer screen - The values of these standard streams can easily be changed from the command line by the user # I/O redirection I - A _text file_ is a kind of text stream so it is reasonable to redirect any of the standard I/O streams to a file. - The command ```bash prog < infile ``` runs `prog` with its stardard input coming from `infile`. - The command ```bash prog > outfile ``` runs `prog` with its stardard output redirected to `outfile`. If `outfile` does not exist, it will be created with content of the output from `prog`. If `outfile` exists, its old content will be overwritten by the output from `prog`. # I/O redirection II - The command ```bash prog >> outfile ``` runs `prog` with its stardard output redirected to `outfile`. If `outfile` does not exist, it will be created with content of the output from `prog`. If `outfile` exists, its content will have the output from `prog` appended to its old content. - The command ```bash prog1 | prog2 ``` has the effect of running both `prog1` and `prog2` in parallel, but with the stardard output of `prog1` being the stardard input of `prog2`. # I/O redirection III - The command ```bash prog 2> errfile ``` runs `prog` and redirects the standard error output to the file `errfile`. - It's possible to redirect the standard error output to the standard output. E.g., ```bash prog > outfile 2>&1 ``` runs `prog` and redirects both the standard output and the standard error output to the file `outfile`. In `bash`, you can get the same effect by typing ```bash prog &> outfile ``` # References - To learn more, go to + [Unix Tutorial](http://www.ee.surrey.ac.uk/Teaching/Unix/) + [Linuxcommand](http://linuxcommand.org/index.php) _---San Skulrattanakulchai_