How to use the fc command under Linux
In the process of using Linux, if you accidentally make a small mistake when dealing with a very long command containing a complicated syntax, you need to re-enter the entire command and parameters until the command is executed successfully. Another option is to use the fc command to edit and rerun the previous command without having to re-enter the entire command and parameters.
Introduction to fc
The fc command is the abbreviation of fix command. It is a built-in command. It can list, edit, and re-execute the commands recently entered in the interactive shell. You can use the designated editor to edit and run the recently entered commands without the need Re-enter the entire command.
The syntax of the fc command is as follows:
[[email protected] ~]# fc --hfc: usage: fc [-e ename] [-lnr] [first] [last] or fc -s [pat=rep] [command]
-e option
Use the specified editor to edit the default is the vi editor, in the following example ls /home/tt
command in vi editor, change the ls cd, save and exit in time will execute cd /home/tt
the command, the specific results are as follows:
[[email protected] ~]# fc -l657 ls /home/tt658 fc -l[[email protected] ~]# fc -e vi 657ls /home/tt"/tmp/bash-fc-27929723442" 1L, 12C writtencd /home/tt[[email protected] tt]# pwd/home/tt[[email protected] tt]#
-l option
List recent history commands, the default is 16
Without parameters, the last 16 commands are displayed by default
[[email protected] ~]# fc -l1 date2 cd ~3 fc -l4 fc -lr5 ls /home/tt6 chage -l
Display the most recent specified number of lines, the following command displays the most recent 3 lines
[[email protected] ~]# fc -l -34 fc -lr5 ls /home/tt6 chage -l[[email protected] ~]#
Specify the starting line number, display the commands from the specified line number to the end line, the following command displays the commands from the line number 530 to the end line
[[email protected] wl]# fc -l 530530 date531 chage -l tt532 chage -h533 chage -l root534 fc -l
Specify the start line number and the end line number, display the command of the specified line number range, the following command displays the line number from 531-534
[[email protected] wl]# fc -l 531 534531 chage -l tt532 chage -h533 chage -l root534 fc -l
-r option
Display history commands in reverse order, which are generally used with the -l parameter. The following is an example of using the -r option. The fc -l option displays the commands from 1 to 2 lines. The number of command lines displayed by executing fc -lr should include the previous fc -l command, so the result will be one line more than the previous one. The number of lines after performing the reverse order is from 3 to 1. The specific results are as follows:
[[email protected] ~]# fc -l1 date2 cd ~[[email protected] ~]# fc -lr3 fc -l2 cd ~1 date[[email protected] ~]#
-n option
The line number is not displayed when displaying the history command. It is generally used with the -l parameter. The line number is not displayed in the following example
[[email protected] tt]# fc -l1 date2 chage -l tt[[email protected] tt]# fc -ln date chage -l tt fc -l[[email protected] tt]#
-s option
-s [pat = rep] [command ] to replace rep pat commands into the command and executes the following examples ls /home/tt
replace cd /home/tt
command, after performing successful, the current directory into a /home/tt
command execution results are as follows:
[[email protected] tt]# fc -l1 date2 ls /home/tt[[email protected] tt]# fc -s cd=ls 2ls /home/tt[[email protected] tt]# pwd/home/tt[[email protected] tt]#
Tips
A useful trick, use fc -s 'pre' may be run automatically at a recent command beginning with 'pre', enter the fc -s
command can execute this command again.
[[email protected] ~]# fc -l1 ls /home/tt2 chage -l3 date
The above is a history list of commands, execute fc -s 'da'
the command will execute the most recent command to 'da' at the beginning of the following is the specific implementation of the results
[[email protected] ~]# fc -s 'da'dateMon Jun 29 20:26:33 CST 2020[[email protected] ~]# fc -s 'l'ls /home/tt[[email protected] ~]# fc -sls /home/tt
From the above results of view, execution fc -s 'da'
may execute a command to the nearest 'da' beginning, i.e. date command.
Execute fc -s 'l'
command to execute a recent command 'l' at the beginning, that is ls /home/tt
the command, followed by the execution fc -s
command, will once again execute ls /home/tt
the command
0 Comments