在终端/bash脚本中将文件夹拆分为多个子文件夹 [英] Split a folder into multiple subfolders in terminal/bash script

查看:220
本文介绍了在终端/bash脚本中将文件夹拆分为多个子文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个文件夹,每个文件夹有15,000至40,000张照片.我希望将每个文件夹拆分为多个子文件夹-每个文件夹中包含2,000个文件.

I have several folders, each with between 15,000 and 40,000 photos. I want each of these to be split into sub folders - each with 2,000 files in them.

执行此操作的快速方法是什么,它将在旅途中创建我需要的每个文件夹并移动所有文件?

What is a quick way to do this that will create each folder I need on the go and move all the files?

当前,我只能找到如何将文件夹中的前x个项目移动到预先存在的目录中.为了在具有20,000个项目的文件夹上使用此文件夹...我需要手动创建10个文件夹,然后运行命令10次.

Currently I can only find how to move the first x items in a folder into a pre-existing directory. In order to use this on a folder with 20,000 items... I would need to create 10 folders manually, and run the command 10 times.

ls -1  |  sort -n | head -2000| xargs -i mv "{}" /folder/

我尝试将其放入for循环中,但是使用mkdir使其无法正确制作文件夹时遇到了麻烦.即使解决了这个问题,我也需要程序仅为每个第20个文件(新组的开始)创建文件夹.它想为每个文件创建一个新文件夹.

I tried putting it in a for-loop, but am having trouble getting it to make folders properly with mkdir. Even after I get around that, I need the program to only create folders for every 20th file (start of a new group). It wants to make a new folder for each file.

那么...我如何轻松地将大量文件移动到每个文件夹中任意数量文件的文件夹中?

So... how can I easily move a large number of files into folders of an arbitrary number of files in each one?

任何帮助都会非常...很好...很有帮助!

Any help would be very... well... helpful!

推荐答案

此解决方案可以处理带有空格和通配符的名称,并且可以轻松扩展以支持不太直接的树结构.它将在工作目录的所有直接子目录中查找文件,并将它们分类到这些文件的新子目录中.新目录将被命名为01等:

This solution can handle names with whitespace and wildcards and can be easily extended to support less straightforward tree structures. It will look for files in all direct subdirectories of the working directory and sort them into new subdirectories of those. New directories will be named 0, 1, etc.:

#!/bin/bash

maxfilesperdir=20

# loop through all top level directories:
while IFS= read -r -d $'\0' topleveldir
do
        # enter top level subdirectory:
        cd "$topleveldir"

        declare -i filecount=0 # number of moved files per dir
        declare -i dircount=0  # number of subdirs created per top level dir

        # loop through all files in that directory and below
        while IFS= read -r -d $'\0' filename
        do
                # whenever file counter is 0, make a new dir:
                if [ "$filecount" -eq 0 ]
                then
                        mkdir "$dircount"
                fi

                # move the file into the current dir:
                mv "$filename" "${dircount}/"
                filecount+=1

                # whenever our file counter reaches its maximum, reset it, and
                # increase dir counter:
                if [ "$filecount" -ge "$maxfilesperdir" ]
                then
                        dircount+=1
                        filecount=0
                fi
        done < <(find -type f -print0)

        # go back to top level:
        cd ..
done < <(find -mindepth 1 -maxdepth 1 -type d -print0)

具有流程替换功能的find -print0/read组合已从

The find -print0/read combination with process substitution has been stolen from another question.

应该注意的是,简单的globing也可以处理各种奇怪的目录和文件名.但是,对于多个级别的目录来说,它并不容易扩展.

It should be noted that simple globbing can handle all kinds of strange directory and file names as well. It is however not easily extensible for multiple levels of directories.

这篇关于在终端/bash脚本中将文件夹拆分为多个子文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