Say I'll start writing

sudo cp /etc/var/more/evenMORE/fileABC

and as the target of the copying, I'd like to define the same folder, except just a different filename (whether it is a new name like "randomNAME" or an iteration of the file "fileABC-backup01" shall be irrelevant). How can I reuse the input file so that I do not have to write it out again? Is this even possible?


4 Answers 4


sudo cp /etc/var/more/evenMORE/file{ABC,DEF} will copy fileABC as fileDEF in the same folder.
In general cp /xyz/{file1,file2} will copy /xyz/file1 as /xyz/file2. Basically, put anything that is common outside the {} and separate the source and destination names with a , in the {}.

  • I edited the op to clarify the quesiton.
    – henry
    Mar 23, 2016 at 20:03
  • Where do you want the output file to be copied? In /etc/var/more/evenMORE or some other directory? Please put the full cp command that you would have used in the question.
    – Munir
    Mar 23, 2016 at 20:06
  • Hey I wrote that! :) It should be in the same folder.
    – henry
    Mar 23, 2016 at 20:07
  • This will copy it in the same folder. Try it out.
    – Munir
    Mar 23, 2016 at 20:08
  • But this is based on the false premise that the filenames begin in the same way. They sometimes do not. Just to appreciate it in full: yes, this is useful for these "iteration"-like operations. Thank you. :)
    – henry
    Mar 23, 2016 at 20:09

If you are going to use this original file name over and over again, I suggest using it as such:

 sudo cp ${OrigFile} ${OrigFile}.bak
 sudo cp ${OrigFile} SomeRandomFile.txt

and every time you reference this ${OrigFile} variable name, it will go fetch this file. Make it something short if you prefer to type less.

But if you are looking for a way the shell references it, like $0 being the command and $1 as the first argument, there is none.


If you're willing to plan ahead a little you can put most of the path into a shell variable.

sudo cp "$a/fileABC" "$a/randomNAME"

You may do it in only one line this way:

sourcefile="/dir/filename"; sudo cp "$sourcefile" `dirname "$sourcefile"`/"new file name"

Not the answer you're looking for? Browse other questions tagged .