如何将所有文件从一个目录移动到另一个目录?

时间:2023-01-14 09:12:53

I know of the mv command to move a file from one place to another, but how do I move all files from one directory into another (that has a bunch of other files), overwriting if the file already exists?

我知道mv命令将文件从一个地方移动到另一个地方,但是我如何将所有文件从一个目录移动到另一个目录(它有一堆其他的文件),如果文件已经存在就重写?

5 个解决方案

#1


9  

It's just mv srcdir/* targetdir/.

它就是mv srcdir/* targetdir/。

If there are too many files in srcdir you might want to try something like the following approach:

如果srcdir文件太多,您可能需要尝试以下方法:

cd srcdir
find -exec mv {} targetdir/ +

In contrast to \; the final + collects arguments in an xargs like manner instead of executing mv once for every file.

相比之下\;final +以类似xargs的方式收集参数,而不是为每个文件执行一次mv。

#2


3  

For moving and overwriting files, it doesn't look like there is the -R option (when in doubt check your options by typing [your_cmd] --help. Also, this answer depends on how you want to move your file. Move all files, files & directories, replace files at destination, etc.

对于移动和覆盖文件,它看起来不像有-R选项(当有疑问通过键入[your_cmd]来检查选项时)。此外,这个答案取决于您希望如何移动文件。移动所有文件,文件和目录,替换目标文件,等等。

When you type in mv --help it returns the description of all options.

输入mv时——帮助它返回所有选项的描述。

For mv, the syntax is mv [option] [file_source] [file_destination]

对于mv,语法是mv[选项][file_source] [file_destination]

To move simple files: mv image.jpg folder/image.jpg

要移动简单的文件:mv image.jpg文件夹/image.jpg

To move as folder into destination mv folder home/folder

将文件夹移动到目标mv文件夹home/folder中

To move all files in source to distination mv folder/* home/folder/

将源文件中的所有文件移动到扩展mv文件夹/* home/folder/

Use -v if you want to see what is being done: mv -v

如果你想看看做了什么,用-v: mv -v

Use -i to prompt before overwriting: mv -i

使用-我在重写前提示:mv -i。

Use -u to update files in destination. It will only move source files newer than the file in the destination, and when it doesn't exist yet: mv -u

使用-u更新目标文件。它将只移动源文件比目标文件更新,当它还不存在时:mv -u

Tie options together like mv -viu, etc.

将选项捆绑在一起,如mv -viu等。

#3


1  

It's also possible by using rsync, for example:

也可以使用rsync,例如:

rsync -va --delete-after src/ dst/

where:

地点:

  • -v, --verbose: increase verbosity
  • - v,- verbose:增加冗长
  • -a, --archive: archive mode; equals -rlptgoD (no -H,-A,-X)
  • ————归档:归档模式;= -rlptgoD(没有- h,-,- x)
  • --delete-after: delete files on the receiving side be done after the transfer has completed
  • ——删除后:在传输完成后,删除接收方的文件

If you've root privileges, prefix with sudo to override potential permission issues.

如果您有根权限,可以使用带sudo的前缀来覆盖潜在的权限问题。

#4


0  

In linux shell, many commands accept multiple parameters and therefore could be used with wild cards. So, for example if you want to move all files from folder A to folder B, you write:

在linux shell中,许多命令接受多个参数,因此可以与通配符一起使用。例如,如果你想将所有文件从文件夹A移动到文件夹B,你可以这样写:

mv A/* B

If you want to move all files with a certain "look" to it, you could do like this:

如果你想把所有的文件都移动到一个特定的“look”上,你可以这样做:

mv A/*.txt B

Which copies all files that are blablabla.txt to folder B

复制所有的文件。txt文件夹B

Star (*) can substitute any number of characters or letters while ? can substitute one. For example if you have many files in the shape file_number.ext and you want to move only the ones that have two digit numbers, you could use a command like this:

星号(*)可以替换任意数量的字符或字母吗?可以用一个。例如,如果在shape file_number中有许多文件。ext和你想移动只有两个数字的,你可以使用如下命令:

mv A/file_??.ext B

Or more complicated examples:

或更复杂的例子:

mv A/fi*_??.e* B

For files that look like fi<-something->_<-two characters->.e<-something->

对于类似于fi<-something->_<- 2个字符->.e<-something->的文件

Unlike many commands in shell that require -R to (for example) copy or remove subfolders, mv does that itself.

与shell中需要-R复制或删除子文件夹的许多命令不同,mv本身就是这样做的。

Remember that mv overwrites without asking (unless the files being overwritten are read only or you don't have permission) so make sure you don't lose anything in the process.

请记住mv在没有请求的情况下进行重写(除非被覆盖的文件是只读的,或者您没有权限),所以请确保在过程中不会丢失任何内容。

For your future information, if you have subfolders that you want to copy, you could use the -R option, saying you want to do the command recursively. So it would look something like this:

对于将来的信息,如果您有要复制的子文件夹,您可以使用-R选项,表示希望递归地执行命令。大概是这样的:

cp A/* B -R

By the way, all I said works with rm (remove, delete) and cp (copy) too and beware, because once you delete, there is no turning back! Avoid commands like rm * -R unless you are sure what you are doing.

顺便说一下,我说的所有东西都适用于rm(删除、删除)和cp(复制),而且要小心,因为一旦你删除了,就没有回头路了!避免像rm * -R这样的命令,除非您确定正在做什么。

#5


0  

mv -f source target

From the man page:

从手册页:

-f, --force
          do not prompt before overwriting

#1


9  

It's just mv srcdir/* targetdir/.

它就是mv srcdir/* targetdir/。

If there are too many files in srcdir you might want to try something like the following approach:

如果srcdir文件太多,您可能需要尝试以下方法:

cd srcdir
find -exec mv {} targetdir/ +

In contrast to \; the final + collects arguments in an xargs like manner instead of executing mv once for every file.

相比之下\;final +以类似xargs的方式收集参数,而不是为每个文件执行一次mv。

#2


3  

For moving and overwriting files, it doesn't look like there is the -R option (when in doubt check your options by typing [your_cmd] --help. Also, this answer depends on how you want to move your file. Move all files, files & directories, replace files at destination, etc.

对于移动和覆盖文件,它看起来不像有-R选项(当有疑问通过键入[your_cmd]来检查选项时)。此外,这个答案取决于您希望如何移动文件。移动所有文件,文件和目录,替换目标文件,等等。

When you type in mv --help it returns the description of all options.

输入mv时——帮助它返回所有选项的描述。

For mv, the syntax is mv [option] [file_source] [file_destination]

对于mv,语法是mv[选项][file_source] [file_destination]

To move simple files: mv image.jpg folder/image.jpg

要移动简单的文件:mv image.jpg文件夹/image.jpg

To move as folder into destination mv folder home/folder

将文件夹移动到目标mv文件夹home/folder中

To move all files in source to distination mv folder/* home/folder/

将源文件中的所有文件移动到扩展mv文件夹/* home/folder/

Use -v if you want to see what is being done: mv -v

如果你想看看做了什么,用-v: mv -v

Use -i to prompt before overwriting: mv -i

使用-我在重写前提示:mv -i。

Use -u to update files in destination. It will only move source files newer than the file in the destination, and when it doesn't exist yet: mv -u

使用-u更新目标文件。它将只移动源文件比目标文件更新,当它还不存在时:mv -u

Tie options together like mv -viu, etc.

将选项捆绑在一起,如mv -viu等。

#3


1  

It's also possible by using rsync, for example:

也可以使用rsync,例如:

rsync -va --delete-after src/ dst/

where:

地点:

  • -v, --verbose: increase verbosity
  • - v,- verbose:增加冗长
  • -a, --archive: archive mode; equals -rlptgoD (no -H,-A,-X)
  • ————归档:归档模式;= -rlptgoD(没有- h,-,- x)
  • --delete-after: delete files on the receiving side be done after the transfer has completed
  • ——删除后:在传输完成后,删除接收方的文件

If you've root privileges, prefix with sudo to override potential permission issues.

如果您有根权限,可以使用带sudo的前缀来覆盖潜在的权限问题。

#4


0  

In linux shell, many commands accept multiple parameters and therefore could be used with wild cards. So, for example if you want to move all files from folder A to folder B, you write:

在linux shell中,许多命令接受多个参数,因此可以与通配符一起使用。例如,如果你想将所有文件从文件夹A移动到文件夹B,你可以这样写:

mv A/* B

If you want to move all files with a certain "look" to it, you could do like this:

如果你想把所有的文件都移动到一个特定的“look”上,你可以这样做:

mv A/*.txt B

Which copies all files that are blablabla.txt to folder B

复制所有的文件。txt文件夹B

Star (*) can substitute any number of characters or letters while ? can substitute one. For example if you have many files in the shape file_number.ext and you want to move only the ones that have two digit numbers, you could use a command like this:

星号(*)可以替换任意数量的字符或字母吗?可以用一个。例如,如果在shape file_number中有许多文件。ext和你想移动只有两个数字的,你可以使用如下命令:

mv A/file_??.ext B

Or more complicated examples:

或更复杂的例子:

mv A/fi*_??.e* B

For files that look like fi<-something->_<-two characters->.e<-something->

对于类似于fi<-something->_<- 2个字符->.e<-something->的文件

Unlike many commands in shell that require -R to (for example) copy or remove subfolders, mv does that itself.

与shell中需要-R复制或删除子文件夹的许多命令不同,mv本身就是这样做的。

Remember that mv overwrites without asking (unless the files being overwritten are read only or you don't have permission) so make sure you don't lose anything in the process.

请记住mv在没有请求的情况下进行重写(除非被覆盖的文件是只读的,或者您没有权限),所以请确保在过程中不会丢失任何内容。

For your future information, if you have subfolders that you want to copy, you could use the -R option, saying you want to do the command recursively. So it would look something like this:

对于将来的信息,如果您有要复制的子文件夹,您可以使用-R选项,表示希望递归地执行命令。大概是这样的:

cp A/* B -R

By the way, all I said works with rm (remove, delete) and cp (copy) too and beware, because once you delete, there is no turning back! Avoid commands like rm * -R unless you are sure what you are doing.

顺便说一下,我说的所有东西都适用于rm(删除、删除)和cp(复制),而且要小心,因为一旦你删除了,就没有回头路了!避免像rm * -R这样的命令,除非您确定正在做什么。

#5


0  

mv -f source target

From the man page:

从手册页:

-f, --force
          do not prompt before overwriting