简体   繁体   English

如何将所有文件从一个目录移动(并覆盖)到另一个目录?

[英]How to move (and overwrite) all files from one directory to another?

我知道mv命令可以将文件从一个地方移动到另一个地方,但是如何将所有文件从一个目录移动到另一个目录(有一堆其他文件),如果文件已经存在则覆盖?

mv -f source target

From the man page:从手册页:

-f, --force
          do not prompt before overwriting

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.最后的+以类似xargs方式收集参数,而不是为每个文件执行一次mv

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; -a , --archive : 存档模式; equals -rlptgoD (no -H,-A,-X )等于-rlptgoD (没有-H,-A,-X
  • --delete-after : delete files on the receiving side be done after the transfer has completed --delete-after : 在传输完成后删除接收方的文件

If you've root privileges, prefix with sudo to override potential permission issues.如果您有 root 权限,请加上sudo前缀以覆盖潜在的权限问题。

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] --help检查您的选项。此外,此答案取决于您要如何移动文件。全部移动文件、文件和目录、替换目标文件等。

When you type in mv --help it returns the description of all options.当您输入mv --help它会返回所有选项的描述。

For mv, the syntax is mv [option] [file_source] [file_destination]对于 mv,语法为mv [option] [file_source] [file_destination]

To move simple files: mv image.jpg folder/image.jpg移动简单文件: mv image.jpg folder/image.jpg

To move as folder into destination mv folder home/folder作为文件夹移动到目标mv folder home/folder

To move all files in source to destination mv folder/* home/folder/将源中的所有文件移动到目标mv folder/* home/folder/

Use -v if you want to see what is being done: mv -v如果您想查看正在执行的操作,请使用-vmv -v

Use -i to prompt before overwriting: mv -i覆盖前使用-i进行提示: mv -i

Use -u to update files in destination.使用-u更新目标中的文件。 It will only move source files newer than the file in the destination, and when it doesn't exist yet: mv -u它只会移动比目标文件更新的源文件,并且当它不存在时: mv -u

Tie options together like mv -viu , etc.将选项mv -viu在一起,如mv -viu等。

如果您只需要对所有覆盖提示回答“y”,请尝试以下操作:

y | mv srcdir/* targetdir/

In linux shell, many commands accept multiple parameters and therefore could be used with wild cards.在 linux shell 中,许多命令接受多个参数,因此可以与通配符一起使用。 So, for example if you want to move all files from folder A to folder B, you write:因此,例如,如果要将所有文件从文件夹 A 移动到文件夹 B,请编写:

mv A/* B

If you want to move all files with a certain "look" to it, you could do like this:如果您想将具有特定“外观”的所有文件移动到它,您可以这样做:

mv A/*.txt B

Which copies all files that are blablabla.txt to folder B它将所有 blablabla.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:例如,如果您有许多文件格式为 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->_<-two characters->.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.为了您以后的信息,如果您有要复制的子文件夹,您可以使用 -R 选项,表示您要递归执行该命令。 So it would look something like this:所以它看起来像这样:

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!顺便说一句,我所说的所有内容也适用于 rm(删除,删除)和 cp(复制),请注意,因为一旦删除,就无法回头! Avoid commands like rm * -R unless you are sure what you are doing.除非您确定自己在做什么,否则请避免使用rm * -R类的命令。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C ++将所有文件从一个目录移动到另一个目录 - C++ move all files from one directory to another 使用 Paramiko 将文件从一个目录移动到另一个目录 - Move files from one directory to another with Paramiko 如何对目录中的所有文件进行tar压缩并将该tar移至另一个目录 - how to tar all files in a directory and move that tar to another directory 如何将某些文件从一个目录移动到另一个目录,同时保持目录结构 - How to move certain files from one directory to another, while maintaining directory structure 一次将一个文件移动到另一个目录,提取文件名的一部分,并在所有文件移动后停止 - Move one file at a time to another directory, extract part of filename and stop after all files moved 将文件从一个目录移动到另一个目录,并添加到新目录中的每个文件名 - Move files from one dir to another and add to each files name in the new directory 如何在Java中将文件从一个驱动器移动到另一个驱动器 - How to move files from one drive to another drive in java 如何强制“cp”覆盖目录而不是在其中创建另一个目录? - How to force 'cp' to overwrite directory instead of creating another one inside? (Linux) 用另一个文件中的数据递归地覆盖文件夹中的所有文件 - (Linux) Recursively overwrite all files in folder with data from another file 将指定文件夹中的所有文件移到一个目录中 - Move all files in specified folder up one directory
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM