简体   繁体   English

如何使用 Bash 脚本将文件随机分布在 3 个文件夹中?

[英]How to randomly distribute the files across 3 folders using Bash script?

I have many subdirectories and files in the folder mydata/files .我在mydata/files夹中有许多子目录和文件。 I want to take files and copy them randomly into 3 folders:我想获取文件并将它们随机复制到 3 个文件夹中:

train
test
dev

For example, mydata/files/ss/file1.wav could be copied into train folder:例如,可以将mydata/files/ss/file1.wav复制到train文件夹中:

train
  file1.wav

And so on and so forth, until all files from mydata/files are copied.依此类推,直到复制mydata/files中的所有文件。

How can I do it using Bash script?如何使用 Bash 脚本来做到这一点?

Steps to solve this:解决此问题的步骤:

  1. Need to gather all the files in the directory需要收集目录下的所有文件
  2. Assign directories to a map将目录分配给 map
  3. Generate random number for each file为每个文件生成随机数
  4. Move the file to the corresponding directory将文件移动到对应目录

The script:剧本:

#!/bin/bash

original_dir=test/

## define 3 directories to copy into
# define an associative array (like a map)
declare -A target_dirs

target_dirs[0]="/path/to/train/"
target_dirs[1]="/path/to/test/"
target_dirs[2]="/path/to/dev/"

# recursively find all the files, and loop through them
find $original_dir -type f | while read -r file ; do
        # find a random number 0 - (size of target_dirs - 1)
        num=$(($RANDOM % ${#target_dirs[@]}))
        # get that index in the associative array
        target_dir=${target_dirs[$num]}
        # copy the file to that directory
        echo "Copying $file to $target_dir"
        cp $file $target_dir
done

Things you'll need to change:您需要更改的内容:

  1. Change the destination of the directories to match the path in your system更改目录的目标以匹配系统中的路径
  2. Add executable priviledges to the file so that you can run it.将可执行权限添加到文件中,以便您可以运行它。
chmod 744 copy_script_name
./copy_script_name

Notes:笔记:

This script should easily be extendable to any number of directories if needed (just add the new directories, and the script will adjust the random numbers.如果需要,这个脚本应该很容易扩展到任意数量的目录(只需添加新目录,脚本将调整随机数。

If you need to only get the files in the current directory (not recursively), you can add -maxdepth 1 (see How to list only files and not directories of a directory Bash? ).如果您只需要获取当前目录中的文件(不是递归),您可以添加 -maxdepth 1 (请参阅如何仅列出文件而不是目录 Bash 的目录? )。

Was able to leverage previous bash experience plus looking at bash documentation (it's generally pretty good).能够利用以前的 bash 经验,并查看 bash 文档(通常相当不错)。 If you end up writing any scripts, be very careful about spaces如果您最终编写任何脚本,注意空格

You can create a temp file, echo your destination folder to it, then use the shuf command.您可以创建一个临时文件,将您的目标文件夹回显到它,然后使用shuf命令。

dest=$(mktemp)
echo -e "test\ndev\ntrain" >> $dest
while IFS= read -r file; do
  mv "$file" "$(shuf -n1 < $dest)/."
done < <(find mydata/files -type f 2>/dev/null)
rm -f "$dest"

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM