In ubuntu I have a folder that contains many (~10M) .txt files (around 1-2KB each). Folder is not big by itself (around 400MB) and does not contain subfolders, just the .txt files. I need to list it and do not succeed in doing so. (I can count the number of files, though, with wc -l) question is: is there a way to split the folder somehow or other way, without calling methods that ls uses?

  • Try: for FILE in * ; do echo $FILE; done – Andrew Stubbs Jul 23 '14 at 10:22
  • You write: "I need to list it and do not succeed in doing so." What have you tried? What error message (or other output) do you get? – JyrgenN Jul 23 '14 at 11:48

You should be able to use find to list the files:

find .

Using * or the like as a command argument is bound to give you an "Argument list too long" error, but it may work in a shell loop like suggested above by Andrew Stubbs.

If it doesn't, you can feed the find output from above to a while loop so you can process single files:

find . | while read filename; do
    dosomething $name

with appropriate values of "dosomething".

To "split" the folder, you can move the files to e.g. subdirectories based on their name, e.g. like this:

mkdir _a _b _c _d _e _f _g _h _i _j _k _l _m _n _o _p _q _r _s _t _u _v _w _x _y
find . | while read filename; do
    case $filename in
        a*) mv $filename _a;;
        b*) mv $filename _b;;


        z*) mv $filename _z;;

More sophisticated schemes are possible, of course.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.