[英]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:解决此问题的步骤:
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:您需要更改的内容:
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.