Linux – Learning the Shell (Part 2)

As mentioned in my previous post, I am working through the online version of The Linux Command Line by William Shotts. My goal is to internalize this material before I begin the LPIC-1 Study Guide. I completed sections 1 through 4 of the chapter Learning the Shell the other day. Today I picked up where I left off and completed sections 5 through 8.

I chuckled when I saw that last line. Linux users do have a sense of geek humor combined with a healthy pessimism against Windows.

Delete Windows 2K Legacy OS.

What are you trying to say, Mr. Shotts?

Sections 1 – 4 were mostly a refresher for me. These next sections introduced a few commands there were new to me. I also want to note that I am not simply reading the text. Rather, when a new command or set of commands are introduced, I am running them in the terminal in my Ubuntu virtual machine. In other words, I’m performing hands-on exercises to help solidify the lessons.

Here are my notes for Learning the Shell: Sections 5–8:

5. Manipulating Files

Linux treats everything as a file, including directories and devices.

File manipulation commands:

# Creates an empty file or updates a file's timestamp.
touch
# Creates a new directory. 
mkdir
# Removes an empty directory.
rmdir
# Removes a directory and its contents recursively.
rm -r
# Moves or renames files and directories.
mv
# Copies directories recursively.
cp -r

File naming tips:

Avoid spaces; use underscores or hyphens.

File names are case-sensitive.

6. Working with Commands

Commands follow a basic structure:
command -options arguments.

Viewing command behavior:

# Shows how a command is interpreted (e.g., builtin, alias).
type
# Displays the path to an executable.
which
# Provides help for shell built-ins. 
help
# Displays usage info for many commands.
--help
# Opens the manual page.
man

Command history:

# Lists previously executed commands.
history
# Repeats the last command.
!!
# Repeats command number n.
!n
# Repeats last command starting with string.
!string

7. I/O Redirection

# Redirecting output and input.
>
# Redirects stdout to a file (overwrites).
>>
# Redirects stdin from a file.
<

Combining commands:

# Sends stdout of one command to stdin of another.
|

Examples:

# Saves output of ls to a file.
ls > files.txt
# Searches for "error" in file.txt.
cat file.txt | grep "error"

8. Expansion.

Quoting and escaping:

# Preserves spaces and allows variable expansion.
" " 
# Preserves literal value (no expansion).
' '
# Escapes a single character.
\

Globbing:

# Matches any string.
*
# Matches any single character.
?
# Matches any one of the characters a, b, or c.
[abc]

Brace expansion:
{a,b,c} expands to a b c.

Tilde expansion:
~ refers to the home directory.

Variable expansion:
$VAR expands to the value of VAR.

There are only two more sections in the Learning the Shell chapter, and then I will be able to move on to Writing Shell Scripts (which has 15 sections).

I feel that working through this material will be beneficial as it will build confidence and familiarity with the command line interface (CLI) so that when I transition to the LPIC-1 specific material, I won’t be having to learn CLI at the same time. I can just focus on the concepts related to the exam objectives.