Reuse text on a bash command

13

2

If i'm writing a long command or just typing an extensive file path, is there any that i can "reuse" it with some command shortcut?

e.g:

1.cp /home/myuser/really/big/file/here/and/there.png /home/myuser/really/big/file/here/and/there.png.bkp

Do i really have to type it all over again?

Fisher

Posted 2011-06-08T19:26:28.520

Reputation: 213

Answers

14

Use brace expansion

cp /home/myuser/really/big/file/here/and/there.png{,.bkp}

bbaja42

Posted 2011-06-08T19:26:28.520

Reputation: 2 815

1+1 and you can go even shorter: cp /home/myuser/really/big/file/here/and/there.{,.bkp} – Mike Fitzpatrick – 2011-06-08T22:53:25.213

@peth: Oops, yes, you're correct. So we can actually go even shorter: cp /home/myuser/really/big/file/here/and/there{,.bkp} :) – Mike Fitzpatrick – 2011-06-16T22:58:51.090

4

Also, history expansion can work here:

cp /home/myuser/really/big/file/here/and/there.png !#:1.bkp

where the !#:1 part refers to the first argument of the command you're currently typing.

glenn jackman

Posted 2011-06-08T19:26:28.520

Reputation: 18 546

1

You can save lots of time typing that by using tab expansion, the tilde shortcut, and command history.

For instance,

~/r[tab]/b[tab]/f[tab]/h[tab]/a[tab]/t[tab]/

(where [tab] means "press the Tab key") would expand to

/home/myuser/really/big/file/here/and/there

You could also type

cp /home/myuser/really/big/file/here/and/there.png /some/destination

then press up-arrow and just change the last three letters of the filename

CarlF

Posted 2011-06-08T19:26:28.520

Reputation: 8 576