简体   繁体   中英

Linux script to create a directory structure

I'm trying to create a directory structure for image uploads as described here , the result should be like this:

upload/
      00/00/00/
...
      00/00/ff/
...   
      00/ff/ff/
...   
      ff/ff/ff/

So i wrote flat python script:

ROOT = '/var/upload'

for a in xrange(0, 256):
    for b in xrange(0, 256):
        for c in xrange(0, 256):
            os.makedirs(os.path.join(ROOT, format(a, '02x'), format(b, '02x'), format(c, '02x')))

and got:

...
    os.makedirs(os.path.join(ROOT, format(a, '02x'), format(b, '02x'), format(c, '02x')))
File "/usr/lib64/python2.7/os.py", line 157, in makedirs
    mkdir(name, mode)

OSError: [Errno 28] No space left on device: '/var/upload/2e/04/cd'

I tried other location - '/opt/upload' with the same result. But there is enough space and there is no quotas:

[root@sky /]# df
/dev/mapper/rfremix_sky-root  51475068     23006628  25830616           48% /
devtmpfs                       2016520            0   2016520            0% /dev
tmpfs                          2024048         2064   2021984            1% /dev/shm
tmpfs                          2024048          500   2023548            1% /run
tmpfs                          2024048            0   2024048            0% /sys/fs/cgroup
tmpfs                          2024048           32   2024016            1% /tmp
/dev/sda1                       487652        90139    367817           20% /boot
/dev/mapper/rfremix_sky-home 424460680     47830948 355045248           12% /home

[root@sky /]# repquota /
repquota: Mountpoint (or device) / not found or has no quota enabled.
repquota: Not all specified mountpoints are using quota.
[root@sky /]#

System:

[root@sky /]# uname -a
Linux sky.dep1 3.12.10-300.fc20.x86_64 #1 SMP Thu Feb 6 22:11:48 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
[root@sky /]# python --version
Python 2.7.5
[root@sky /]#

So, question : what am I doing wrong?

Or maybe there are other solutions on bash, perl & etc, with better performance (256**3 = 16.777.216 folders)?

UPDATE (for Thomas Orozco):

[root@sky /]# df -i
Файловая система                Iнодов IИспользовано IСвободно IИспользовано%     Cмонтировано в
/dev/mapper/rfremix_sky-root   3276800       3276800         0           100% /
devtmpfs                        504130           422    503708             1% /dev
tmpfs                           506012            13    505999             1% /dev/shm
tmpfs                           506012           504    505508             1% /run
tmpfs                           506012            13    505999             1% /sys/fs/cgroup
tmpfs                           506012            41    505971             1% /tmp
/dev/sdb3                    164849116         96453 164752663             1% /mnt/win
/dev/sda1                       128016           380    127636             1% /boot
/dev/mapper/rfremix_sky-home  26968064        218284  26749780             1% /home
tmpfs                           506012          4364    501648             1%         /home/chip/.chrome/ramdisk
[root@sky /]#

As @Ricardo Cárdenes said - cause of the exception - limit in the number of inodes in file system. At another mount point (/home in my case) with sufficient inodes - this script work well.

Suitable solution for me, as @Krumelur says - to create directories as they are needed.

Thanks to everyone!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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