简体   繁体   English

从目录中提取有限的文件,然后将其插入另一个目录

[英]Take limited files from directory and insert them into another directory

我的目录包含80.000个图像 ,我想使用Bash命令将该目录中的30.000个图像随机插入另一个目录。

假设您位于80000张图片所在的目录中,而另一个目录称为target,则此目录应该有效:

ls | shuf | head -n 30000 | xargs -i cp "{}" target/.

While the provided answer is correct shuf is not installed on all systems so you might consider to use sort -R instead. 尽管提供的答案正确, shuf不是在所有系统上都安装了shuf因此您可以考虑使用sort -R I quote the man page: 我引用手册页:

Sort by a random order. 按随机顺序排序。 This is a random permutation of the inputs except that the equal keys sort together. 这是输入的随机排列,除了相等的键排序在一起。 It is implemented by hashing the input keys and sorting the hash values. 它通过对输入键进行哈希处理并对哈希值进行排序来实现。 The hash function is chosen randomly. 哈希函数是随机选择的。 The hash function is randomized by /dev/random content, or by file content if it is specified by --random-source. 哈希函数由/ dev / random内容随机分配,如果由--random-source指定,则按文件内容随机分配。 Even if multiple sort fields are specified, the same random hash function is used for all of them. 即使指定了多个排序字段,也对所有它们使用相同的随机哈希函数。

As long as you do not have any duplicates (exactly the same file just with a different name) and you do not want to install shuf or any other tool you can use this to get a 'random' sort. 只要您没有任何重复项(名称完全相同,只是文件名不同),并且您不想安装shuf或任何其他工具,就可以使用它来进行“随机”排序。

ls | sort -R | tail -n 30000 | xargs -I "{}" cp "{}" ./target

暂无
暂无

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

相关问题 转换文件并将它们放置在另一个目录中 - Convert files and place them in another directory 列出目录中的文件并将它们复制替换到 Linux 中的另一个目录中 - List the files in Directory and Copy-Replace them into another Directory in Linux “查找”目录下的所有.log文件,将它们复制到以其父目录命名的另一个目录? - 'find' all .log files under a directory, copy them to another directory named after their parent directory? 如何比较一个目录中的文件列表是否存在于第二个目录中并将它们复制到第三个目录? - How do I compare if list of files from one directory exist in second directory and copy them to third directory? 使用awk或bash将文件从一个目录移动到另一目录 - move files from one directory to another directory with awk or bash Bash 脚本备份,检查目录是否包含其他目录的文件 - Bash script backup, check if directory contains the files from another directory 替换另一个目录中的文件 - replacing files in another directory 如何获取当前目录中的实际文件(而不在脚本中精确命名)并进行处理? - How to take actual files (without naming it precisely in the script) in the current directory and to process them? 根据唯一时间戳对目录中的文件进行排序并将其分组 - sort files from directory based on unique time stamp and group them 从目录中获取所有文件,并将它们放入数组中 - Get all files from a directory and put them in an array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM