Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have several files in a directory like:

data1.dat
data2.dat
....
data24678.dat    

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 :

/PATH/OF/FILE/data1.data
/PATH/OF/FILE/data2.data
/PATH/OF/FILE/data3.data
......
/PATH/OF/FILE/data50.data

File2.txt :

/PATH/OF/FILE/data51.data
/PATH/OF/FILE/data52.data
/PATH/OF/FILE/data53.data
......
/PATH/OF/FILE/data100.data

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!!

share|improve this question

2 Answers 2

up vote 2 down vote accepted
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.
share|improve this answer
    
THANKS!!! and how can I change the name of the text files? –  Alejandro Apr 4 at 22:45
    
@Alejandro I added that but that information was already in Graeme's answer. –  Hauke Laging Apr 4 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
share|improve this answer
    
I got this error: split: unrecognized option `--additional-suffix=.txt' –  Alejandro Apr 4 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 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 at 22:57

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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