Video summary
The Linux shell maintains a record of all commands entered by the user in a history list, which can be viewed using the `history` command. This feature becomes particularly useful when dealing with long directory paths or complex file operations that are tedious to type repeatedly. For instance, after navigating through several directories and copying files, a user might want to return to a specific location without retyping the entire path. While one could manually scroll through the history using arrow keys, this process is inefficient if many commands have been executed. Instead, piping the history list to `grep` allows the user to quickly search for specific keywords, such as "graph," and identify the exact command number needed to revisit a previous state.
History expansion provides a powerful mechanism to recall and re-execute these past commands by prefixing them with an exclamation point followed by the command's number. Beyond simply retrieving commands by index, the feature supports dynamic manipulation of command history. Users can recall the immediately preceding command using two consecutive exclamation points, effectively re-running the last action with updated timestamps or variables. Additionally, the system allows for pattern-based retrieval; entering a specific string recalls the most recent command starting with that text, while appending a question mark enables searching for commands containing that string anywhere within them. This flexibility ensures that users can efficiently access previous actions without needing to remember exact syntax or sequence numbers.
The capabilities of history expansion extend further into command modification and substitution, offering significant time-saving benefits for repetitive tasks. A user can recall a complex command involving file backups and renaming, then use special modifiers like the colon to introduce global substitutions across the entire line. By specifying delimiters, one can replace specific strings within the recalled command, such as changing directory names from "chapter01" to "chapter02" or incrementing version numbers sequentially. This allows for executing a series of similar operations with minimal effort, as the shell automatically updates and runs the modified command. Such advanced features demonstrate how history expansion transforms the terminal into an interactive environment where past actions serve as building blocks for future workflows.
In conclusion, mastering history expansion and searching capabilities empowers users to streamline their shell interactions and reduce manual typing errors. While the transcript notes that other search methods exist and would require a dedicated video to cover in depth, the combination of the `history` command with expansion features is sufficient for most daily tasks. By leveraging these tools, users can effortlessly reuse previous commands, modify them on the fly, and automate repetitive processes like batch file renaming or directory management. Ultimately, understanding how to navigate and manipulate the command history list turns the shell into a highly efficient workspace that adapts to the user's workflow rather than hindering it with redundant input.
Read the full video transcript
As you type shell commands, the shell
stores them in a history list.
You can see this history list with the
history command.
And here we'll pipe it to less.
And these are some of the commands that
we've done.
Here's how we use history to make our
lives easier.
For example, I'll change to a directory
with a long path name
slashservewww/ht
docs/graph
example.
And then I'll list it.
And then I'm going to copy one of the
files to my current directory.
Let's copy nonzero.png png
to tilda slashhde
demo
and then return to the h demo directory
to continue my work.
Sometime later, after having typed lots
of commands, I want to go back to that
graph example directory,
rather than type the cd command again. I
could use the up arrow key to move back
through my history list to find it.
But if I had done a lot of commands,
that might take a lot of time.
Let me use down arrow to return to my
empty prompt.
Instead, I'll find that command in the
history by piping the history command
to GP and look for the word graph.
And it turns out there are several
occurrences of graph, but the one I'm
looking for is this one, which was
command number 205.
Now I'll make use of a feature called
history expansion.
You use history expansion by starting
with an exclamation point and then
following it with a history list number
of the command you want to redo.
In this case, command 205.
And history expansion
brings back the command, shows it to me,
does it. And now I'm in the directory
that I want.
I can list my files again. And I'm going
to copy zerobase.png
to my
directory h demo. And then go back to h
demo.
History expansion has quite a few
capabilities in addition to retrieving
commands by their history list number.
Let me do a couple of extra commands
here just so I'll have some extra things
in the history list. So, I go
placeholder
and let's do an ls slash user and
slashvar.
And then let me do the date command.
I use two exclamation points in a row to
bring back the last command that I did.
And you can see that it re-executed it
because I have a different time. It was
8 seconds later.
If I want to redo a command that starts
with a particular string, I can enter
that string. For example, I want history
expansion to bring back the last command
that started with echo,
which was my placeholder.
If I want to redo a command that
contains a particular string, I use
exclamation point question mark.
Let's look for anything that had EVC-com
in it. And although you don't have to,
you can put an ending delimiter as well.
And it brings back a command that I used
to transfer files.
I pressed control C to get out of it.
Those are the uses of history expansion
that are covered in the book. Here's a
feature that isn't in the book.
Let me clear
and get a view of my current directory.
I want to create a backup directory
and then I want to copy chapter 1
recursively to that backup directory and
change its name at the same time. So I'm
going to do a copy recursively.
the directory chapter01 and all of its
contents will go into the backup
directory under the name
backupchapter01.
And let's take a look at the tree and
see what it looks like now.
Perfect. That's exactly what I wanted.
Now I want to do the same thing for
chapters two and three without having to
retype this entire command.
and history expansion will let me do
that.
I'm going to bring back the last command
that had the word chapter in it.
I'm using that question mark at the end
as a delimiter.
Then I'm going to use a colon which
introduces a modifier.
and globally on the whole line. I'm
going to substitute
and use a slash as a delimiter.
All occurrences of 01
with 02.
And there's my closing delimiter for the
substitution.
The shell will then display and execute
the edited command.
It's replaced this command here
with a command where all the 01's are
02s.
I can also do a substitution
using the two exclamation points in a
row. I'll I'll bring back the last
command.
Use a colon to introduce a modifier and
globally substitute all the 02s in the
last command with 03s.
And this command gets executed.
And there's our completed backup.
So I've used history expansion with
substitution
to save some time and effort.
In addition to history expansion, the
book discusses other ways of searching
the history list.
Covering those in detail would require
an entire new video, so I won't cover
them here. But the history command in
combination with history expansion is
enough to let you save time and effort
by reusing previous shell commands.