Grep It
August 25, 2007 | In Linux | No CommentsFind string in current & sub directories
grep -n -r "getHead()" */* | more
history — a bash command
August 20, 2007 | In Linux | 4 CommentsIn ubuntu, look at your home dir’s .bashrc file to control history
GC May 10 2009
# don't put duplicate lines in the history. See bash(1) for more options
# export HISTCONTROL=ignoredups
export HISTCONTROL=ignoreboth
# History - ignore duplicates with &, ls bg, exit
# http://www.talug.org/events/20030709/cmdline_history.html
export HISTIGNORE="&:ls:[bf]g:exit"
# number of lines of History to save
export HISTSIZE=5000
Quick way to reload your bashrc file without having to invoke a new shell:
[user@localhost]# source ~/.bashrc
man bash:
When bash is invoked as an interactive login shell, or as a non-interactive shell with the –login option, it first reads and
executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile,
~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable.
The –noprofile option may be used when the shell is started to inhibit this behavior.When a login shell exits, bash reads and executes commands from the file ~/.bash_logout, if it exists.
When an interactive shell that is not a login shell is started, bash reads and executes commands from /etc/bash.bashrc and
~/.bashrc, if these files exist. This may be inhibited by using the –norc option. The –rcfile file option will force bash
to read and execute commands from file instead of /etc/bash.bashrc and ~/.bashrc.
Using Bash’s History Effectively
I will attempt here to focus on only one feature of Bash, its command history. There are other good documents on the other features of Bash. If you’re interested in anything other than the command history/recall, then this document really isn’t going to fulfill your needs.
Okay, so let’s get into it!
What is Bash? “Descended from the Bourne Shell, Bash is a GNU product, the “Bourne Again SHell.” It’s the standard command line interface on most Linux machines. It excels at interactivity, supporting command line editing, completion, and recall.” –Bash Prompt HOWTO
So, Bash is GNU software and all that that implies (GPL, free, reliable, etc.). It’s the de facto standard interface that you use to interact with the system from a command line. Bash is also highly configurable. We’re going to take advantage of that later…
The last feature mentioned as one of the major selling points of Bash is “recall”. This is what I’ve been calling the command history. When typing commands at the command line interface (CLI), one can sometimes come up with rather long commands.
While completion (feature #2) alleviates much of the pain associated with entering long commands and deserves an entire presentation unto itself (stay tuned), completion also adds to the problem in a way, by making longer commands more acceptable. If we didn’t have completion, we might not name our files “really_big_file.txt.old.do_not_delete_yet”.
There’s also the advent of long options, prevalent in the GNU world. So, instead of netstat -rn you might have netstat –route –numeric.
Then, of course, there’s always the endless stringing together of commands with pipes (|) to make mini programs (my personal favorite).
So, we have this problem where command lines can get very long. Well, it’s one thing to build up long commands once. The frustration comes in having to build them up repeatedly. The obvious solution is to store commands entered on the command line and provide access to those stored commands so that they can be used again with relative ease.
That is Bash’s history/recall function in a nutshell. You just press the up arrow to scroll back through your command history. See the command you want and press Enter/Return to execute it again.
Now if that were all there was to command history, it would be pretty weak. Naturally, one might want to improve upon a previous command and Bash uses GNU Readline to provide full editing capabilities at the command-line. So, say you have a command like this:
ls -latrh /home/jason/Documents | grep work
Then you realize that you meant to do a case-insensitive grep, so you hit the up arrow and recall the command. Your cursor is sitting at the end of the line. All you have to do is arrow back four times (or just type meta-b) and type “-i “:
ls -latrh /home/jason/Documents | grep -i work
A total of 5 keystrokes, not including the added text and pressing Enter, both of which you would’ve had to do anyway. Using Readline’s meta-b shortcut saves another two keystrokes… truly efficient.
Okay, well say you didn’t just run the sought-after command. You know you’ve used it within the past few days, but you don’t want to scroll through what could be hundreds of commands to find it. Well, there are a couple ways to do this, depending on how much you can remember about the history yourself…
* special knowledge
o If you know you haven’t executed any commands with the same starting letter sequence since then, you can just use the built-in Bash history expansion command ! followed by the first few letters of the command.
o The unique string doesn’t have to be at the start of the command. You can use the more flexible built-in Bash history expansion command !? followed by a unique string appearing anywhere in the command.
o Both of these commands will immediately recall and execute the most recent matching command. Thus, it is usually not a good idea to use these methods with destructive commands like rm!
* some knowledge
o If you aren’t positively sure of what would happen if you were to use the ! or !? method, or if you need to search for something more unique in the command than the first few letters can provide, then you could use the history search feature.
o Before you begin typing your command, type ctrl-r. This will put you into history search mode (actually, reverse incremental history search mode).
o Now when you begin typing, the most recent command matching what you’ve typed so far will appear on the line with a cursor at the start of the match. (Try playing around with this feature; there are a few interesting behaviors in there.)
o When you’ve found what you’re looking for, you have a couple options. Just pressing Enter will immediately recall and execute the command. ctrl-j or Escape will retrieve the command, but allow you to continue editing. If you can’t find what you’re looking for or if you just change your mind, hit ctrl-g or ctrl-c to cancel.
* vague memory
o If you really are uncertain of the history or if you know you could be searching back through many similar commands for one of particular interest, then you can use this more brute-force method.
o Type the following command to get a list of all related commands with their history numbers:
history | grep -i “
o Once you’ve found the command you want, you can execute it specifically by its number using the following built-in history expansion command:
!
o Type the following command to get a list of all related commands with their history numbers:
history | grep -i "search string"
o Once you've found the command you want, you can execute it specifically by its number using the following built-in history expansion command:
! history number
Remember: Once you’ve used one of these methods to recall and execute a command, that command is now the most recent command in your history. You can now just press the up arrow once to retrieve it again.
Okay, so now you can recall commands with relative ease. But we really haven’t done anything special yet. We’re just using the default behaviors and commands provided in Bash. The trick comes in configuring what gets put into your history!
A little-known trick (I’ve only seen one distro ship with this turned on by default) is that you can filter what gets stored in the command history. This is done merely by setting an environment variable, $HISTIGNORE.
Just set the HISTIGNORE variable to a colon-delimited list of patterns which should be excluded. There’s also the special pattern ‘&’, which suppresses duplicate entries (very useful!).
Here’s an example that suppresses duplicate commands, the simple invocation of ‘ls’ without any arguments, and the shell built-ins bg, fg, and exit:
export HISTIGNORE=”&:ls:[bf]g:exit”
Try it out. I think you’ll like it.
Here’s a neat trick, submitted by Robert Cymbala. If you include the expression “[ \t]*” in the HISTIGNORE string, you can suppress history recording at will for any given command just by starting with a space!
Another optional feature of Bash history filtering that isn’t always enabled by default in distributions is cmdhist. This determines whether multi-line commands are stored in the history as a single command (on) or not (off; default). This makes it much easier to go back and edit multi-line commands.
To enable this feature, you would type
shopt -s cmdhist
To disable this feature, you would type
shopt -u cmdhist
As you may have guessed, there are many options for Bash that can be toggled using the ’shopt’ built-in. They are documented about halfway down this page.
There exist many more interesting things that can be done with Bash’s command history. I refer those interested in further details to the official Bash Reference at the GNU website. For more on the various special shell variables used by Bash, see this page and (search for “HIST”).
I hope this has been an approachable and useful tour of Bash’s command history feature.
Jason Bechtel
original at source: http://www.talug.org/events/20030709/cmdline_history.html
********************************************************
Another Take
history
history [n]
history -c
history -d offset
history [-anrw] [filename]
history -ps arg
With no options, display the history list with line numbers. Lines prefixed with a `*’ have been modified. An argument of n lists only the last n lines. Options, if supplied, have the following meanings:
-c
Clear the history list. This may be combined with the other options to replace the history list completely.
-d offset
Delete the history entry at position offset. offset should be specified as it appears when the history is displayed.
-a
Append the new history lines (history lines entered since the beginning of the current Bash session) to the history file.
-n
Append the history lines not already read from the history file to the current history list. These are lines appended to the history file since the beginning of the current Bash session.
-r
Read the current history file and append its contents to the history list.
-w
Write out the current history to the history file.
-p
Perform history substitution on the args and display the result on the standard output, without storing the results in the history list.
-s
The args are added to the end of the history list as a single entry.
When any of the `-w’, `-r’, `-a’, or `-n’ options is used, if filename is given, then it is used as the history file. If not, then the value of the HISTFILE variable is used.
from: http://www.gnu.org/software/bash/manual/bashref.html#SEC114
Easily recall previous commands
1. Up and Down cursor keys.
2. “speed search” – Ctrl-R then first few letters of command. To cycle through all matching comands, Type Ctrl-R repeatedly
"speed search" - Ctrl-R then first few letters of command. To cycle through all matching comands, Type Ctrl-R repeatedly
man uname
August 16, 2007 | In Linux | Comments OffUNAME(1) User Commands UNAME(1)
NAME
uname – print system information
SYNOPSIS
uname [OPTION]…
DESCRIPTION
Print certain system information. With no OPTION, same as -s.
-a, –all
print all information, in the following order:
-s, –kernel-name
print the kernel name
-n, –nodename
print the network node hostname
-r, –kernel-release
print the kernel release
-v, –kernel-version
print the kernel version
-m, –machine
print the machine hardware name
-p, –processor
print the processor type
-i, –hardware-platform
print the hardware platform
-o, –operating-system
-o, –operating-system
print the operating system
–help display this help and exit
–version
output version information and exit
AUTHOR
Written by David MacKenzie.
REPORTING BUGS
Report bugs to
COPYRIGHT
Copyright © 2004 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY
To Get Redhat Version
cat /etc/redhat-release
Powered by WordPress
RSS Feed - Syndicate this Site
and comments feed



