What I want to do is basically
cp long/directory/path/file long/directory/path/file-copy
This or similar operations are pretty common. Typing out the whole path twice is obviously awkward, even with auto-completion or copy-paste. There are a couple of simple ways to do it, but none seems ideal:
- First
cd
into the directory, then simplycp file file-copy
. Least keystrokes, but I end up in the directory, which I didn't really want. - Wrap the above in a subshell,
sh -c 'cd dir-path; cp file file-copy'
, to make thecp
local. Fair enough, but that means I have to type the commands in a string literal, which disables auto-completion and isn't overall nice. - Define the dir as a variable, then just
cp "$dir/file" "$dir/file-copy"
. Can work nicely, but I'm kind of paranoid about namespace pollution... echo dir-path | while read p; do cd "$p"; cp file file-copy; done
. Basically combines subshell-wrapping with variable definition, by emulating a lambda-calculus stype let-binding withread
. I quite like it, but it's just a bit weird, and involves a lot of extra characters.- Anything I can come up with that uses
sed 's/file/file-copy/'
needs even more boilerplate around it. - Open the file with an editor, then save-as. Not well applicable to non-text files, and often resource-costly.
Is there a more elegant alternative? Ideally one that works with mv
as well, analogously.