The fc executable allows us to re-edit and/or re-execute previous commands. If we just run it without any arguments, we will see the last command executed in our default editor (e.g. vi). We can also list previous executed commands up to the limit set by the environment variable $HISTSIZE with the flag -l:
# fc -l -6 → last 6 commands executed
242 view chrony
243 cd ..
244 cat logrotate.conf
245 cd /etc/cron.d
246 ll
247 cat 0hourly
.
# fc -ln -7 → last 7 commands executed without line number
view chrony
cd ..
cat logrotate.conf
cd /etc/cron.d
ll
cat 0hourly
fc -l -6
Once we have spotted the right command, we can edit it in 2 ways:
# fc 245 → edits the command 245th in history
# fc -4 → edits the 4th last command
And we can re-execute a command without editing in various ways:
# fc -s -> re-executes the last command
# fc -s 245 -> re-executes the 245th command
# !245 -> re-executes the 245th command
# fc -s cat -> re-executes the most recently executed command starting with “cat”
# !cat -> re-executes the most recently executed command starting with “cat”
We can also use the more intuitively named history to do the same a with fc -l: