Introducing the Shell
|
|
Files and Directories
|
The file system is responsible for managing information on the disk.
Information is stored in files, which are stored in directories (folders).
Directories can also store other directories, which then form a directory tree.
cd [path] changes the current working directory.
ls [path] prints a listing of a specific file or directory; ls on its own lists the current working directory.
pwd prints the user’s current working directory.
/ on its own is the root directory of the whole file system.
Most commands take options (flags) that begin with a - .
A relative path specifies a location starting from the current location.
An absolute path specifies a location from the root of the file system.
Directory names in a path are separated with / on Unix, but \ on Windows.
. on its own means ‘the current directory’; .. ` means ‘the directory above the current one’.
--help is an option supported by many bash commands, and programs that can be run from within Bash, to display more information on how to use these commands or programs.
man [command] displays the manual page for a given command.
|
Creating Things
|
Command line text editors let you edit files in the terminal.
You can open up files with either command-line or graphical text editors.
nano [path] creates a new text file at the location [path] , or edits an existing one.
cat [path] prints the contents of a file.
rmdir [path] deletes an (empty) directory.
rm [path] deletes a file, rm -r [path] deletes a directory (and contents!).
mv [old_path] [new_path] moves a file or directory from [old_path] to [new_path] .
mv can be used to rename files, e.g. mv a.txt b.txt .
Using . in mv can move a file without renaming it, e.g. mv a/file.txt b/. .
cp [original_path] [copy_path] creates a copy of a file at a new location.
|
Wildcards, Pipes and Filters
|
wc counts lines, words, and characters in its inputs.
* matches zero or more characters in a filename, so *.txt matches all files ending in .txt .
? matches any single character in a filename, so ?.txt matches a.txt but not any.txt .
cat displays the contents of its inputs.
sort sorts its inputs.
head displays the first 10 lines of its input.
tail displays the last 10 lines of its input.
command > [file] redirects a command’s output to a file (overwriting any existing content).
command >> [file] appends a command’s output to a file.
[first] | [second] is a pipeline: the output of the first command is used as the input to the second.
The best way to use the shell is to use pipes to combine simple single-purpose programs (filters).
|
Finding Things
|
find finds files with specific properties that match patterns.
grep selects lines in files that match patterns.
$([command]) inserts a command’s output in place.
|
Shell Scripts
|
Save commands in files (usually called shell scripts) for re-use.
bash [filename] runs the commands saved in a file.
$@ refers to all of a shell script’s command-line arguments.
$1 , $2 , etc., refer to the first command-line argument, the second command-line argument, etc.
Use Ctrl+R to search through the previously entered commands.
Use history to display recent commands, and ![number] to repeat a command by number.
Place variables in quotes if the values might have spaces in them.
Letting users decide what files to process is more flexible and more consistent with built-in Unix commands.
|
Loops
|
A for loop repeats commands once for every thing in a list.
Every for loop needs a variable to refer to the thing it is currently operating on.
Use $name to expand a variable (i.e., get its value). ${name} can also be used.
Do not use spaces, quotes, or wildcard characters such as ‘*’ or ‘?’ in filenames, as it complicates variable expansion.
Give files consistent names that are easy to match with wildcard patterns to make it easy to select them for looping.
|
Additional Exercises
|
date prints the current date in a specified format.
Scripts can save the output of a command to a variable using $(command)
basename removes directories from a path to a file, leaving only the name
cut lets you select specific columns from files, with -d',' letting you select the column separator, and -f letting you select the columns you want.
|
Survey
|
|