Command Line Fundamentals Getting Started with the Terminal ██ What is the Terminal? 2 / 31 ██ Terminal/Shell Open a terminal. • When you type something into the command line, a program called "the shell" interprets it and does something • So, there are a couple of things that are required for the shell to correctly interpret what you want to happen... ◦ Command line entries start with a command name, followed by options and arguments ◦ Some commands don't need any options or arguments ◦ The command name, options, and arguments are separated by spaces
3 / 31 ██ First Commands Let's try some basic commands: • date - displays the current date and time
• whoami - shows your username
• clear - clears the terminal screen
• pwd - print working directory (where you are)
4 / 31 ██ Tab Completion and Command History Two of the most useful features of the shell: Tab Completion • Start typing a command or filename and press Tab • The shell will complete it for you (or show options) • Saves time and prevents typos Command History • Press Up Arrow to see previous commands • Press Down Arrow to go forward through history • You can edit and re-run old commands 5 / 31 ██ Where are my files? 6 / 31 ██ Navigating • Your computer has a set of files organized into directories • Example: ◦ Open a file browser ◦ Open a terminal Commands: • pwd - print the directory you are currently in
• ls - list the files and directories that are in the current directory
• cd - change directories
7 / 31 ██ Demo 8 / 31 ██ Anatomy of a Command Entry: Command Arguments and Options • Commands can take arguments • The command does something with/to the argument ◦ Examples: ▪ cd Desktop
▪ ls Desktop
• Options change the behavior of a command ◦ Examples: ▪ ls -l
▪ ls -l -a
• So, when you type in a command you are telling the shell: ◦ What you want to do (the command) ◦ What you want to do it to (the argument(s)) ◦ How you want it done (the option(s)) 9 / 31 ██ Looking at Files 10 / 31 ██ Plain Text Files • A lot of work we are going to do will be centered around creating, reading, and modifying plain text files • What is a plain text file? ◦ Open up "LibreOffice" and create a new document ◦ Open up Text Editor, and create a new file
◦ Now open a terminal and cd to the directory that you just saved them in
◦ Run the cat command on each file
• Plain text files contain just text
11 / 31 ██ Sandboxes • I will create "Sandboxes" for us to practice in. • These are just folders with files that are safe to delete/move/etc. • Let's download and unpack the Module 01 sandbox. 12 / 31 ██ Looking at File Contents There are several commands for looking at the content of files: • cat - print contents of file(s) to screen
• head - print first 10 lines of file(s) to screen
• tail - print last 10 lines of file(s) to screen
• less - show the contents of a file in a "pager"
◦ Use arrow keys or Page Up/Down to scroll ◦ Press q to quit
• wc - print the number of lines, words, and characters in the file
13 / 31 ██ WARNING If you enter any of these commands without a filename, they will go into "stdin mode" $ cat
You have to press Ctrl-C to exit 14 / 31 ██ Demo 15 / 31 ██ Basic File Operations 16 / 31 ██ Creating/Deleting/Moving Files and Directories Commands: • mkdir - make a new directory
• touch - create an empty file (or update timestamp)
• cp - copy a file or directory
• mv - move a file or directory (also used to rename)
• rm - remove a file or directory
17 / 31 ██ Demo 18 / 31 ██ Be Careful with rm!
The rm command is permanent
• There is no "Recycle Bin" or "Trash" on the command line • Once a file is removed, it's gone Tips: • Double-check before running rm
• Start with ls to see what you're about to delete
• Use rm -i for interactive mode (asks for confirmation)
19 / 31 ██ Getting Help Most commands accept a --help and/or -h option.
20 / 31 ██ Getting Help: Manual Pages • All of these commands come with an instruction manual
• We can read the manual using the man command
$ man ls
$ man grep
$ man rm
• Use arrow keys or Page Up/Down to scroll • Press q to quit
• Search with / followed by search term
21 / 31 ██ Spaces in Filenames • Spaces in file or directory names cause a problem because the shell uses spaces to separate the command name, command options, and command arguments • If you try to remove a file named Report 1.pdf with rm Report 1.pdf, the rm command will try to remove two files, one named Report, and one
named 1.pdf
• There are two ways to fix this: ◦ Put quotes around the name: rm "Report 1.pdf"
◦ Escape the space (put a backslash in front of it): rm Report\ 1.pdf
22 / 31 ██ Demo 23 / 31 ██ Wildcards: The * Glob
• Sometimes you want to refer to multiple files at once • The * character is a wildcard that matches any number of characters in a filename
• The shell expands * before the command runs
Examples: • ls *.txt - list all files ending in .txt
• ls data-* - list all files starting with data-
• cat *.log - print contents of all .log files
24 / 31 ██ Important: The Shell Does the Expanding • The command never sees the *
• The shell replaces it with the list of matching filenames first
• You can see exactly what the shell will expand by using echo:
$ echo *.txt
file-1.txt file-2.txt notes.txt
$ echo data-*
data-001.csv data-002.csv data-003.csv
• This is a useful trick for checking what files will be matched before running a command like rm
25 / 31 ██ Demo 26 / 31 ██ Practice Download a practice directory and perform the following tasks: 1. Navigate to the sandbox for this module. 2. How many files are in the top-level sandbox directory? 3. How many directories are in the top-level sandbox directory? 4. Are there any hidden files? 5. What is in the Easter Egg? 6. Rename the file 'file-1.txt' to 'file-01.txt' 7. Create a directory named "docs" and move the README in it. 8. Create a directory named "backups" and copy the "data", "config", and "log" directories into it. 9. Create a directory named "old" and move the "electron_scattering" and "simulations" directories into it. 27 / 31 ██ Summary So far we have learned: • What the terminal and shell are • How to navigate the filesystem (pwd, ls, cd)
• How to view file contents (cat, head, tail, less)
• How to create and delete files and directories (mkdir, touch, cp, mv, rm)
• How to get help (man)
• How to handle spaces in filenames • How to use * to match multiple files (and echo to preview matches)
Next we'll see how to save these commands in shell scripts! 28 / 31 ██ Homework Let's walk through how to do the homework. 29 / 31 ██ Last Slide This space intentionally left blank 30 / 31 31 / 31