Introduction to the
Unix environment
Valeriu Ohan
Bucharest, 2019
What is Unix?
• Operating system (OS) created in the 1970s as a platform for developing software
• Grew in popularity as it was licensed to universities and US government institutions
• Implemented by most of the major computer systems developers (HP-UX, IBM-AIX, Apple-
Mac OS, Sun-Solaris, Microsoft-Xenix)
• Set the standard for data representation, file system hierarchy, inter-process communication,
software modularity, the so called Unix philosophy – “the idea that the power of a system
comes more from the relationships among programs than from the programs themselves”
• Led to the creation of clones: Linux (the most popular), Minix (academic, micro-kernel)
The file system hierarchy
FHS
© https://www.despre-linux.eu/ © https://www.tecmint.com
The terminal emulator
Command line interface
• The simplest interface of any program
• Teams don’t have the resources to invest
in “friendlier” (more complex) GUIs
• All operating systems offer some type of
CLI
• Hundreds of programs that can be
combined in complex tasks
• Good for task automation
• Don’t forget the Tab key
Orientation commands
• help ~ list the built-in commands (the ones integrated in the terminal)
• pwd ~ print working (current) directory (.)
• absolute path (starting from /) vs relative path (starting from .)
• ls ~ list the content of a folder (ls –l, ls –ltr, ls –lSh)
• man ~ manual of a program or command, with option description
• cd ~ change the current directory (cd ~, cd -, cd .., cd /home/gene)
• history ~ view the command history (history –c to clear it)
• hostname ~ print the name of the machine
• which ~ print the full path to a program (not built-in)
• whoami ~ print the current username
• env ~ list the environment variables
Running a custom program
• echo $PATH ~ list of folders where the CLI looks for programs, separated by ‘:’
• hello_ngs vs ./hello_ngs
• Ctrl + C (kill) ~ terminate the current program and remove it from memory
• hello_ngs & ~ run the program in a separate process and leave the CLI free
• Ctrl + Z ~ suspend the current program
• jobs ~ list the programs started by the CLI
• bg %1 ~ move the suspended program in the background, so that the CLI is free
• fg %1 ~ move the suspended program in the foreground, taking over the CLI
• kill %1 ~ terminate job 1
• nohup ~ prevent a program from being terminated when the user logs off (nohup sh –c)
Running multiple programs
• echo $? ~ view the exit code of the last terminated program (default 0)
• prog1 && prog3 ~ run prog1 and, if it’s successful, run prog3
• prog3 && prog1 ~ the same, order changed
• prog3 ; prog1 ~ run prog3 and, after it finishes, run prog1
• prog1 || prog3 ~ run prog1 and, if it fails, run prog3
• prog3 || prog1 ~ the same, order changed
Streams, pipes and
redirection
keyboard stdin - 0 • prog1 > file1 ~ redirect stdout to file1
pipe
monitor stdout - 1 • prog1 1> file1 ~ same as above
monitor stderr - 2
• prog1 >> file1 ~ append stdout to file1
• prog1 2> file1 ~ redirect stderr to a file1
• prog1 &> file1 ~ redirect stdout & stderr to file1
prog1 prog2
• prog2 < file2 ~ redirect file2 into stdin
• prog1 | prog2 ~ pipe stdout into stdin
file1
• prog1 2>&1 | prog2 ~ output duplication into stdin
• prog1 2>&1 1> file1 ~ wrong!, order is important
/dev/null ~ empty stream
file2
•
File and folder commands
• mkdir ~ create a new folder (directory)
• rmdir ~ remove an empty folder
• cat, more, less ~ print the content of one or more files (less prog1, cat prog*)
• head, tail ~ print the beginning or end of a file (default 10 lines) (head –n 20)
• cp ~ copy a file
• mv ~ move (rename) a file
• rm ~ remove (delete) a file
• find ~ find a file (find /home –name “prog*”)
Wildcards
• * ~ zero or more characters (prog* matches prog, prog1, program, program1)
• + ~ one or more characters (prog+ matches prog1, program, program1)
• ? ~ zero or one character (prog? matches prog, prog1)
• [ ] ~ list of characters or range (prog[12] ~ prog1 or prog2, prog[1-9] ~ prog1 to prog9)
• [^ ] ~ negate the list or range (prog[^1] matches prog2 but not prog1)
• shopt [-s|-u] extglob ~ enable/disable global options
• ls !(*.jar) ~ do not list any jar files
File content commands
• grep ~ filter the content of a file (grep –i bmw file3, grep –nr ‘<path>’ –e ‘bmw’)
• wc ~ count the lines, words and characters inside a file
• sort ~ sort the input
• uniq ~ list the unique entries (omit consecutive identical entries)
• cut ~ filter normalized (schemed) formats
• rev ~ reverse the character order of each line of a file
• sed ~ replace the content of a file (sed s/Opel/Vauxhall/g file3)
• tr ~ translate or delete characters from the input stream (tr “ “ “\n”)
• split ~ split a file into multiple files by size or number of lines
• paste ~ merge lines of files
Special commands
• uname ~ print information about the OS (uname –sr ~ version, uname –m ~ architecture)
• chmod ~ change the access attributes of a file (chmod +x ~ executable, chmod –w ~ ro)
• time ~ measure the running time of a program
• sleep ~ pause execution for a specified amount of time (seconds)
• su ~ login as a different user (or superuser)
• sudo ~ run the next command as a different user (sudo su -)
• top ~ view the resource allocation of a machine (top –u gene)
• du ~ show disc usage (du –s /home/gene, du –hd1 /home/gene)
• touch ~ set the modification date of a file to current time or create an empty file
• alias ~ give a different name to a command (with arguments)
• clear ~ clear the terminal window
Putting everything together
nohup sh –c ‘sleep 60; prog3 || prog2 <file3 | cut –dʺ ʺ –f3 | sort | uniq’ 1>result.out 2>error.out &