The document provides an overview of the Linux command line interface (CLI), including:
- The CLI does not require graphics and is generally faster for experienced users than a GUI
- The bash shell is the default shell and allows command line completion
- Programs are executed through the shell and can take arguments to alter behavior
- Built-in commands are included with the shell while other programs must be found in the PATH
- Output redirection and piping allows chaining of commands and redirection of streams
2. Shell
• User interface to operating system
– Text shell (cmd, sh, ash, bsh, bash, ...)
– GUI shell (Aqua, Windows, GNOME)
• Choice of shell depends on how computer
is used
– Text shell for speed and experienced users
– GUI shell for ease of use
www.iita.org
3. Command line interface
• User input is text input
• Does not require graphics or mouse, less
resource intensive
• Generally faster (for experienced users)
than GUI
• Examples of CLI
– Quake 1/2/3/4 pressing ~ key brings CLI
– R (statistical package) is CLI
– cmd.exe
– Matlab
www.iita.org
4. Command line interface
• User input is interpreted
• … and executed
• Results rendered as text
• … or as graphics
• In this session we will focus on Linux CLI
“bash”
www.iita.org
5. bash
• Default shell on Ubuntu
• Runs on OSX, Windows, any *NIX
• Compatible with predecessor bsh
– Bourne shell
– Bourne-again shell
• Command line completion (tab key)
• Wikipedia Article
www.iita.org
6. Programs
• Programs do things
• Programs
– Compiled programs, bash scripts, other scripts
(#!/path/to/interpreter)
– Executable +x flag
• PATH variable
– Where to look for matching program
• Executing programs
– In path: # ls
– Not in path: # /home/user/runscript.sh
www.iita.org
7. Program arguments and data streams
• Arguments to program
– Anything that follows the command
• Arguments alter the way program behaves
• Any program has
– One standard input stream STDIN
– One standard output stream STDOUT
– One error output stream STDERR
• System call exec, execve
– int execve(const char *filename, char *const
argv[], char *const envp[]);
www.iita.org
8. Program execution
• Shell will provide
– Running directory
– Arguments
– Stream redirection
• STDIN from console
• STDOUT to console
• STDERR to console
• Run the command # ls -la
– ls is the program /bin/ls
– -la is one argument, but two flags
www.iita.org
9. Bash built-ins
• Shell built in commands
– cd change directory
– pwd print current directory to STDOUT
– echo print to STDOUT
– exec execute command and replace current
shell
– exit
– history show command line history
– alias register alias command
– set and unset
– … plenty more www.iita.org
10. Other programs
• Other useful programs, not part of shell
– ls list files in current directory
– ps list processes
– man display manual pages: # man ls
– kill send signal to process
– nano friendly text editor
– vi, vim unfriendly text editor
– screen text-mode window manager
– grep filter input stream
– cat concatenate files and print to STDOUT
– head and tail print first/last X lines to STDOUT
www.iita.org
11. Hands-on Training
• Start the terminal
• What's the current directory?
• Create a folder “training”
• Navigate to folder
• Create “README” file in “training” folder
• Create “deleteme.txt” file
• Remove deleteme.txt file
• Wipe “training” folder
• Find help for using command rmdir
www.iita.org
12. Expansions
• . expands to current directory
• ~ expands to user's home directory
• .. expands to parent directory
• Brace expansion
– # mkdir {old,new,current}
• Parameter expansion
– $0, $1, $@
• Command substitution
– # `which ls`
www.iita.org
13. Useful commands
• Grep is used to filter lines matching a
pattern
– # grep PATTERN file1 file2 file3
• Cat is used to concatenate files and output
to STDOUT
– # cat file1 file2
• Find is used to search for matching files
– # find /home/ubuntu (list all files there)
– # find . (list all files from current directory)
www.iita.org
14. Output stream redirection
• Command # ls -1 will list files in current
directory
• ls command writes the list of files to
STDOUT
• Instruct bash to redirect STDOUT to a file:
– # ls -1 > filelist.txt
• View file contents
– # cat filelist.txt
• Filter list contents
– # grep 'something' filelist.txt
www.iita.org
15. Appending to file
• Instruct bash to redirect STDOUT to a file:
– # ls -1 > filelist.txt
– Will overwrite contents of filelist.txt
• Instruct bash to append to file
– # ls -1 >> filelist.txt
• Redirecting both STDERR and STDOUT
– # command &> filename
– # command &>> filename
www.iita.org
16. Input stream redirection
• Input stream can be replaced by file input
• grep will use STDIN if no file argument
given
– # grep 'test'
– Expects input on STDIN, type something and
press enter
• Following commands are equivalent
– grep 'D' filename.txt
– grep 'D' < filename.txt
www.iita.org
17. Pipelining
• Sequence of commands where STDOUT
(or STDERR) is attached to STDIN of
following command
• # cat filename.txt | grep 'D'
– cat writes filename.txt to STDOUT
– grep will filter STDIN for lines containing D
www.iita.org