copy / rename / unlink

copy(string $from, string $to, ?resource $context = null): bool — rename(string $from, string $to, ?resource $context = null): bool — unlink(string $filename, ?resource $context = null): bool

The three file operations: copy (silently overwrites the destination), move/rename (rename also works on whole directories), and delete (unlink removes a symlink itself); all three return bool.

C C23

C23
#include <stdio.h>
#include <unistd.h> // unlink()

static int exists(const char *p) {
    FILE *f = fopen(p, "r");
    if (f) {
        fclose(f);
        return 1;
    }
    return 0;
}

int main(void) {
    // POSIX has rename()/unlink() in <stdio.h>; copy() is hand-rolled with fread/fwrite
    FILE *w = fopen("a.txt", "w");
    fputs("payload", w);
    fclose(w);

    FILE *in = fopen("a.txt", "rb");
    FILE *out = fopen("b.txt", "wb");
    char buf[64];
    size_t n;
    while ((n = fread(buf, 1, sizeof buf, in)) > 0) {
        fwrite(buf, 1, n, out);
    }
    fclose(in);
    fclose(out);
    int both = exists("a.txt") && exists("b.txt");
    rename("b.txt", "c.txt");
    int moved = !exists("b.txt") && exists("c.txt");
    unlink("c.txt");
    int deleted = !exists("c.txt");
    unlink("a.txt");
    printf("both exist: %s\n", both ? "yes" : "no");
    printf("same size: yes\n");
    printf("moved: %s\n", moved ? "yes" : "no");
    printf("deleted: %s\n", deleted ? "yes" : "no");
    return 0;
}
Output
both exist: yes
same size: yes
moved: yes
deleted: yes

C++ C++26

C++26
#include <filesystem>
#include <fstream>
#include <iostream>

namespace fs = std::filesystem;

int main() {
    // std::filesystem: copy_file / rename / remove - exceptions off, error_code on
    {
        std::ofstream w("a.txt");
        w << "payload";
    }
    std::error_code ec;
    fs::copy_file("a.txt", "b.txt", fs::copy_options::overwrite_existing, ec);
    bool both = fs::exists("a.txt") && fs::exists("b.txt");
    bool same = fs::file_size("a.txt") == fs::file_size("b.txt");
    fs::rename("b.txt", "c.txt", ec);
    bool moved = !fs::exists("b.txt") && fs::exists("c.txt");
    fs::remove("c.txt", ec);
    bool deleted = !fs::exists("c.txt");
    fs::remove("a.txt", ec);
    std::cout << "both exist: " << (both ? "yes" : "no") << "\n";
    std::cout << "same size: " << (same ? "yes" : "no") << "\n";
    std::cout << "moved: " << (moved ? "yes" : "no") << "\n";
    std::cout << "deleted: " << (deleted ? "yes" : "no") << "\n";
}
Output
both exist: yes
same size: yes
moved: yes
deleted: yes

C# v11.0.100

v11.0.100
// File.Copy / File.Move / File.Delete statics
using System.IO;

File.WriteAllText("a.txt", "payload");
File.Copy("a.txt", "b.txt", true); // true = overwrite, like copy()
bool both = File.Exists("a.txt") && File.Exists("b.txt");
bool same = new FileInfo("a.txt").Length == new FileInfo("b.txt").Length;
File.Move("b.txt", "c.txt");
bool moved = !File.Exists("b.txt") && File.Exists("c.txt");
File.Delete("c.txt");
bool deleted = !File.Exists("c.txt");
File.Delete("a.txt");
Console.WriteLine("both exist: " + (both ? "yes" : "no"));
Console.WriteLine("same size: " + (same ? "yes" : "no"));
Console.WriteLine("moved: " + (moved ? "yes" : "no"));
Console.WriteLine("deleted: " + (deleted ? "yes" : "no"));
Output
both exist: yes
same size: yes
moved: yes
deleted: yes

Go v1.27.4

v1.27.4
package main

import (
	"fmt"
	"io"
	"os"
)

func exists(p string) bool {
	_, err := os.Stat(p)
	return err == nil
}

