aum Posted July 29 Share Posted July 29 If you use the Linux command line often, you might forget which commands you've used recently. The history command is there to give your mind a jog. Fuse/Getty Images History does repeat itself -- at least with regards to the Linux command line interface (CLI). Although modern Linux distributions don't require users to ever open a terminal window (which is a good thing), if you ever decide to adopt Linux as a server operating system or just want to get even more flexibility and power out of the OS, the command line will be your friend. I've been using Linux for a very long time, so the terminal window is second nature to me. Because of that, I run a lot of commands. Sure, I could do everything I need with a GUI, but sometimes the terminal is just faster (for me). However, there are days when I find myself staring at the terminal window, scratching my head, and trying to remember the command I ran the previous day (or even hour). Thankfully, there are a number of ways to locate the command in question. I tend to use two methods. The first is to use the up and down arrow keys to scroll through the last few commands I ran. Although that method is my go-to, there are times when I have to scroll so far back in my history that it's not exactly the most efficient use of my time. There's another way to view those previously run commands. With a nod to irony, the method of viewing previously run commands is yet another command… history. According to the history man page (which can be viewed with the command man history), "The GNU History library is able to keep track of those lines, associate arbitrary data with each line, and utilize information from previous lines in composing new ones." A better description would be the history command prints out a line-by-line history of the commands you've previously run. By default, 1000 commands will be retained. You can even verify this with the command: echo $HISTSIZE The output should read simply: 1000 How to use the Linux history command What you'll need: The only thing you'll need for this is a running instance of Linux. And because the history command is found on all Linux distributions, it doesn't matter which one you choose. 1. Open a terminal window First, open your default terminal window or log into your Linux server. 2. Run the history command The history command is very simple to use. By default, the command reads the file ~/.bash_history and prints the contents in the terminal. To view this, issue the command: history You can then scroll through the output to find the command you need. 3. Change the number of commands to retain Let's say, however, that 1000 commands is too many for you to have to scan through. You can change the number of entries using the export command. Say, for instance, you want to change the history limit to 500. The commands for that would be: export HISTSIZE=500 export HISTFILESIZE=500 4. Ignore duplicates You can also configure history to not print duplicate commands. For that, issue the command: export HISTCONTROL=ignoredups 5. Limit the number of outputs Let's say you only want to view the last 10 commands. For that, use history like so: history 10 Or: history 25 Or: history 100 You get the idea. When you find the command you're looking for, highlight it, hit [Ctrl]+[Shift]+[C] to copy the command, and [Ctrl]+[Shift]+[V] to paste it back into the terminal. Simplify the search with grep Say you know you've run a particular command many times with different options. For example, you've used the nano editor on a configuration file but you can't remember where the file was. You can filter out only commands that included nano by piping the history output through grep, like so: history | grep nano The output will only include nano commands. Piping the output of history through grep. Screenshot by Jack Wallen/ZDNET And that's how Linux can help remind you of the command you're looking for, without you having to spend too much time or energy recalling what you did two or three days ago in the CLI. Source Karlston 1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.