Now is the time to make use of that number. I’ve already shown you how to look through your history and find a specific command in your history complete with the command number. It’s a double exclamation point simply to make it quick and easy to type in. The second exclamation point tells BASH that it’s the last command that you’d like to run again. The first exclamation point tells BASH that you are running a previous commands. If you’d like to run the previous command again quickly, you can simply run “ !!“. Searching through the history is great and all, but I’d rather not have to copy and paste found commands. Now I can bask in the glory of my ingenious command and use it as the inspiration for other inspired words of command line magic. Since I don’t think that I have the word “ link” in many of my commands, I pick it as the target to search for and run the following: $ history | grep linkĤ12 find ~/wordpress -type f | grep \.php$ | grep link | sort -r I was quite proud of that command, but I can’t remember how I did it. php” inside the ~/wordpress folder it then filters for just matches that contain the text “ link” and then reverses the output. If you remember that you ran a really cool command and can’t quite remember how you did it, combining history with grep provides an easy way to find that command with just a piece of the command.įor example, let’s say that I ran a command that listed all the files that end in “. So, “ history 1” will just show you “ history 1“. Notice that the last command is the same as the command you ran to produce it. For example, if you want to see the previous ten commands, you can run the following: $ history 10 You can also have the history command produce just the last certain number of commands by supplying a number. I’ll tell you how to use these numbers below. Each command will be preceded with a number that represents that command’s command number. Listing and Searching Command HistoryīASH provides the history command that when executed with no options will list the entire command history. This method is very good for quickly running commands that you have to execute repeatedly without many other commands between. You can simply press Enter to execute the command again, or you can modify the command as needed before executing it. Once you’ve found the desired command, you can treat it as if you’ve typed that command in the command line. To scroll back down the list, you can use the down arrow key. Using the up key, you can scroll through previous commands one command at a time. The easy way to get started with your command history is to simply navigate with the up and down arrow keys. Furthermore, if you force a Terminal session to quit, the command history will not be updated. If you have two sessions running concurrently, they cannot cross-reference the newly-executed commands run in the other session. It’s important to keep in mind that commands executed for a session are only written when you exit. When you load a new session, this file is read and those commands along with the new commands you execute for that session become your new command history. When you close a Terminal session, BASH writes all the commands you’ve run for that session to your ~/.bash_history file. Since most people don’t modify which shell they access through Terminal, I only point this out to inform those who have changed their shell that their experiences may differ depending on the shell used. The instructions that I give here are specific to BASH and may or may not work in other shells. Introduction to BASH Command Line HistoryīASH (Bourne Again SHell) is the default command line shell that is used in Ubuntu, and most other distros for that matter. Today, I’m going to teach you how to do exactly this. If only there were a way to pull up commands that you’ve already run to run again as is or to quickly modify. You’re starting to learn how to use the command line, but it’s annoying to always have to type in similar commands over and over. So, make sure to check out Mastering the Command Line if you want to know how to become a command line power user.īack to today’s topic. I’ve gone through my older posts on this topic and tagged them as well. Since I’m probably going to end up with a lot of content under this topic, I’ve decided to create a dedicated tag: Mastering the Command Line. My focus is specifically for Ubuntu users, but the information and techniques can be used for any Linux distro. I’ve had a lot of fun recently posting about how to do stuff on the command line in Linux.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |