Thursday, March 13, 2014

Linux and Unix fc and history command

Linux and Unix fc and history command


About fc and history
The fc command lists, edits, or re-executes commands previously entered to a shell.
The history command allows you to use words from previous command lines in the command line you are typing. This simplifies spelling corrections and the repetition of complicated commands or arguments.
Each shell (the Bourne shell, the Bourne Again Shell, the C Shell, the Korn Shell, etc.) has its own slight differences in how it handles, and allows access to, the command history. In general, the following commands will help you navigate and use your command history within your Linux/Unix shell.
The "history" command
history [-c] [-d offset] [n] or history -anrw [filename] or history -ps arg [arg...]
history displays or manipulate the history list with line numbers, prefixing each modified entry with a `*'. An argument of n lists only the last b entries.

Options

-cclear the history list by deleting all of the entries.
-doffsetdelete the history entry at offset OFFSET.
-aappend history lines from this session to the history file.
-nread all history lines not already read from the history file.
-rread the history file and append the contents to the history list.
-wwrite the current history to the history file and append them to the history list
-pperform history expansion on each ARG and display the result without storing it in the history list.
-sappend the ARGs to the history list as a single entry.
If FILENAME is given, it is used as the history file. Otherwise, if $HISTFILE has a value, that is used, else ~/.bash_history.
If the $HISTTIMEFORMAT variable is set and not null, its value is used as a formatstring for strftime to print the time stamp associated with each displayed history entry. No time stamps are printed otherwise.
The "fc" command
fc [-e ename] [-lnr] [first] [last] or fc -s [pat=rep] [command]
fc is used to list or edit and re-execute commands from the history list. FIRST and LAST can be numbers specifying the range, or FIRST can be a string, which means the most recent command beginning with that string.

Options

-e ENAMEselect which editor to use. Default is FCEDIT, then EDITOR, then vi
-llist lines instead of editing.
-nomit line numbers when listing.
-rreverse the order of the lines (newest listed first).
With the `fc -s [pat=rep ...] [command]' format, COMMAND is re-executed after the substitution OLD=NEW is performed.
A useful alias to use with this is r='fc -s', so that typing `r cc' runs the last command beginning with `cc' and typing `r' re-executes the last command.
Other History Commands and Shortcuts
!stringExecute the most recent command that begins with string.
!numExecute command that is number num in the command history.
!-numExecute the command was run num commands previous in the history.
!!Execute the previous (most recently-executed) command.
!?string[?]Execute the most recent command containing the string string. The trailing ?may be omitted if string represents the end of the command in question.
^string1^string2^Repeat the previous command executed, replacing string1 with string2. The previous command must contain string1.

Examples

fc -l
Lists the history of commands on the computer similar to the following:
 2 grep --help 3 bg 4 fg 5 pine 6 cd public_html 7 rm index.html 8 sz index.html 9 ls -laxo 10 chmod 755 index.htm 
fc -e - ls
Executes the most recently executed command that begins with the letters ls.
history
Typing history alone would give results similar to the following:
 2 grep --help 3 bg 4 fg 5 pine 6 cd public_html 7 rm index.html 8 sz index.html 9 ls -laxo 10 chmod 755 index.htm 
!ls
Executes the most recently executed command that begins with the letters ls.
!!
Would re-execute the most recently executed command.

No comments: