How to rename a file?

How can I rename a file on the unix platform programmatically without using the standard rename function?

+3
source share
2 answers

The historical way to rename a file is to use link (2) to create a new hard link to the same file, and then use unlink (2) to delete the old name.

+38
source

The following is a somewhat ironic solution that itself does not use a standard system call rename(2):

#include <stdlib.h>

if (system("mv file1 file2") != 0)
    perror("system");

This is an indirect use rename(2), this syscall is called mv(1).

+4
source

Source: https://habr.com/ru/post/1719773/


All Articles