Introduction Lab 2: Linux/Unix shell Basics Navigating Shortcuts and globs Rearranging files Looking at files I/O Comp Sci 1585 Redirecting I/O STDERR Data Structures Lab: Environment Tools for Computer Scientists variables Tricks Processes Getting help
Outline Introduction 1 Introduction Basics Navigating Shortcuts and 2 Basics globs Rearranging files Navigating Looking at files Shortcuts and globs I/O Rearranging files Redirecting I/O STDERR Looking at files Environment variables 3 I/O Tricks Redirecting I/O Processes STDERR Getting help 4 Environment variables 5 Tricks 6 Processes 7 Getting help
What is a shell? Introduction Basics Navigating Shortcuts and globs Rearranging files Looking at files I/O Redirecting I/O STDERR Environment variables Tricks Processes Getting help
What is a shell? Introduction • login is a program that logs users in to a computer. Basics • When it logs you in, login checks /etc/passwd for Navigating Shortcuts and your shell. globs Rearranging files • After it authenticates you, it runs whatever your shell Looking at files I/O happens to be. Redirecting I/O STDERR • Shells give you a way to run programs and view their Environment output. variables • They also usually include some built-in commands. Tricks • Shells use variables to track information about commands Processes and the system environment. Getting help • The standard interactive shell is bash : • There are others, though, e.g., zsh and fish .
Outline Introduction 1 Introduction Basics Navigating Shortcuts and 2 Basics globs Rearranging files Navigating Looking at files Shortcuts and globs I/O Rearranging files Redirecting I/O STDERR Looking at files Environment variables 3 I/O Tricks Redirecting I/O Processes STDERR Getting help 4 Environment variables 5 Tricks 6 Processes 7 Getting help
Outline Introduction 1 Introduction Basics Navigating Shortcuts and 2 Basics globs Rearranging files Navigating Looking at files Shortcuts and globs I/O Rearranging files Redirecting I/O STDERR Looking at files Environment variables 3 I/O Tricks Redirecting I/O Processes STDERR Getting help 4 Environment variables 5 Tricks 6 Processes 7 Getting help
Navigating the filesystem Introduction Basics Navigating Shortcuts and globs • $ ls L i s t files. You can give it a directory to list. Rearranging files Looking at files $ ls -l Display the output in a detailed l ist, one line • I/O per file. Redirecting I/O STDERR $ ls -h Display file sizes in a h uman-readable format. • Environment variables $ ls -a Display a ll files, including hidden ones. • Tricks • $ pwd P rint w orking d irectory. Processes Getting help • $ cd DIRECTORY C hange d irectory. $ cd without a directory takes you $HOME . • $ cd - takes you to the previous directory you were in. •
Outline Introduction 1 Introduction Basics Navigating Shortcuts and 2 Basics globs Rearranging files Navigating Looking at files Shortcuts and globs I/O Rearranging files Redirecting I/O STDERR Looking at files Environment variables 3 I/O Tricks Redirecting I/O Processes STDERR Getting help 4 Environment variables 5 Tricks 6 Processes 7 Getting help
File and Directory Shortcuts Introduction Basics Navigating Shortcuts and globs Rearranging files • . always refers to the directory you are currently in. Looking at files • .. always refers to the parent of the current directory. I/O Redirecting I/O STDERR • ~ refers to your home directory. Environment variables • / refers to the root directory. Everything lives under root. Tricks • Globs: Processes * matches 0 or more characters in a file or directory name • Getting help ? matches exactly one character in a file or directory • name • For example, $ ls *.cpp lists all your cpp files.
Outline Introduction 1 Introduction Basics Navigating Shortcuts and 2 Basics globs Rearranging files Navigating Looking at files Shortcuts and globs I/O Rearranging files Redirecting I/O STDERR Looking at files Environment variables 3 I/O Tricks Redirecting I/O Processes STDERR Getting help 4 Environment variables 5 Tricks 6 Processes 7 Getting help
Rearranging files Introduction Basics • $ mv SOURCE DESTINATION M o v e (or rename) files. Navigating Shortcuts and globs $ mv -i I nteractively ask you before overwriting files. Rearranging files • Looking at files $ mv -n N ever overwrite files. • I/O Redirecting I/O STDERR • $ cp SOURCE DESTINATION C o p y files. Environment variables $ cp -r R ecursively copy directories, which is what you • Tricks want to do. Processes • $ rm FILE R e m ove one or more files. Getting help $ rm -f F orcibly remove nonexistent files. • • $ mkdir DIRECTORY M a k es a dir ectory. $ mkdir -p DIRECTORY/SUBDIRECTORY Makes every • missing directory in the given p ath
Outline Introduction 1 Introduction Basics Navigating Shortcuts and 2 Basics globs Rearranging files Navigating Looking at files Shortcuts and globs I/O Rearranging files Redirecting I/O STDERR Looking at files Environment variables 3 I/O Tricks Redirecting I/O Processes STDERR Getting help 4 Environment variables 5 Tricks 6 Processes 7 Getting help
Looking at files Introduction • $ cat [FILE] Print out file contents. Basics • $ less [FILE] Paginate files or STDIN. Navigating Shortcuts and globs • $ head [FILE] Print lines from the top of a file or Rearranging files Looking at files STDIN. I/O • $ tail [FILE] Print lines from the end of a file or Redirecting I/O STDERR STDIN. Environment variables $ tail -n LINES Print LINES lines instead of 10. • Tricks $ tail -f Print new lines as they are appended • Processes ( $ tail only). Getting help • $ sort [FILE] Sort s files or STDIN. $ sort -u Only prints one of each matching line • ( u nique). • Often paired with $ uniq for similar effect. • $ diff FILE1 FILE2 Shows diff erences between files. • a/d/c Added/Deleted/Changed.
Outline Introduction 1 Introduction Basics Navigating Shortcuts and 2 Basics globs Rearranging files Navigating Looking at files Shortcuts and globs I/O Rearranging files Redirecting I/O STDERR Looking at files Environment variables 3 I/O Tricks Redirecting I/O Processes STDERR Getting help 4 Environment variables 5 Tricks 6 Processes 7 Getting help
Outline Introduction 1 Introduction Basics Navigating Shortcuts and 2 Basics globs Rearranging files Navigating Looking at files Shortcuts and globs I/O Rearranging files Redirecting I/O STDERR Looking at files Environment variables 3 I/O Tricks Redirecting I/O Processes STDERR Getting help 4 Environment variables 5 Tricks 6 Processes 7 Getting help
Redirecting I/O Introduction • Each program has three default I/O streams: Basics Navigating • STDIN: input, by default from the keyboard (cin). Shortcuts and globs • STDOUT: output, by default to the screen (cout). Rearranging files Looking at files • STDERR: output, by default to the screen (cerr). I/O Redirecting I/O • We can redirect IO to or from files or other programs. STDERR Environment • $ cmd1 | cmd2 Pipe STDOUT from cmd1 into STDIN variables for cmd2 . Tricks Processes • $ cmd < input.txt Funnel data from input.txt to Getting help STDIN for cmd . • $ cmd > output.txt Funnel STDOUT from cmd into output.txt . • Question: what do you think the following does? $ cmd < input.txt > output.txt
Outline Introduction 1 Introduction Basics Navigating Shortcuts and 2 Basics globs Rearranging files Navigating Looking at files Shortcuts and globs I/O Rearranging files Redirecting I/O STDERR Looking at files Environment variables 3 I/O Tricks Redirecting I/O Processes STDERR Getting help 4 Environment variables 5 Tricks 6 Processes 7 Getting help
STDERR redirection tricks Introduction Basics Navigating Shortcuts and globs Rearranging files • bash uses 1 and 2 to refer to STDOUT and STDERR. Looking at files I/O • $ cmd 2> err.txt Funnel STDERR from cmd into Redirecting I/O STDERR err.txt . Environment variables • $ cmd 2>&1 Funnel STDERR from cmd into STDOUT. Tricks • $ cmd &> all-output.txt Funnel all output from Processes cmd into all-output.txt Getting help • Common usage: $ cmd &> /dev/null dumps all output to the bit bucket.
Outline Introduction 1 Introduction Basics Navigating Shortcuts and 2 Basics globs Rearranging files Navigating Looking at files Shortcuts and globs I/O Rearranging files Redirecting I/O STDERR Looking at files Environment variables 3 I/O Tricks Redirecting I/O Processes STDERR Getting help 4 Environment variables 5 Tricks 6 Processes 7 Getting help
Recommend
More recommend