If you experience any difficulty in accessing content on our website, please contact us at 1-866-333-8917 or email us at support@chicagovps.net and we will make every effort to assist you.

By
October 16, 2024

8 Simple Techniques to Reuse Commands Efficiently in Linux

 

Repeating commands in the Linux command line is a straightforward task, and there are several methods to achieve it. This article outlines eight ways to reexecute commands on Linux, both unchanged and with modifications.

The simplest method to repeat a command is by using the !! syntax. This allows you to run the exact same command again without needing to input anything extra. Below are a couple of examples:

If you wish to repeat the last command but want to make a modification, you can indicate the desired change by surrounding the original text and the new text with ^ symbols, as demonstrated in the example below.

To revisit an earlier command within your command history, utilize the history command, which typically keeps a record of the last 1,000 commands executed. From there, you can type ! followed by the corresponding number of the command you wish to run again. Here’s an illustration:

Another way to execute a previous command is by navigating through your command history: Simply press the up arrow key until you reach the desired command, and then hit the return key to execute it again.

Once you’ve accessed a prior command using the up arrow key, you can modify it before running it anew. You can go back with the backspace key and then input the new segment of the command.

To quickly move to the start of the command, use ^a, or to reach the end, use ^e. For instance, if I initially neglected to include the echo command, I could have quickly shifted to the command’s start with ^a and added the missing command.

You may also navigate left and right using the left and right arrow keys. Retrieve the command with the up arrow key, then adjust your cursor left and right as necessary. Once you’ve made your changes, just press the return key to execute the modified command.

The shortcut ^a allows you to move the cursor to the start of the command, while ^e takes the cursor to the end of the command.

If you wish to reuse the last argument from the previous command, refer to it using !$ in your next command. For example:

To search for a command in your history that you’d like to execute again, hold down the Ctrl key and press r. This will prompt you to enter the search term. The most recent command that includes that string will be executed, and you can include multiple words in your search. As you type, the original command will be displayed on the right.

For commands you anticipate using often, consider creating an alias. You can make an alias with a command similar to the one below, though it will only persist until you log out. To keep it available every time you log in, add it to your .bashrc file. Here are a few examples:

Using aliases can not only help you save time but also eliminate the risk of making typos.

If you want to find out how many commands your history buffer can keep, execute the command below.

This setting can be modified by adding a specific command to your .bashrc file. The default setting can be found in /etc/profile.

Utilizing commands from your command history is a great time-saver, particularly when executing complex or frequently repeated commands.


ChicagoVPS is your gateway to unparalleled hosting solutions. Our state-of-the-art datacenters and powerful network ensures lightning-fast speeds and uninterrupted connectivity for your websites and applications. Whether you’re a startup looking for scalable resources or an enterprise in need of enterprise-grade hosting, our range of plans and customizable solutions guarantee a perfect fit. Trust in ChicagoVPS to deliver excellence, combining unmatched reliability and top-tier support.

For Inquiries or to receive a personalized quote, please reach out to us through our contact form here or email us at sales@chicagovps.net.

Subscribe Email

Top