# Script to move files in multiple folders

**URL:** <https://community.unix.com/t/script-to-move-files-in-multiple-folders/322969>\
**Category:** Shell Programming and Scripting\
**Created:** [January 19, 2013, 5:11pm UTC](https://community.unix.com/t/script-to-move-files-in-multiple-folders/322969 "2013-01-19T17:11:30Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mmsiddig](https://community.unix.com/letter_avatar/mmsiddig/32/6_22734026db12803ebb3e059622ac3534.png) [@mmsiddig](https://community.unix.com/u/mmsiddig)\
**Post date:** [January 19, 2013, 5:11pm UTC](https://community.unix.com/t/script-to-move-files-in-multiple-folders/322969/1 "2013-01-19T17:11:30Z")

</div>

Hello all,

I would appreciate any help to write a script. I have folder A which contains over 30 thousands xml files, I would like create multiple folders and move those files (500 in each folders).

Thank you

---

<div class="post-metadata">

**Author:** ![Yoda](https://community.unix.com/user_avatar/community.unix.com/yoda/32/2498_2.png) [@Yoda](https://community.unix.com/u/Yoda)\
**Post date:** [January 19, 2013, 5:56pm UTC](https://community.unix.com/t/script-to-move-files-in-multiple-folders/322969/2 "2013-01-19T17:56:38Z")

</div>

Here is a bash script:

```nohighlight
#!/bin/bash

c=1; d=1; mkdir -p dir_${d}

for xml_file in *.xml
do
        if [$c -eq 501]
        then
                d=$(( d + 1 )); c=0; mkdir -p dir_${d}
        fi
        echo mv "$xml_file" dir_${d}/
        c=$(( c + 1 ))
done

```

Note: Remove the highlighted echo if the output looks good.
