Linux Bootcamp Fall 2015 UWB CSS Based on: http://swcarpentry.github.io/shell-novice "Software Carpentry" and the Software Carpentry logo are registered trademarks of NumFOCUS.
What this bootcamp is: A brief introduction to Linux and how to use it within UWB CSS environment
What this bootcamp is NOT: In-depth overview of technical aspects of Linux
What is Linux? Perhaps, what is an Operating System?
What do you want to do? Whatever you do, you will need the help of the Operating System...
The Operating System Hardware: CPU, Screen, Mouse, Keyboard,... Operating System: Linux, Windows, MacOSX... Application: GUI/CLI, text editor, web browser,... Operator: Single User, Administrator, guest users, or another computer (machine to machine)
User Interaction Graphical User Interface Command Line Interface
The Command Line Interface (CLI) Prompt User@Computer:Directory$_ rafaelsi@uw1-320-01:~$_
Accessing the CLI (Locally) On MacOS: On Windows: Go to Applications -> Utilities -> Terminal Use Cygwin
Accessing the CLI (Remotely) On Windows: Run PuTTY with settings: Host: UW1-320-lab.uwb.edu Port: 1422 On MacOS: Type in the terminal: slogin -p 1422 NetID@UW1-320-lab.uwb.edu
Aside Regarding Remote Access You can also remotely access a computer s GUI. That is, make a remote computer s windows appear on your current machine. From a CLI, slogin with the -Y option will enable this. If we have time, we may demo this.
Basic Commands
CLI Structure: Read, Evaluate, Print, Loop NetID@uw1-320-01:~$ COMMAND -OPTIONS PARAMETERS OUTPUT NetID@uw1-320-01:~$ _ Some notes: Not all commands produce output to the screen. For some commands, getting nothing back means it worked. Parameters = usually needed; Options = usually optional
A Very Simple First Command: echo Learning objectives of this section: 1. Understand the syntax of a command 2. How to get help 3. What do options and parameters do
ECHO, Echo, echo... rafaelsi@uw1-320-01:~$ echo Hello World! Hello World! rafaelsi@uw1-320-01:~$ _
Sample Problem: How to Get the Following Input rafaelsi@uw1-320-01:~$ echo ############## dddd d ccc c bb b a a rafaelsi@uw1-320-01:~$ _
Getting Help: Command s Manual rafaelsi@uw1-320-01:~$ man echo rafaelsi@uw1-320-01:~$ help echo
Echo, Expressions and Backslash-Escaped Characters rafaelsi@uw1-320-01:~$ echo -e first Line \nsecond line first line second line rafaelsi@uw1-320-01:~$ _
Quiz About Formatted echo What do the following commands output? Why? echo "first\tline\nsecond\tline\n3rd\tline\nfourth\tline" echo -e "first line\nsecond line\n3rd line\nfourth line" echo -e "first\tline\nsecond\tline\n3rd\tline\nfourth\tline"
Back to the Sample Problem rafaelsi@uw1-320-01:~$ echo -e a\ta\nbb\tb\nccc\tc" dddd d ccc c bb b a a rafaelsi@uw1-320-01:~$ _
Echo, echo, echo... rafaelsi@uw1-320-01:~$ echo -e "dddd\td\nccc\tc\nbb\tb\na\ta" dddd d ccc c bb b a a rafaelsi@uw1-320-01:~$ _
Organizing Files
The File System
Current Working Directory: pwd What is a working directory? In a GUI OS, how do we know what the working directory is? Why does what works in a GUI not work in a CLI? What are the implications for navigating a file system?
Specifying Paths in a CLI Questions to share with your neighbor: What is a path? How do we specify a path in a GUI? What is the root directory in Linux? Why is there no equivalent directory in Windows?
Paths in Linux Path separator character is / Default is to assume a path is relative to the current working directory. Note the root directory symbol is the same as the path separator. Example: What s the difference between: echo /net/metis/home/rafaelsi/myfile echo net/metis/home/rafaelsi/myfile
The File System: Basic Navigating Commands To list files and directories: ls To change the current directory: cd Try this: 1) Log in the console, and list the files you have available. 2) Navigate into the MATLAB folder, located inside your Document directory 3) What do you find in there?
Special Navigating Symbols Parent Directory.. Root / Home ~ Current working Directory. Exercise: 1) How do you return to your home folder? Figure out three ways of doing this. 2) How would you extract the contents of a zipped archive into the /tmp folder?
Editing and Manipulating Files
Text Files and Linux How do you communicate between applications in Windows or Mac OSX? Text streams (and their file form) are fundamental to commands (tools) in Linux. Many Linux commands accept text as input and return text as output: creating text files and manipulating text streams is crucial. Many text editors exist to create text files: nano, vim, emacs, nedit, pico, etc.
File Editing (nano) ( ^A often means control-a.) Exercise: 1) Type nano in your terminal and add some content. 2) Save the changes as a new file, exit nano and list the file. 3) Re-open the file with nano, but enabling mouse support this time. (How would you find out how to do that?)
Outputting File Contents to Screen Print file content: cat Print file first lines: head Print file last lines: tail Print all contents page-by-page (Ctrl-f, b, g): less Exercise: 1) Display on the screen the file you create 2) Display just the last 3 lines of the same file 3) Display details of the OS (by inspecting the file /etc/os-release)
But First: Download Some Working Files Execute the following in your home directory: curl -o example.zip http://swcarpentry.github.io/shell-novice/shell-novice-data.zip Questions for the class: 1) What does the curl command do? (How did you find out?) 2) What does the option -o do, and what happens if we don t have it?
File System Manipulation Create a directory: mkdir Move (or rename) files: mv Copy files: cp Remove (or delete): rm Exercise: 1) How to extract zipped content? (I haven t told you the command; use whatever resources you need to find out what to use.) Extract the contents of the examples.zip file in your home directory. 2) How to rename the folder data to examples? 3) Go into examples/data. How would you make a copy of hardware. cfg and put it into the backup directory?
Quiz: What Results from the Following Commands cp file1 file2 rm /tmp/file1 cp../folder2/file. cd.
Manipulating Lots of Files: Wildcards Wildcards allow you to refer to filenames by matching any of one or more characters. The * symbol means any character(s) The? symbols means any single character Exercise: Go to the /tmp directory. How would you list all items that have the word test in their names? How would you list all items that start with thistest but are suffixed by a single number (e.g.,.2 )? Print to screen the contents of all files named alog and then a numeral.
A Motivating Example Why does the following doesn t copy all items in the directory nelle (from the files you downloaded)? cp Users/nelle/* Users/rick/.
Manipulating Directories By default, standard file manipulation commands (i.e., cp, rm, mv) act on files only. They do not affect directories. The -r option affects all sub-directories specified, recursively to the leaves of the tree. Be careful! Use -i to force a confirmation.
Exercise for Manipulating Directories Do the following: Copy examples to examples_copy. Go into examples_copy. Delete the user sub-directory. Make a copy of data in each of the user directories in User. Delete the examples_copy/data directory, with Linux first confirming whether this is what you want to do before it does the deletion.
Piping and Redirection
Linux and the Tools Philosophy Application philosophy : One, big program that tries to do many (related) things. Tools philosophy : Many, small programs that each do only one thing (well), that can be arbitrarily connected together to do (nearly) anything. Connecting requires: A connection medium: In Linux, the medium is a text stream (input and output). Things that connect: In Linux, pipes and redirectors do this.
Piping and Redirection Symbols > Saves output to a file >> Appends output to a file < Redirects input from a file Pipes or redirects: Output from one command as input into another
Examples of Piping and Redirection (In these examples, I m assuming you re in /tmp/examples.) Send output to a file: ls -1 > allfiles Append output to a file: cat hello >> allfiles Receive input from a file: grep What < hello Take output from one command and use it as input to another command: ls -1 wc -l
Exercise in Piping and Redirection How would you count the number of files in the current directory? How would you save (as a single column text files) the names of all files that include the word alog? Search the file longfile and save, in another file, only those lines that have the phrase physics.
Exercise in Piping and Redirection (Answers) (I m assuming you re in /tmp/examples.) How would you count the number of files in this directory? ls -1 wc -l How would you save (as a single column text files) the names of all files that include the word alog? ls -1 *alog* > listofnames Search the file longfile and save, in another file, only those lines that have the phrase physics : grep physics longfile > listofme
A Short Aside on Job Control Exit a text stream input: Ctrl-d Kill the current process: Ctrl-c Kill a process from elsewhere: kill -9 pid (You can get the process ID (pid) from ps or top.)