- "기밀 VM의 빈틈을 메운다" 마이크로소프트의 오픈소스 파라바이저 '오픈HCL'란?
- The best early Black Friday AirPods deals: Shop early deals
- The 19 best Black Friday headphone deals 2024: Early sales live now
- I tested the iPad Mini 7 for a week, and its the ultraportable tablet to beat at $100 off
- The best Black Friday deals 2024: Early sales live now
Hiding from history on Linux
Linux shells like bash have a convenient way of remembering commands that you type, making it easy to run them again without having to retype them. Just use the history command (which is a bash built-in) and then use an exclamation point followed by the number shown in front of the command in the history command output that you want to rerun. Alternatively, you can back up to that command by pressing the up arrow key as many times as needed to reach that command and then press return. Don’t forget, though, that you can also set up commands you are likely to use often as aliases by adding a line like this to your ~/.bashrc file so that you don’t need to search for them in your command history. Here’s an example:
$ alias users="who | awk '{print $1}'" $ users myacct shs shs $ alias uusers="who | awk '{print $1}' | uniq"
The aliases above will display logged in users. The second removes duplicates. Add the commands to your ~/.bashrc file to have them set up each time you log in.
Making use of the history command can make it easier to reuse commands and avoid typos. However, there are ways to get Linux to ignore some of the commands that you type so that your history buffer doesn’t fill up with commands you don’t want it to remember – especially if those commands would take up a lot of room in your history buffer.
Command history can also help with troubleshooting a problem as you can get a listing of the commands that were run before the problem appeared.
How much to remember
The $HISTSIZE setting controls how many commands will be remembered. If you only want the most recent 100 lines to be remembered in your history buffer, you could run a command like this:
$ export HISTSIZE=100
In general, $HISTSIZE defaults to 1,000 commands. Check it by running a command like this one:
$ echo $HISTSIZE 1000
Change the setting in your ~/.bashrc file if you want history to remember more or fewer commands.
Ignoring specific commands
You can keep certain commands from being saved in your history buffer by adding them to the $HISTIGNORE variable. In the setting shown below, the pwd, date, history and clear commands will be ignored since these commands are unlikely to be rerun using command history.
$ export HISTIGNORE='pwd:date:history:clear' $ pwd /home/myacct $ history $ history | tail -1 86 pwd 87 history | tail -1
Note, however, that the history | tail -1 command shown above is captured, but not the history command when entered by itself.
Ignoring duplicate commands
If you type a command some number of times in a row and your $HISTCONTROL setting contains the string “ignoredups” (i.e., ignore duplicates), you’ll only see the command once for each repetitive sequence.
$ echo hello hello $ pwd /home/myacct $ pwd /home/myacct $ who | wc -l 4 $ pwd $ history | tail -5 79 echo hello 80 pwd <== was run twice, but is shown once 81 who | wc -l 82 pwd 83 history | tail -6
Ignoring commands that are entered with a preceding space
You can also elect to have your history buffer fail to remember any command that you enter when you run the command after pressing the space key – for example, if you type “ date” instead of “date”. This allows you to have your command history ignore any command without having to set up each command separately in your $HISTIGNORE setting.
In fact, if you want your command history to omit both commands that start with a space along with duplicate commands entered sequentially, either of these settings will do the same thing. Add them to your ~/.bashrc file to make them permanent.
$ export HISTCONTROL="ignoredups:ignorespace" $ export HISTCONTROL="ignoreboth"
To be clear, the “ignoreboth” setting means to ignore both duplicate commands when entered sequentially and commands that are entered with a preceding space.
Your history file
The command history file is identified by the $HISTFILE setting, but is rarely anything but .bash_history. Note that commands you just entered will not yet have been added to this file.
$ echo $HISTFILE /home/shs/.bash_history
The command below would be added to my history buffer, but note that it doesn’t show up when I ask to see the bottom of my .bash_history file.
$ echo byebye for now byebye for now $ tail -1 ~/.bash_history tail -2 ~/.bash_history
But on next login, I will see this:
$ tail -1 ~/.bash_history echo byebye for now
Wrap-up
Deciding what commands you want remembered in your command history file and which you want ignored can improve the usefulness of your command history – especially if you don’t want to scan through hundreds of remembered commands to find the handful that you want to return. I hope that some part of this post has left you with some good ideas about making your command history more valuable.
Copyright © 2023 IDG Communications, Inc.