I often type the wrong command as follows under Linux bash shell:
rsync -av dir1 user@server1.cyberciti.biz:/path/to/dir1
OR
cp -av file1*.txt file2*.txt /path/to/dest
I need to correct those command as follows:
rsync -av dir5 user@server1.cyberciti.biz:/path/to/dir1
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?
| Tutorial details | |
|---|---|
| Difficulty | Easy |
| Root privileges | No |
| Requirements | 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:
^WORD1^WORD2^
OR
!!:s/WORD1/WORD2
OR
!!:gs/WORD1/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^
OR
$ !!: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:
!!:gs/file/delta
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
!!:gs/file/delta-&/
Sample outputs:
cp -av delta-file1*.txt delta-file2*.txt /path/to/dest
You should follow me on twitter here or grab rss feed to keep track of new changes.
Featured Articles:
- 30 Handy Bash Shell Aliases For Linux / Unix / Mac OS X
- Top 30 Nmap Command Examples For Sys/Network Admins
- 25 PHP Security Best Practices For Sys Admins
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- Linux: 20 Iptables Examples For New SysAdmins
- Top 20 OpenSSH Server Best Security Practices
- Top 20 Nginx WebServer Best Security Practices
- 20 Examples: Make Sure Unix / Linux Configuration Files Are Free From Syntax Errors
- 15 Greatest Open Source Terminal Applications Of 2012

- My 10 UNIX Command Line Mistakes
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- The Novice Guide To Buying A Linux Laptop














{ 5 comments… read them below or add one }
Very useful. Love it!
I love this kind of tricks, where can I get more of them ?
Thank you!
Cool! Love it!
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.
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).