gpt4 book ai didi

linux - 根据名称的部分将成千上万个文件放在单独的文件夹中

转载 作者:太空宇宙 更新时间:2023-11-04 05:55:52 25 4
gpt4 key购买 nike

我有一个包含100.000个文件的目录,其名称如下:

1_jpeg.ext
1_png.ext
2_jpeg.ext
2_png.ext
...
101.235_jpeg.ext
101.236_jpeg.ext


我需要将所有这些文件放入单独的文件夹中,如下所示:

jpeg_folder
---001000
---002000
---003000
---...
png_folder
---001000
---002000
---003000
---...


我想将这些文件拆分为每个文件夹,每个文件夹恰好包含1000张图像。

它们的排序方式很重要,例如在jpeg文件夹下创建的名为001000的文件夹将具有以下文件:

1_jpeg.ext  
2_jpeg.ext
...


我在16.04 Ubuntu机器上。

最佳答案

我们可以使用sortsplit进行分组。

由于Ubuntu具有GNU实用程序,因此我们可以使用特定于GNU的split选项直接生成合适的名称。

#/bin/bash

# somewhere to store the intermediate information
mkdir tmp

# create list of files to move (dot prefix so "ls *" ignores it)
find -maxdepth 1 -mindepth 1 -type f | sort -n > tmp/.files

for ext in _jpeg.ext _png.ext; do
# top-level output directory names
type="$(basename "$ext" .ext)_folder"

# chunk each type of image into groups of (up to) 1000 files
grep "$ext\$" <tmp/.files |\
split --numeric-suffixes=1 -a3 -l1000 - tmp/

# use split's output filenames as basis for new directories
# then move each group of files. tidy up as we go
for grp in tmp/*; do
dir="$type/$(basename $grp)000"
mkdir -p "$dir"
cat "$grp" | xargs mv -t $dir/.
rm "$grp"
done
done

# clean up
rm tmp/.files
rmdir tmp

关于linux - 根据名称的部分将成千上万个文件放在单独的文件夹中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54864296/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com