How to copy a file within its original directory, most easily?

leftaroundabout

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 simply cp 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 the cp 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 with read. 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.

casey

As per this question, you can use:

cp /long/path/to/file{,-copy}

This a shell expansion done by bash (not sure about others), so it will work with any command.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to find a file and copy its directory?

From Dev

How to copy a file line by line keeping its original line breaks

From Dev

How to copy a file line by line keeping its original line breaks

From Dev

How to Move or Copy Folders using directory Wildcards within Batch File?

From Dev

How to check if a file is within a directory

From Dev

How can I easily copy the whole CSS file from a website?

From Dev

How to force ElementTree to keep xmlns attribute within its original element?

From Dev

How to easily delete swap file placed in another directory (vim)?

From Dev

How to copy files within directory rather than a whole directory in TCL?

From Dev

how to copy a directory (folder and its contents) to another directory?

From Dev

How to find most recent file from a directory

From Dev

How to get the most recent file of a directory in Java

From Dev

How to find most recent file from a directory

From Dev

How to move a file to another directory and write its filename to a file (create and append) within a loop in C# for SSIS Script Task?

From Dev

Grunt.js: copy file without its parent directory

From Dev

How to copy a file into folder and all its subdirectories

From Dev

C# How to copy a file without changing the original file name

From Dev

How to copy a file to another directory programmatically?

From Dev

How to copy a file in a zipfile into a certain directory?

From Dev

How to make a copy of file in the same directory

From Dev

How to Copy Only the Directories with a specific File in that directory

From Dev

How to copy a file to another directory programmatically?

From Dev

how can i copy file in data directory

From Dev

How to copy a file in a zipfile into a certain directory?

From Dev

How to copy a file or multiple to the directory I previously was?

From Dev

How can I copy a hidden directory recursively and preserving its permissions?

From Dev

How do I copy the directory and its folders in windows command line?

From Dev

How to download file with its original name instead of unique name?

From Dev

How to download file with its original name instead of unique name?

Related Related

  1. 1

    How to find a file and copy its directory?

  2. 2

    How to copy a file line by line keeping its original line breaks

  3. 3

    How to copy a file line by line keeping its original line breaks

  4. 4

    How to Move or Copy Folders using directory Wildcards within Batch File?

  5. 5

    How to check if a file is within a directory

  6. 6

    How can I easily copy the whole CSS file from a website?

  7. 7

    How to force ElementTree to keep xmlns attribute within its original element?

  8. 8

    How to easily delete swap file placed in another directory (vim)?

  9. 9

    How to copy files within directory rather than a whole directory in TCL?

  10. 10

    how to copy a directory (folder and its contents) to another directory?

  11. 11

    How to find most recent file from a directory

  12. 12

    How to get the most recent file of a directory in Java

  13. 13

    How to find most recent file from a directory

  14. 14

    How to move a file to another directory and write its filename to a file (create and append) within a loop in C# for SSIS Script Task?

  15. 15

    Grunt.js: copy file without its parent directory

  16. 16

    How to copy a file into folder and all its subdirectories

  17. 17

    C# How to copy a file without changing the original file name

  18. 18

    How to copy a file to another directory programmatically?

  19. 19

    How to copy a file in a zipfile into a certain directory?

  20. 20

    How to make a copy of file in the same directory

  21. 21

    How to Copy Only the Directories with a specific File in that directory

  22. 22

    How to copy a file to another directory programmatically?

  23. 23

    how can i copy file in data directory

  24. 24

    How to copy a file in a zipfile into a certain directory?

  25. 25

    How to copy a file or multiple to the directory I previously was?

  26. 26

    How can I copy a hidden directory recursively and preserving its permissions?

  27. 27

    How do I copy the directory and its folders in windows command line?

  28. 28

    How to download file with its original name instead of unique name?

  29. 29

    How to download file with its original name instead of unique name?

HotTag

Archive