3

Is there a way to change the previous command in linux? I'm copying a bunch of files using

cp path/to/source1 path/to/target2

and I want to change it to

cp path/to/source2 path/to/target2

so I want to replace the 1 with the 2

I know I can put this inside a loop but I need to do this after checking something in my notebook.

Rich
  • 5,603
  • 9
  • 39
  • 61
Yotam
  • 10,295
  • 30
  • 88
  • 128

4 Answers4

8
!!:gs/1/2/

Here are some more examples: http://mark.stosberg.com/Tech/tips/bash.tips

Emil Vikström
  • 90,431
  • 16
  • 141
  • 175
2

the command for this manipulation of history is:

^old-text^new-text

For more reading I can just recommend the man page of bash, esp. the parts of readline and history.

flolo
  • 15,148
  • 4
  • 32
  • 57
  • Thanks. Is there a way to change a command that is not the last? Also, this change only the first occurance of old-text – Yotam Dec 07 '11 at 12:52
1

I suppose you do this in a command line, not in bash script the quickest way is to write cp and PRESS Alt+., press space, and Press Alt+. again and repair the path

Alt+. gives you last parameter you used

Ctrl+R gives you reverse search

UP arrow gives you latest commands

Jan Vorcak
  • 19,261
  • 14
  • 54
  • 90
0

press the up arrow to get the last command, then change whatever you want to have different.

or if you want to address an older command hit 'Ctrl-r' then start typing what you want and if the correct old command appears hit right arrow to make it the current. Then change whatever you want to change.

Angelo Fuchs
  • 9,825
  • 1
  • 35
  • 72