Shared server guidance · Question & answer

How do I copy or rename a selected Linux file without silently overwriting another?

cp copies a file and mv moves or renames it. Both can replace an existing destination, so inspect the target first and use the interactive option for an attended operation. A move within one filesystem and a move between filesystems have different underlying costs.

BeginnerLinux VPS and dedicated servers with GNU utilities
Go to the answer

Before you start

  • An authorised SSH session on the Linux server.

Instructions

1. Inspect the source and destination

Replace the sample paths with two approved files in a directory you control. Confirm that the source is a regular file and whether the destination already exists.

Remote Linux shell
ls -l -- "$HOME/server-notes/source.txt" "$HOME/server-notes/draft.txt"

A missing destination is expected for a new copy; a missing source must be resolved first.

2. Choose one operation

The first example creates a working copy. The second renames that copy. Read any overwrite prompt and answer no if the destination is not the intended file.

Remote Linux shell
cp -i -- "$HOME/server-notes/source.txt" "$HOME/server-notes/draft.txt"
mv -i -- "$HOME/server-notes/draft.txt" "$HOME/server-notes/reviewed.txt"

-i prompts before replacement. -- prevents a filename beginning with a hyphen from being parsed as an option.

Check the result

  • Inspect the final file’s size and contents. For an exact byte-for-byte copy, compare a trusted SHA-256 digest of each file.

Troubleshooting

The result appeared inside a directory
When the destination is an existing directory, the source basename is placed inside it. Inspect the target before running the command.

Official references

Use the documentation for your installed version when a flag, service name or platform feature differs.