Key Points
Introducing the Shell
- The shell functions as a program designed to execute other programs, primarily by interpreting and running commands.
- Throughout this lesson, we focus on Bash, which is commonly the default shell in many Unix systems.
- In Bash, programs are executed by typing commands at the command-line prompt.
- The shell offers several benefits, including a high ratio of actions per keystroke, the ability to automate repetitive tasks, and the capability to connect to and operate on networked machines.
- One of the main challenges in using the shell is understanding the specific commands required and the methods to execute them effectively.
Exploring File Systems and Path Navigation
- 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.
-
pwdprints the user’s current working directory. -
ls [path]prints a listing of a specific file or directory;lson its own lists the current working directory. -
cd [path]changes the current working directory. - Most commands take options that begin with a single
-. - Directory names in a path are separated with
/on Unix, but\on Windows. -
/on its own is the root directory of the whole file system. - An absolute path specifies a location from the root of the file system.
- A relative path specifies a location starting from the current location.
-
.on its own means ‘the current directory’;..means ‘the directory above the current one’.
File and Directory Management Techniques
-
cp [old] [new]duplicates a file from one location to another. -
mkdir [path]generates a new folder at the specified path. -
mv [old] [new]relocates or renames a file or folder. -
rm [path]permanently deletes a file from the filesystem. -
*is a wildcard that can represent any number of characters, making*.txtapplicable to all files with a.txtsuffix. -
?is a wildcard for a single character, meaning?.txtmatches files likea.txtbut not longer names likeany.txt. - The Control key’s usage might be noted in several ways, such as
Ctrl-X,Control-X, or^X. - There’s no undo option in the shell; deleted items are irretrievably lost.
- Filenames usually follow a
name.extensionformat, where the extension suggests the file type but doesn’t enforce it. - For various tasks, a text editor more advanced than Nano might be necessary.
Pipes and Filters
-
wcquantifies lines, words, and characters. -
catdisplays file contents. -
sortorganizes input. -
headshows the first 10 lines. -
tailreveals the last 10 lines. -
command > [file]directs output to a file, overwriting existing content. -
command >> [file]appends output to a file. -
[first] | [second]creates a pipeline, using the output of the first command as input for the second. - Combining simple, focused programs into pipelines epitomizes the shell’s most effective usage.
Automating Repetitive Tasks with Loops
- A
forloop executes commands for each item in a list. - Use a variable within a loop to refer to the current item.
- Expand a variable’s value with
$name, with${name}as an alternative syntax. - Avoid spaces, quotes, and wildcards like ’*’ or ‘?’ in filenames to simplify command usage.
- Consistent naming and wildcard patterns facilitate file selection for loops.
- Navigate command history with the up-arrow key and Ctrl+R for search.
- Repeat commands with
historyand![number]based on their history number.
Creating Shell Scripts
- Shell scripts save commands for later use, enhancing reusability.
- Executing
bash [filename]runs the saved commands in a file. -
$@denotes all command-line arguments passed to a shell script. -
$1,$2, and so on, refer to the first, second, and subsequent command-line arguments, respectively. - Enclose variables in quotes to handle values that may contain spaces.
- Allowing users to choose which files to process increases flexibility and aligns with the conventions of Unix commands.
Discovering Content
-
findsearches for files that match specific criteria. -
grepfilters lines within files based on patterns. -
--helpoffers command and program usage information within Bash. -
man [command]provides a command’s manual page. $([command])
replaces itself with the output of the enclosed command.