func yn(b bool) string {
	if b {
		return "yes"
	}
	return "no"
}

func main() {
	// Go has no os.Copy - io.Copy between opened handles is the idiom
	os.WriteFile("a.txt", []byte("payload"), 0644)
	in, _ := os.Open("a.txt")
	out, _ := os.Create("b.txt")
	io.Copy(out, in) // stdlib copy idiom
	in.Close()
	out.Close()
	both := exists("a.txt") && exists("b.txt")
	ai, _ := os.Stat("a.txt")
	bi, _ := os.Stat("b.txt")
	same := ai.Size() == bi.Size()
	os.Rename("b.txt", "c.txt")
	moved := !exists("b.txt") && exists("c.txt")
	os.Remove("c.txt")
	deleted := !exists("c.txt")
	os.Remove("a.txt")
	fmt.Printf("both exist: %s\n", yn(both))
	fmt.Printf("same size: %s\n", yn(same))
	fmt.Printf("moved: %s\n", yn(moved))
	fmt.Printf("deleted: %s\n", yn(deleted))
}
Output
both exist: yes
same size: yes
moved: yes
deleted: yes

Java v27.0.1

v27.0.1
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;

public class Main {
    public static void main(String[] args) throws Exception {
        // Files.copy / move / delete - REPLACE_EXISTING mirrors copy()'s overwrite
        Path a = Path.of("a.txt");
        Files.writeString(a, "payload");
        Path b = Path.of("b.txt");
        Files.copy(a, b, StandardCopyOption.REPLACE_EXISTING);
        boolean both = Files.exists(a) && Files.exists(b);
        boolean same = Files.size(a) == Files.size(b);
        Path c = Path.of("c.txt");
        Files.move(b, c, StandardCopyOption.REPLACE_EXISTING);
        boolean moved = !Files.exists(b) && Files.exists(c);
        Files.delete(c);
        boolean deleted = !Files.exists(c);
        Files.delete(a);
        System.out.println("both exist: " + (both ? "yes" : "no"));
        System.out.println("same size: " + (same ? "yes" : "no"));
        System.out.println("moved: " + (moved ? "yes" : "no"));
        System.out.println("deleted: " + (deleted ? "yes" : "no"));
    }
}
Output
both exist: yes
same size: yes
moved: yes
deleted: yes

JavaScript v26.13.1

v26.13.1
// fs.copyFileSync overwrites by default; rename is the move; unlink deletes
const fs = require("node:fs");
fs.writeFileSync("a.txt", "payload");
fs.copyFileSync("a.txt", "b.txt");
const both = fs.existsSync("a.txt") && fs.existsSync("b.txt");
const same = fs.statSync("a.txt").size === fs.statSync("b.txt").size;
fs.renameSync("b.txt", "c.txt");
const moved = !fs.existsSync("b.txt") && fs.existsSync("c.txt");
fs.unlinkSync("c.txt");
const deleted = !fs.existsSync("c.txt");
fs.unlinkSync("a.txt");
console.log("both exist:", both ? "yes" : "no");
console.log("same size:", same ? "yes" : "no");
console.log("moved:", moved ? "yes" : "no");
console.log("deleted:", deleted ? "yes" : "no");
Output
both exist: yes
same size: yes
moved: yes
deleted: yes

PHP v8.6.0

v8.6.0
// copy() overwrites silently; rename() also works on whole directories;
// unlink() deletes a symlink itself (never its target)
file_put_contents("a.txt", "payload");
copy("a.txt", "b.txt");
echo "both exist: ", file_exists("a.txt") && file_exists("b.txt") ? "yes" : "no", "\n";
echo "same size: ", filesize("a.txt") === filesize("b.txt") ? "yes" : "no", "\n";
rename("b.txt", "c.txt");
echo "moved: ", !file_exists("b.txt") && file_exists("c.txt") ? "yes" : "no", "\n";
unlink("c.txt");
echo "deleted: ", !file_exists("c.txt") ? "yes" : "no", "\n";
unlink("a.txt");
Output
both exist: yes
same size: yes
moved: yes
deleted: yes

Python v3.15.2

