Linux 如何将所有文件从一个目录移动(并覆盖)到另一个目录?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7190965/
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
How to move (and overwrite) all files from one directory to another?
提问by Chris
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
将文件从一个地方移动到另一个地方的命令,但是如何将所有文件从一个目录移动到另一个目录(有一堆其他文件),如果文件已经存在则覆盖?
采纳答案by musiKk
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
为每个文件执行一次。
回答by Shahbaz
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:
如果您想将具有特定“外观”的所有文件移动到它,您可以这样做:
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. 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
除非您确定自己在做什么。
回答by iSkore
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
-v
如果您想查看正在执行的操作,请使用:mv -v
Use -i
to prompt before overwriting: mv -i
用于-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
,等等。
回答by kenorb
It's also possible by using rsync
, for example:
也可以通过使用rsync
,例如:
rsync -va --delete-after src/ dst/
where:
在哪里:
-v
,--verbose
: increase verbosity-a
,--archive
: archive mode; equals-rlptgoD
(no-H,-A,-X
)--delete-after
: delete files on the receiving side be done after the transfer has completed
-v
,--verbose
: 增加冗长-a
,--archive
: 存档模式;等于-rlptgoD
(否-H,-A,-X
)--delete-after
: 在传输完成后删除接收方的文件
If you've root privileges, prefix with sudo
to override potential permission issues.
如果您拥有 root 权限,请添加前缀sudo
以覆盖潜在的权限问题。
回答by David Parks
mv -f source target
From the man page:
从手册页:
-f, --force
do not prompt before overwriting
回答by blizz
If you simply need to answer "y" to all the overwrite prompts, try this:
如果您只需要对所有覆盖提示回答“y”,请尝试以下操作:
y | mv srcdir/* targetdir/