2

Although I have found many many cases where one can use the split command to split a file into parts, I wasn't lucky enough to find a way to use split to massively split a group of files.

For example I have a folder and inside this folder are hundreds of files that start with the character "a" and end with ".txt". How can I get split with one line of code to split all of these files, say into parts with at most 20 lines of text? Something like:

split -l 20 a*.txt 

does not work because split gives an

extra operand "filename" error.

Thanks

2 Answers 2

1

This will do the job:

find . -maxdepth 1 -type f -name 'a*.txt' -exec split --additional-suffix=.txt -l 20 {} {} \;

Run the above command from within the directory that contains the files you want to split. It will:

  1. find all the text files in the current path with names starting with a.
  2. split them and keep the original split filename (so that you know from which original file it was split) and add letters to it.
  3. add .txt to the end of each newly created file name for easier file type identification.
0
1

I mostly use split in scenario where I want later splitted parts to merge together again. In situation like you describe, I can't imagine, how you could later them merge into original files. So, if in your situation merging back again is not relevant, I think this simple command could be sufficent:

cat a*.txt | split -l 20
1
  • 1
    Thank you for your correct answer. Raffa's answer was more complete that is why I had to accept that as a solution.
    – pebox11
    Jul 17, 2019 at 22:29

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.