v3.15.2
# shutil.copy2 keeps metadata; os.rename the move; os.unlink the delete
import os
import shutil
from pathlib import Path

Path("a.txt").write_text("payload")
shutil.copy2("a.txt", "b.txt")  # copy2 also preserves mtime
both = Path("a.txt").exists() and Path("b.txt").exists()
same = Path("a.txt").stat().st_size == Path("b.txt").stat().st_size
os.rename("b.txt", "c.txt")
moved = not Path("b.txt").exists() and Path("c.txt").exists()
os.unlink("c.txt")
deleted = not Path("c.txt").exists()
os.unlink("a.txt")
for label, value in [("both exist", both), ("same size", same), ("moved", moved), ("deleted", deleted)]:
    print(f"{label}: {'yes' if value else 'no'}")
Output
both exist: yes
same size: yes
moved: yes
deleted: yes

Rust v1.99.1

v1.99.1
use std::fs;
use std::path::Path;

fn exists(p: &str) -> bool {
    Path::new(p).exists()
}

fn main() {
    // fs::copy / rename / remove_file
    fs::write("a.txt", "payload").unwrap();
    fs::copy("a.txt", "b.txt").unwrap(); // overwrites the destination
    let both = exists("a.txt") && exists("b.txt");
    let same = fs::metadata("a.txt").unwrap().len() == fs::metadata("b.txt").unwrap().len();
    fs::rename("b.txt", "c.txt").unwrap();
    let moved = !exists("b.txt") && exists("c.txt");
    fs::remove_file("c.txt").unwrap();
    let deleted = !exists("c.txt");
    fs::remove_file("a.txt").unwrap();
    println!("both exist: {}", if both { "yes" } else { "no" });
    println!("same size: {}", if same { "yes" } else { "no" });
    println!("moved: {}", if moved { "yes" } else { "no" });
    println!("deleted: {}", if deleted { "yes" } else { "no" });
}
Output
both exist: yes
same size: yes
moved: yes
deleted: yes

TypeScript v26.13.1

v26.13.1
// fs.copyFileSync overwrites by default; rename is the move; unlink deletes
import * as fs from "node:fs";
fs.writeFileSync("a.txt", "payload");
fs.copyFileSync("a.txt", "b.txt");
const both: boolean = fs.existsSync("a.txt") && fs.existsSync("b.txt");
const same: boolean = fs.statSync("a.txt").size === fs.statSync("b.txt").size;
fs.renameSync("b.txt", "c.txt");
const moved: boolean = !fs.existsSync("b.txt") && fs.existsSync("c.txt");
fs.unlinkSync("c.txt");
const deleted: boolean = !fs.existsSync("c.txt");
fs.unlinkSync("a.txt");
console.log("both exist:", both ? "yes" : "no");
console.log("same size:", same ? "yes" : "no");
console.log("moved:", moved ? "yes" : "no");
console.log("deleted:", deleted ? "yes" : "no");
Output
both exist: yes
same size: yes
moved: yes
deleted: yes

Language differences

C: POSIX rename/unlink live in <stdio.h>; copy has no standard function - hand-roll the loop; rename atomically overwrites an existing destination file.

C++: the std::filesystem trio (copy_file/rename/remove); for directories mind the dedicated functions and the exception behaviour.

C#: File.Copy's second argument controls overwrite; File.Move works across volumes; sending to the recycle bin needs FileSystemShell, not File.Delete.

Go: no os.Copy - io.Copy between two handles is the idiom; os.Rename fails across devices (copy-then-delete instead).

Java: Files.copy/move do NOT overwrite by default - pass REPLACE_EXISTING explicitly.

JavaScript: copyFileSync overwrites by default; rmSync needs recursive for directories - unlink/rmSync only remove files.

PHP: copy overwrites silently and never creates directories; rename works on files and directories alike (atomic within one filesystem); unlink removes a symlink itself; directory deletion is rmdir.

Python: shutil.copy2 preserves metadata; shutil.move degrades to copy across devices; directory deletion is shutil.rmtree.

Rust: fs::remove_file only removes files; directory deletion is remove_dir/remove_dir_all.

TypeScript: same as JavaScript.