Shell – How to manage huge amount of files in shell

filesystemspythonshell

$ ls
./dir_with_huge_amount_of_files/errors/

Suppose a directory is full of pictures with unix timestamps, I mean a lot measured in many GBs or even more. Shell-commands like ls will get overflow-style warnings because they are not designed to work with millions (or more) of pictures. How can I manage such huge amount of files? If, for example, I want to find the picture in the middle (according to the timestamp in the name and creation time), is there some file-system that offers a built-in search feature? Which commands would you use? I tried the comfortable ls and find with necessary flags but they were either very slow or generated warnings so I am thinking that either I need better file-system or db or something like that to pre-index the pictures. I basically need one array to which inodes of the photos should be placed in chronological order. How to do that? Later, metadata with unix-timestamps could be added.

[Update]

There is a serious flaw in current answers, people just post sort-of-answers without empirical tests. If they had tested their suggestions, they would probably fail. Hence, I created you a command-line tool by which you can create the sandbox to create the huge amount of files and test your suggestions like with 1e7 amount of files. It can take a long time to generate the files so be patient. If someone knows quicker way to do this, please edit the code. Type python code.py --help to get the help. Have fun!

Usage Example to create a lot of dirred files

$ ls ./data2
ls: ./data2: No such file or directory
$ python testFill.py -n 3 -d 7                                                 
$ tree data2/                                                                  
data2/
|-- 0
|   |-- 1302407302636973
|   |-- 1302407302638022
|   `-- 1302407302638829
|-- 1
|   |-- 1302407302639604
|   |-- 1302407302641652
|   `-- 1302407302642399
|-- 2
|   |-- 1302407302643158
|   |-- 1302407302645223
|   `-- 1302407302646026
|-- 3
|   |-- 1302407302646837
|   |-- 1302407302649110
|   `-- 1302407302649944
|-- 4
|   |-- 1302407302650771
|   |-- 1302407302652921
|   `-- 1302407302653685
|-- 5
|   |-- 1302407302654423
|   |-- 1302407302656352
|   `-- 1302407302656992
`-- 6
    |-- 1302407302657652
    |-- 1302407302659543
    `-- 1302407302660156

7 directories, 21 files

Code testFill.py

# Author: hhh
# License: ISC license

import os, math, time, optparse, sys

def createHugeAmountOfFiles(fileAmount, dirAmount):
   counter = 0
   DENSITY = 1e7
   dir = "./data/"

   do = dir+str(counter)+"/"
   while (os.path.exists(do)):
      counter = counter+1
      do = dir+str(counter)+"/"

   os.mkdir(do)

   for d in range(int(dirAmount)):
      for f in range(int(fileAmount)):
         timeIt = int(time.time()*1e6)
         if (not os.path.exists(do)):
            os.mkdir(do)

         if (timeIt % DENSITY == 0):
            counter = counter+1
            do = dir+str(counter)+"/"

            if (not os.path.exists(do)):
               os.mkdir(do)


         do = dir+str(counter)+"/"
         if(not os.path.exists(do)):
            os.mkdir(do)

         f = open(do+str(timeIt), 'w')
         f.write("Automatically created file to test Huge amount of files.")
         f.close()
      counter = counter +1


def ls(dir):
   for root, dirs, files in os.walk("./data/"+dir):
      print(files)

def rm(dir):
   for root, dirs, files in os.walk("./data/"+dir):
      for f in files:
         os.remove("./data/"+dir+"/"+f)


def parseCli():
   parser = optparse.OptionParser()
   parser.add_option("-f", "--file", dest="filename",
                     help="Location to remove files only in ./Data.", metavar="FILE")
   parser.add_option("-n", "--number", dest="number",
                     help="Number of files to generate", metavar="NUMBER")
   parser.add_option("-r", "--remove", dest="remove",
                     help="Data -dir content to remove", metavar="NUMBER")
   parser.add_option("-d", "--dir", dest="dir",
                     help="Amount of dirs to generate", metavar="NUMBER")
   parser.add_option("-q", "--quiet",
                     action="store_false", dest="verbose", default=True,
                     help="don't print status messages to stdout")

   return parser.parse_args()

def main():
   (options, args) = parseCli()

   if (options.filename):
      ls(options.filename)
   if (options.number and options.dir):
      createHugeAmountOfFiles(options.number, options.dir)
   if (options.remove):
      rm(options.remove)


main()

Best Answer

Try a different shell. I'd recommend trying zsh for instance, and see if it allows more parameters.

If I understand correctly, part of the filename, is a UNIX timestamp. It might be advisable, to divide the files into folders. If the date/time format is a UNIX epoch number, put chunks of fractions of that number, say 10000's, in a separate folder.

If an ISO 8601 timestamp is part of the filename, simply divide by year, month or day.

Related Question