This commit is contained in:
Miloslav Ciz 2025-03-01 19:31:47 +01:00
parent 5b6494e48c
commit 2b011199db
15 changed files with 1986 additions and 1919 deletions

51
unix.md
View file

@ -27,7 +27,7 @@ Unix then started being sold commercially. This led to its fragmentation into di
For [zoomers](genz.md) and other noobs: Unix wasn't like [Windows](windows.md), it was more like [DOS](dos.md), things were done in [text interface](cli.md) only (even a [TUI](tui.md) or just colorful text was a luxury) -- if you use the command line in "[Linux](linux.md)" nowadays, you'll get an idea of what it was like, except it was all even more primitive. Things we take for granted such as a [mouse](mouse.md), [copy-pastes](copy_paste.md), interactive text editors, having multiple user accounts or [running multiple programs at once](multitasking.md) were either non-existent or advanced features in the early days. There weren't even personal computers back then, people accessed share computers over terminals. Anything these guys did you have to see as done with stone tools -- they didn't have GPUs, gigaherts CPUs, gigabytes of RAM, scripting languages like Python or JavaScript, Google, stack overflow, wifi, mice, IDEs, multiple HD screens all around, none of that -- and yet they programmed faster, less buggy software that was much more efficient. If this doesn't make you think, then probably nothing will.
## How To
## How To For Noobs
UNDER CONSTRUCTION
@ -41,16 +41,57 @@ Learning to use Unix practically means **learning the [command line](cli.md)** p
PRO TIP: convenient features are often implemented, most useful ones include going through the history of previously typed commands with UP/DOWN keys and completing commands with the TAB key, which you'll find yourself using very frequently. Try it. It's enough to type just first few letters and then press tab, the command will be completed (at least as much as can be guessed).
You run a utility simply by writing its name, for example typing `ls` will show you a list of files in your current directory. Very important is the `man` command that shows you a **manual page** for another command, e.g. typing `man ls` should display a page explaining the utility in detail. Short help for a utility can also be obtained by writing `-h` after it, for example `grep -h`.
You run a utility simply by writing its name, for example typing `ls` will show you a list of files in your current directory. Very important is the `man` command that shows you a **manual page** for another command, e.g. typing `man ls` should display a page explaining the `ls` utility in detail. Short help for a utility can also usually be obtained by writing `-h` after it, for example `grep -h`.
Unix utilities (and other programs) can also be invoked with **arguments** that specify more detail about what should be done. Arguments are written after the utility name and are separated by spaces (if the argument itself should contain space, it must be enclosed between double quotes, e.g. `"abc def"`). For example the `cd` (change directory) utility must be given the name of a directory to go to, e.g. `cd mydirectory`.
Some arguments start with one or two minus characters (`-`), for example `-h` or `--help`. These are usually called **flags** and they serve either to set something on/off or to name other parameters. For example many utilities accept a `-s` flag which means "silent" and tells the utility not to write anything out. A flag oftentimes has a short and long form (the long form starts with two minus characters), so `-s` and `--silent` are the same thing. The other type of flag says what kind of argument the following argument is -- for example a common flag is `--output` (or `-o`) with which we specify the name of the output file, so for instance running a C compiler may look like `cc myprogram.c --output myprogram`. Flags accepted by utilities along with their meanings are documented in the manual pages (see above).
Some arguments start with one or two minus characters (`-`), for example `-h` or `--help`. These are usually called **flags** and serve either to turn something on/off or to name other parameters. For example many utilities accept a `-s` flag which means "silent" and tells the utility to shut up and not write anything out. A flag oftentimes has a short and long form (the long one starting with two minus characters), so `-s` and `--silent` are the same thing. The other type of flag says what kind of argument the following argument is going to be -- for example a common one is `--output` (or `-o`) with which we specify the name of the output file, so for instance running a C compiler may look like `c99 mysourcecode.c --output myprogram` (we tell the compiler to name the final program "myprogram"). Short flags can usually be combined like so: instead of `-a -b -c` we can write just `-abc`. Flags accepted by utilities along with their meaning are documented in the manual pages (see above).
TODO: utils, shell, sh (running programs, ...), usual "workflows" (man pages, history, arrows, tab-completion, ...), often used commands, examples, permissions
Now to the very basic stuff: **browsing directories, moving and deleting files etc.** This is done with the following utils: `ls` (prints files in current directory), `pwd` (prints path to current directory), `cd` (travels to given directory, `cd ..` travels back), `cat` (outputs content of given file), `mkdir` (creates directory), `rm` (removes given file; to remove a directory `-rf` flag must be present), `cp` (copies file), `mv` (moves file, including directory -- note that moving also serves for renaming). As an exercise try these out (careful with `rm -rf`) and read manual pages of the commands (you'll find that `ls` can also tell you for example the file sizes and so on).
To **run a program** that's present in the current directory as a file you can't just write its name (like you could e.g. in [DOS](dos.md)), it MUST be prefixed it with `./` (shorthand for current directory), otherwise the shell thinks you're trying to run an INSTALLED program, i.e. it will be looking for the program in a directory where programs are installed. For example having a program named "myprogram" in current directory it will be run with `./myprogram`. Also note that to be able to run a file as a program it must have the executable mode set, which is done with `chmod +x myprogram` (you may have to do this if you e.g. download the program from the Internet). **Programs can also take arguments** just like we saw with the built-in utilities, so you can run a program like `./myprogram abc def --myflag`.
TODO: more more more
Here is a quick cheatsheet of the most common Unix utilities:
| name | function | possible arguments (just some) |
| ------------- | ------------------------------------------------------------------------ | ------------------------------------------- |
| alias | create or display alias (nickname for another command) | alias=command |
| bc | interactive calculator | |
| c99 | C language compiler | file, -o (output file) |
| cd | change directory | directory name (`..` means back) |
| chmod | change file mode | +x (execute), +w (write), +r (read), file |
| cmp | compare files | -s (silent), file1, file2 |
| cp | copy files | -r (recursive, for dirs), file, newfile |
| date | write date and/or time | format |
| df | report free space on disk | -k (use KiB units) |
| du | estimate size of file (useful for directories) | -k (use KiB units), -s (only total), file |
| echo | write out string (usually for scripts) | |
| ed | simple text editor | |
| expr | evaluate expression (simple calculator) | expression (as separate arguments) |
| false | return false value | |
| grep | search for pattern in file | pattern, file, -i (case insensitive) |
| head | show first N lines of a file | -n (count), file |
| kill | terminate process or send a signal to it | processid, -9 (kill), -15 (terminate) |
| ls | list directory (shows files in current dir.) | -s (show file sizes in block) |
| man | show manual page | topic |
| mkdir | make directory | name |
| mv | move (rename) file | -i (ask for rewrite), file, newfile |
| pwd | print working directory | |
| rm | remove files | -r (recursive, for dirs), -f (force) |
| sed | stream editing utility (for replacing text etc.) | script, file |
| sh | shell (the command line interpreter, usually for scripting) | -c (command string) |
| sort | sort lines in file | -r (reverse), -u (unique), file |
| tail | show last N lines of a file | -n (count), file |
| true | return true value | |
| uname | output system name and info | -a (all, output everything) |
| wc | word count (count characters or lines in file, can tell exact file size) | -c (character), -l (lines), file |
TODO: stdin/out/err, utils, shell, sh (running programs, ...), usual "workflows" (man pages, history, arrows, tab-completion, ...), often used commands, examples, permissions, variables and exit codes
## See Also
- [unix philosophy](unix_philosophy.md)
- [Linux](linux.md)
- [GNU](gnu.md)
- [GNU](gnu.md)