7

For example, let’s say that you want to copy a file like this:

cp path/to/the/file.txt path/to/the/file.copy

It’s very frustrating to write the path twice so is there something like this with a special character:

cp path/to/the/file.txt :)file.copy

It would be very useful especially if you are trying to diff a two file in the same directory

Giacomo1968
  • 58,727

2 Answers2

16
cp path/to/the/{file.txt,file.copy}

This expands to cp path/to/the/file.txt path/to/the/file.copy.

kos
  • 285
Brusooo
  • 176
10

Brace expansion is the way to go here, but for the sake of completeness, in bash, you can use a "special character" for it, but it is cumbersome: !#:$:h.

  • !#: this is a history expansion event designator for "[the] entire command line typed so far".
  • :$ is a word designator for the last argument. If you wanted to use some other argument than the last one, you'll need to use the count: :1, :2, etc.
  • :h is a modifier to "[r]emove a trailing pathname component, leaving only the head".

So:

$ echo /usr/lib/python3.12/pipes.py !#:$:h/foo
echo /usr/lib/python3.12/pipes.py /usr/lib/python3.12/foo
/usr/lib/python3.12/pipes.py /usr/lib/python3.12/foo

These particular designators also work the same way in zsh.

muru
  • 1,336