I have several files in a directory like:


To run another code, I need to have a text file with the names of the files but I need to split in 50 files per text file, like: File1.txt :


File2.txt :


any idea?? I was trying like:

ls -1 | xargs -n 50 >> File1.txt

but I don't know who to select each "item" in the xargs command. Thanks!!

2 Answers 2

ls -1 | split --lines=10

puts the files in the same directory. This can be avoided by

ls -1 | (cd /where/ever; split --lines=10)

or for a different file name:

ls -1 | split --lines=10 /dev/stdin /path/to/splitfile.
  • THANKS!!! and how can I change the name of the text files?
    – Alejandro
    Apr 4, 2014 at 22:45
  • @Alejandro I added that but that information was already in Graeme's answer. Apr 4, 2014 at 23:12

Try this (uses GNU split which is default on most Linux distros):

ls |
  sed "s:^:$(pwd)/:" |
  split -dl 50 --additional-suffix=.txt - /path/to/dest/File
  • I got this error: split: unrecognized option `--additional-suffix=.txt'
    – Alejandro
    Apr 4, 2014 at 22:53
  • @Alejandro If it is not supported, just remove that option. The effect will just be that the files don't have the .txt extension.
    – Graeme
    Apr 4, 2014 at 22:55
  • The -d may not be supported on some systems also, this gives numbered files instead of aa, ab, etc. The rest is POSIX though.
    – Graeme
    Apr 4, 2014 at 22:57

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.