Bash History: Correct / Repeat The Last Command With a Substitution

I often type the wrong command as follows under Linux bash shell:

rsync -av dir1


cp -av file1*.txt file2*.txt /path/to/dest

I need to correct those command as follows:

rsync -av dir5
cp -av delta1*.txt delta2*.txt /path/to/dest

How do I replace dir1 with dir5 or file with delta and repeat the last command under bash shell?

The bash shell supports both history search and replace operations. The bash (and many other modern) shell provides access to the command history, the list of commands previously typed. The bash shell supports a history expansion feature found in other shell such as csh.

Syntax: Bash history search and replace the command args

The syntax is as follows for quick substitution and repeat the last command, replacing word1 with word2:






In this example, I’m trying to copy a file called youtube-demo-andriod-app-part1.avi to /backup directory
$ cp youtube-demo-andriod-app-part-102.avi /backup/
To repeat the last command with a substitution:
$ ^102^1002^
$ !!:s/102/1002
Sample outputs:

cp youtube-demo-andriod-app-part-1002.avi /backup/

Please note that this substitutes only applies to the first occurrence. For example:
$ cp -av file1*.txt file2*.txt /path/to/dest
Replace file1 and file2 with delta1 and delta2, enter:
Sample outputs:

cp -av delta1*.txt delta2*.txt /path/to/dest

You can also repeat the previous substitution with &:
cp -av file1*.txt file2*.txt /path/to/dest

Sample outputs:

cp -av delta-file1*.txt delta-file2*.txt /path/to/dest

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 7 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
7 comments… add one
  • Zak Aug 13, 2012 @ 14:25

    Very useful. Love it!

  • Marin Aug 16, 2012 @ 3:30

    I love this kind of tricks, where can I get more of them ?

    Thank you!

  • GeneRickyShaw Aug 24, 2012 @ 4:52

    Cool! Love it!

  • loadedmind Sep 13, 2012 @ 17:05

    Good stuff. Thanks for sharing!

    Don’t forget if it’s something as simple as replacing the first/last command, you can type CTRL+A to go to the beginning of the line and DEL the characters. CTRL+E goes to the end of the line. CTRL+W removes the last word. CTRL+U removes everything to the left of the cursor.

  • kingfong Oct 4, 2012 @ 22:54

    Why not just enter the following line into your .bash_profile?

    set -o vi

    Then you can edit history commands on the command line using vi (or emacs if you prefer).

    • asdf Nov 2, 2016 @ 23:25

      “Why not just enter the following line into your .bash_profile? set -o vi”

      …because when you’re on a team of hundreds working on tens of thousands of servers, you have to learn how to deal with systems without special environment.

  • passerby Feb 23, 2016 @ 2:13

    About the first method: ” ^WORD1^WORD2^ “,
    The last “^” seems unneeded?

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum