如何从一个文件夹移动文件在Linux中不同的文件夹 [英] How to move file from one folder to different folder in linux

查看:386
本文介绍了如何从一个文件夹移动文件在Linux中不同的文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我已经试过了,

#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;
}

错误:

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.

推荐答案

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

要从一个地方在同一个文件系统中移动(从C)的文件到另一个的 的只是使用的重命名(2)系统调用

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

最起码,你的特殊的例子,你需要code:

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
}

如果你真的想 MV TempFile.txt ../ MST / TempFile.txt 专门为 TempFile.txt 只有你可以只调用重命名(TempFile.txt,../ MST / TempFile.txt)和处理错误的情况下,像我建议。如果你确信 ../ MST / 横亘在同一文件系统比然后 EXDEV 不应该发生,你不需要特别处理它(但你需要处理的错误)。

If you really want to mv TempFile.txt ../MST/TempFile.txt specifically for TempFile.txt only 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 EXDEV should not happen and you don't need to handle it particularly (but you do need to handle errors).

如果要移动两个不同的文件系统之间的文件,你必须使用的取消链接(2))原始的源文件)。你可以通过各种方式检测这种情况:你可以只尝试改名,如果错误号(见的错误号(3))为 EXDEV 你需要复制的文件。或者你可以使用 STAT(2)查询源文件(和目标的目录的)元数据-eg它的大小和文件系统。

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 rename and if errno (see errno(3)) is EXDEV you 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.

当然,你需要了解什么是在Linux(或POSIX)文件,特别是什么是 inode的 ....

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

您也可以使用系统 /斌/ MV (但要小心奇怪的字符式的空格或semicolons-的文件路径,你需要躲避他们避免 code注射液),显然你不希望。

You could have used system with /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.

您应该 strace的(1)播放(或或许也是 ltrace )的在各种情况下的MV 明白它在做什么。此外,研究 GNU的coreutils 提供源$ C ​​$ C /斌/ MV 尤其是在 mv.c ...

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

一些额外的C或C ++库可为您提供功能的移动文件(在同一个文件系统,他们应该做一个重命名,在不同的文件系统,他们腼腆源文件数据也许一些元数据,并取消链接源),例如:用C g_file_move (从的吉奥与油嘴从侏儒的),或在C ++中的 COPY_FILE 通过删除在升压,等等等等....

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 coy the source file data and perhaps some meta-data and unlink the source), e.g. in C g_file_move (from Gio with Glib from Gnome), or in C++ copy_file -followed by remove in Boost, etc etc....

PS。临时文件请参见 TMPFILE(3),的 mkstemp(3),等等...

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

这篇关于如何从一个文件夹移动文件在Linux中不同的文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