Unix

  • Unix
    • Developed at AT&T Bell Labs in the 1960’s
    • Command Line Interpreter
    • GUIs (Window systems) are now available
  • Linux
    • Originally developed by Linus Torvalds, is a variant of Unix and is open source
  • Mac OS
    • originally based on FreeBSD Unix
    • Mac OS is a UNIX 03-compliant operating system certified by the Open Group)

The “Unix Philosophy”

Emphasis is placed on building simple, short, clear, modular, and extensible code that can be easily maintained and repurposed by developers other than the original creators. The Unix Philosophy favors composability. Maxims include:

  1. Make each program do one thing well. To do a new job, build afresh rather than complicate old programs by adding new “features.”
  2. Expect the output of every program to become the input to another, as yet unknown, program. Don’t clutter output with extraneous information. Avoid stringently columnar or binary input formats. Don’t insist on interactive input.
  3. Design and build software, even operating systems, to be tried early, ideally within weeks. Don’t hesitate to throw away the clumsy parts and rebuild them.
  4. Use tools in preference to unskilled help to lighten a program task, even if you have to detour to build the tools and expect to throw some of them out after you’ve finished using them.

Kernel

The kernel is the heart of the operating system. It interacts with the hardware and most of the tasks like memory management, task scheduling and file management.

Shell

The shell is a command line interpreter; it translates commands entered by the user and converts them into a language that is understood by the kernel.

Input/Output

  • standard input
  • standard output
  • standard error

Redirection

Overwrite:

  • > - output
  • < - input

Append:

  • >> - output
  • << - input

Merge:

  • &> output
  • <& input

Useful commands

Utility

  • echo - write arguments to the standard output
  • wc - word, line, character, and byte count
  • diff - compare files line by line
  • xargs - construct argument list(s) and execute
  • ssh - OpenSSH remote login client
  • scp - OpenSSH secure file copy

Filesystem

  • ls - list directory contents
  • cd - change directory
  • pwd - return working directory
  • mkdir - make directories
  • rmdir - remove directories
  • touch - change file access and modification times
  • cp - copy files
  • rm - remove directory entries
  • mv - move files

Searching

  • find - walk a file hierarchy
  • locate - find filenames quickly
  • grep - file pattern searcher
  • tree list contents of directories in a tree-like format. Requires installation via a package manager such as homebrew.

Reading and manipulating files

  • cat - concatenate and print files
  • less - opposite of more
  • head - display first lines of a file
  • tail - display the last part of a file
  • sort - sort or merge lines of text and binary files
  • sed - stream editor
  • awk - pattern-directed scanning and processing language

Processes

  • ps - process status
  • top - display sorted information about processes
  • kill - terminate or signal a process

Piping

Programs can output to other programs:

program_a | program_b

Permissions

Each file in Unix has an associated permission level. This allows the user to prevent others from reading/writing/executing their files or directories. Use ls -l filename to find the permission level of that file

Permission levels:

  • r means “read only” permission
  • w means “write” permission
  • x means “execute” permission
-rwxrwxrwx
 [1][2][3]

1. user
2. group
3. world

Job control

Interrupting a job while it is executing can be done using UNIX communication mechanisms called signals which provide software interrupts. For example, ^c is a keybinding to SINGINT.

You can also pause a job with ^z, check to see it’s in the background by running jobs, and resume the job in the foreground with fg %<job-number> or in the background with bg %<job-number>.

  • signal - simplified software signal facilities
  • kill - terminate or signal a process
  • nohup - invoke a utility immune to hangups

Scripting

A list of commands to be executed sequentially can be combined into a single file called a script. Syntax can be specific to bash, zsh, or any number of command-line interpreter scripting languages.

Run a shell script with the sh command followed by the file path.

Adding a shebang to the beginning of a script and making it an executable by running chmod +x <file_name> allows the file to be called without the sh command. chmod -x reverts the executable to a plain file.

Resources

Tutorials

Tools