fc Command in Linux with Examples

As we all know that LINUX is command friendly and while working on LINUX, you may deal with very long commands that may include long paths or really difficult syntax, and imagine what if working with such commands you do a minor mistake which will require re-writing of the entire command synopsis and its arguments in order to execute it again successfully. Now, there are two solutions to this – either re-write the entire syntax or simply use fc command that allows you to edit and re-execute the commands previously entered to a shell without re-writing them again. This command line utility really comes to help while editing long commands.

So, the fc command is used to list, edit or re-execute the commands previously entered into an interactive shell.
Here’s the syntax of fc command :

 
//syntax of fc command

fc [-e ename] [-lnr] [first] [last]
           or
fc -s [pat=rep] [command]

where the -lnr refers to the options available for fc and -e ename refers to the editor you want to use for editing. first and last here is for specifying the range .

Using fc command

To easily understand the use of fc command and how does it work let’s take a simple example where you want to change your working directory to kt/kartik/thakral and by mistake while changing your directory you write karik instead of kartik, in this case you can simply use fc command to edit the previously entered path.

After you enter the incorrect directory path you’ll see something like this :

then you can just simply type fc command and after writing fc command your editor will open up so that you can now edit the previously entered command like shown below :

after editing your path you can exit your editor and after exiting fc command will re-execute the cd kt/kartik/thakral like shown below :

Using fc command with options

  • Using -l option : This option allows you to see some of the previously entered commands in your terminal i.e this lists the lines of previously entered commands instead of editing.

  • Using -n option : As shown above while using -l option fc command lists the lines along with the line numbers, but in case if you want to list the lines without line numbers you can simply use -n option with -l option as shown below :

  • Using -r option : This option can also be used with -l option to just reverse the order of lines i.e newest commands are listed first now as shown below :

  • Using first and last : Suppose you want to just edit a particular set of commands, in that case you can use first and last arguments as shown below :

  • Using -e option : The choice of editor to edit the demands totally depends on user and with the help of -e option user can choose editor of his choice, you just have to pass the name of editor with -e option as shown below :

Applications of fc command

  • fc command is the best way to edit the previously entered commands in the case of a minor mistake without re-writing the entire command syntax and argument again.
  • It can also be used to list the previously entered commands in the terminal which can be helpful in case you are working with some new commands.
  • fc command in a way lets you know the command history.


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.