bash 移动大量文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11942422/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 22:32:04  来源:igfitidea点击:

Moving large number of files

bash

提问by DrXCheng

If I run the command mv folder2/*.* folder, I get "argument list too long" error.

如果我运行命令mv folder2/*.* folder,我会收到“参数列表太长”错误。

I find some example of lsand rm, dealing with this error, using find folder2 -name "*.*". But I have trouble applying them to mv.

我找到了一些关于lsand 的例子rm,处理这个错误,使用find folder2 -name "*.*". 但是我在将它们应用于mv.

回答by Karl Bielefeldt

find folder2 -name '*.*' -exec mv {} folder \;

-execruns any command, {}inserts the filename found, \;marks the end of the exec command.

-exec运行任何命令, {}插入找到的文件名,\;标记 exec 命令的结束。

回答by Idelic

The other findanswers work, but are horribly slow for a large number of files, since they execute one command for each file. A much more efficient approach is either to use +at the end of find, or use xargs:

其他find答案有效,但对于大量文件来说速度非常慢,因为它们为每个文件执行一个命令。一种更有效的方法是+在 结束时find使用,或者使用xargs

# Using find ... -exec +
find folder2 -name '*.*' -exec mv --target-directory=folder '{}' +

# Using xargs
find folder2 -name '*.*' | xargs mv --target-directory=folder

回答by Gadget

First, thanks to Karl's answer. I have only minor correction to this.

首先,感谢卡尔的回答。我对此只有轻微的修正。

My scenario:

我的场景:

Millions of folders inside /source/directory, containing subfolders and files inside. Goal is to copy it keeping the same directory structure.

/source/directory 中有数百万个文件夹,其中包含子文件夹和文件。目标是复制它保持相同的目录结构。

To do that I use such command:

为此,我使用这样的命令:

find /source/directory -mindepth 1 -maxdepth 1 -name '*' -exec mv {} /target/directory \;

Here:

这里:

  • -mindepth 1 : makes sure you don't move root folder
  • -maxdepth 1 : makes sure you search only for first level children. So all it's content is going to be moved too, but you don't need to search for it.
  • -mindepth 1 : 确保你不移动根文件夹
  • -maxdepth 1 :确保您只搜索第一级孩子。所以它的所有内容也将被移动,但您不需要搜索它。

Commands suggested in answers above made result directory structure flat - and it was not what I looked for, so decided to share my approach.

上面答案中建议的命令使结果目录结构变得平坦 - 这不是我想要的,所以决定分享我的方法。

回答by InternetSeriousBusiness

find folder2 -name '*.*' -exec mv \{\} /dest/directory/ \;

find folder2 -name '*.*' -exec mv \{\} /dest/directory/ \;

回答by user2309000

finddoesn't work with really long lists of files, it will give you the same error "Argument list too long". Using a combination of ls, grepand xargsworked for me:

find不适用于非常长的文件列表,它会给你同样的错误“参数列表太长”。结合使用lsgrepxargs对我有用

$ ls|grep RadF|xargs mv -t ../fd/

$ ls|grep RadF|xargs mv -t ../fd/

It did the trick moving about 50,000 files where mv and find alone failed.

它完成了移动大约 50,000 个文件的技巧,其中 mv 和 find 单独失败。

回答by Sid

This one-liner command should work for you. Yes, it is quite slow, but works even with millions of files.

这个单行命令应该适合你。是的,它很慢,但即使处理数百万个文件也能工作。

for i in /folder1/*; do mv "$i" /folder2; done

It will move all the files from folder /folder1to /folder2.

它将所有文件从文件夹移动/folder1/folder2.