Hi all i was asked to find the terminal command that will make a copy of a file lets call it program3.cpp and give to the copy the name homework6.cpp. After that you will have two files with different names, but identical contents.

I know how to copy the file but i cant figure out how to create a second identical file with a different name. All help is appreciated. Thanks!

  • 5
    I love the irony of renaming program to homework on SE Oct 30, 2019 at 3:51

2 Answers 2


Copy and rename in the same time (also change filename, not only path):

cp program3.cpp homework6.cpp

Rename only:

mv program3.cpp homework6.cpp
  • 23
    Ah... I hate cp only supports "copy and rename" in the same dir; when you copy to another location, the destination can only be a dir, not the target file name.. and you must cp first and then mv.
    – WesternGun
    Jun 16, 2019 at 9:04
  • 2
    @WesternGun That's not true. cp /opt/MyFile /tmp/MyNewNamedFile What you CAN'T do is to copy a file from another location to your current location and rename it at the same time here (.) . (As far as I know)
    – DimiDak
    Jun 14, 2021 at 20:30
  • 1
    @DimiDak sure you can: cp ~/make/makefiles/rails_makefile ./Makefile renames the file rails_makefile to Makefile in the current directory :) (well, at least on macOS it does, maybe linux is different?)
    – stevec
    Jan 30, 2022 at 0:48
  • 1
    @stevec Yeap, that worked :)
    – DimiDak
    Jan 30, 2022 at 9:30

If you want to have the files permanently linked use the ln command instead of cp

ln program3.cpp homework6.cpp

This puts a file descriptor (hard link) under the name homework6.cpp to the same file location as program3.cpp

You must log in to answer this question.

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