1

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

2 Answers 2

3
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.
2
  • 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
1

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
3
  • 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.