在UNIX中将文件从一个位置移动到另一个位置 [英] Move a file from a location to another in UNIX

查看:123
本文介绍了在UNIX中将文件从一个位置移动到另一个位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须使用一些命令在unix中实现一个shell



我想将多个文件移动到像mv -t那样的文件夹位置。



问题是重命名功能,只是重命名文件不要移动该文件。



  void  mv_t()
{
int mutat;
char mvFile_name1 [ 256 ];
char mvFile_name2 [ 256 ];
int nr = 0 ;

printf( 要移动的文件数:);
fflush(stdin);
scanf( %d,& nr);

printf( 你搬家的文件:);
fflush(stdin);
scanf( %s,& mvFile_name1);

for (i = 0 ; i< nr; i ++)
{
printf( 您要移动的文件:);
fflush(stdin);
scanf( %s,& mvFile_name2);

mutat = rename(mvFile_name1,mvFile_name2);
if (mutat!= 0
perror( 错误);
}
}



这就是我写的。在for中,他取mvFile_name1并将其重命名为mvFile_name2,然后他没有该文件的先前名称。但如果这样可行,仍然不要移动mvFile_name1中的文件,他只需将它们重命名为mvFile_name1。

解决方案

如果要移动所有输入文件到指定的输出目录(如 mv -t 那样)然后你必须用这种方式重命名:

重命名(oldfilename,newfilename)



其中 newfilename 是新目录名和旧文件的正确字符串连接name。


重命名函数有两个误解:



  • 第一个参数必须是现有文件名,第二个参数必须是新文件名(参见重命名(2) - Linux手册页 [ ^ ])。你已经互换了它们。
  • 至少有一个参数(这里可能是目的地)应该是带路径的全名。如果没有,该功能不会移动,而是重命名当前工作目录中的文件。


所以你必须:



  • 保存新目录的输入。
  • 从输入的现有文件中获取没有路径的普通文件名(在用户输入完整路径的情况下)。
  • 从路径和纯文件名创建完整的目标文件名。
  • 调用重命名函数。

I have to implement a shell in unix with some commands

I want to move multiple files into a folder location like mv -t does.

The problem is that rename function, just rename a file dont move that file.

void mv_t()
{
  int mutat;
  char mvFile_name1[256];
  char mvFile_name2[256];
  int nr=0;

  printf("How many files you want to move: ");
  fflush(stdin);
  scanf("%d", &nr);

  printf("The file where you move: ");
  fflush(stdin);
  scanf("%s", &mvFile_name1);

  for(i=0; i<nr; i++)
  {
    printf("The file you want to move: ");
    fflush(stdin);
    scanf("%s", &mvFile_name2);

    mutat = rename(mvFile_name1, mvFile_name2);
    if(mutat != 0)
      perror("Error");
  }
}


This is what I wrote. In "for" he take mvFile_name1 and rename it as mvFile_name2, and then he don't have the previous name for the file. But if this will work, still don't move the files in the mvFile_name1, he just rename them like mvFile_name1.

解决方案

If you want to move all the input files to the specified output directory (like mv -t does) then you have to use rename this way:

rename( oldfilename, newfilename)


where newfilename is the proper string concatenation of the new directory name and the old file name.


There are two misunderstandings with the rename function:

  • The first argument must be the existing file name and the second the new file name (see rename(2) - Linux manual page[^]). You have interchanged them.
  • At least one argument (here probably the destination) should be a full name with path. If not, the function does not move but rename the file in the current working directory.

So you have to:

  • Save the input of the new directory.
  • Get the plain file name without path from the existing file entered (in the case the user entered a full path).
  • Create the full destination file name from the path and the plain file name.
  • Call the rename function.


这篇关于在UNIX中将文件从一个位置移动到另一个位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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