linux下如何将文件从一个文件夹移动到另一个文件夹

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20161243/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 01:26:12  来源:igfitidea点击:

How to move file from one folder to different folder in linux

c++clinuxmv

提问by user2845185

How to move a particular file from one folder to another folder?

如何将特定文件从一个文件夹移动到另一个文件夹?

What I have tried,

我所尝试的,

#include <stdio.h>

int main() {
    FILE *tFile;
    if (tFile != NULL)
        tFile = NULL;
    if ((tFile = fopen("TempFile.txt", "rw")) == NULL) {
        return -1;
    }

    mv("TempFile.txt", "../MST");

    printf("Done Succesfully\n");
    return 0;
}

Error :

错误 :

test.c:17:2: warning: no newline at end of file
/tmp/ccKLWYNa.o(.text+0x5e): In function `main':
: undefined reference to `mv'
collect2: ld returned 1 exit status

Please guide me how can I do this.

请指导我如何做到这一点。

回答by Basile Starynkevitch

You really should read Advanced Linux Programming.

你真的应该阅读高级 Linux 编程

To move (from C) a file from one place to another in the same file systemjust use the rename(2)syscall.

要将(从 C)文件从同一文件系统中的一个位置移动到另一个位置,只需使用rename(2)系统调用。

At the very least, for your particular example, you'll need to code:

至少,对于您的特定示例,您需要编写代码:

char* srcpath = "TempFile.txt"; // assume it is a variable path
char destpath[1024];
snprintf (destpath, sizeof(destpath), "../MST/%s", srcpath);
if (rename (srcpath, destpath)) {
   // something went wrong
   if (errno == EXDEV) {
      // copy data and meta data 
   } else { perror("rename"); exit(EXIT_FAILURE); };
} 
else { // the rename succeeded
}

If you really want to mv TempFile.txt ../MST/TempFile.txtspecifically for TempFile.txtonly you could just call rename("TempFile.txt", "../MST/TempFile.txt")and handle the error cases like I suggest. If you are sure that ../MST/lie in the same file system than .then EXDEVshould not happen and you don't need to handle it particularly (but you do need to handle errors).

如果您真的只想mv TempFile.txt ../MST/TempFile.txt专门用于TempFile.txt您可以rename("TempFile.txt", "../MST/TempFile.txt")像我建议的那样调用并处理错误情况。如果您确定../MST/位于同一个文件系统中,.那么EXDEV不应该发生并且您不需要特别处理它(但您确实需要处理错误)。

If you want to move a file between two different file systems, you have to copy the data (and perhaps some of the meta-data) yourself (and then remove e.g. with unlink(2)) the original source file). You could detect that situation by various means: you could just try the renameand if errno(see errno(3)) is EXDEVyou need to copy the file. Or you could use stat(2)to query the source file(and the destination directory) meta-data -e.g. its size and its file system.

如果您想在两个不同的文件系统之间移动文件,您必须自己复制数据(可能还有一些元数据)(然后删除例如使用unlink(2))原始源文件)。您可以通过各种方式检测这种情况:您可以尝试rename如果errno(请参阅errno(3)EXDEV是否需要复制文件。或者你可以使用stat(2)来查询源文件(和目标目录)元数据 - 例如它的大小和它的文件系统。

Of course, you need to understand what are files on Linux (or Posix), in particular what is an inode....

当然,您需要了解 Linux(或 Posix)上的文件是什么,尤其是什么是inode....

You could have used systemwith /bin/mv(but be careful about strange characters -like spaces or semicolons- in the file paths, you need to escape them to avoid code injection), apparently you don't want to.

您可以使用systemwith /bin/mv(但要小心文件路径中的奇怪字符 - 如空格或分号,您需要对它们进行转义以避免代码注入),显然您不想这样做。

You should play with strace(1)(or perhaps also ltrace) on mvin various situations to understand what it is doing. Also, study the source code of GNU coreutilswhich provides /bin/mvnotably in mv.c...

您应该在各种情况下使用strace(1)(或者也可以使用ltracemv来了解它在做什么。另外,研究GNU coreutils的源代码,它/bin/mvmv.c 中特别提供......

Some extra C or C++ libraries may provide you with functions to move files (in the same filesystem they should do a rename, in different file systems they copy the source file data and perhaps some meta-data and unlink the source, so cannot be atomic), e.g. in C g_file_move(from Giowith Glib from Gnome), or in C++ copy_file-followed by removein Boost, etc etc....

一些额外的 C 或 C++ 库可能会为您提供移动文件的函数(在同一个文件系统中他们应该做一个rename,在不同的文件系统中他们复制源文件数据和一些元数据并取消链接源,所以不能是原子的)例如用C g_file_move(从吉奥与油嘴从侏儒),或在C ++ COPY_FILE-followed通过remove在升压,等等等等....

PS. For temporary files see tmpfile(3), mkstemp(3), etc...

附注。对于临时文件,请参阅tmpfile(3)mkstemp(3)等...