FSL Merge in Nipype crashes if too many files

I need to merge around 5000 Nifti 3D files into a 4D.
When i use FSL merge in Nipype, it seems that there is a limit to the number of files you can input without crashing.
Above roughly 2200 files I get the following error:

/opt/miniconda-latest/envs/neuro/lib/python3.6/subprocess.py in _execute_child(self, args, executable, preexec_fn, close_fds, pass_fds, cwd, env, startupinfo, creationflags, shell, p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite, restore_signals, start_new_session)
1362 if errno_num == errno.ENOENT:
1363 err_msg += ': ’ + repr(err_filename)
→ 1364 raise child_exception_type(errno_num, err_msg, err_filename)
1365 raise child_exception_type(err_msg)
1366

OSError: [Errno 7] Argument list too long: ‘/bin/sh’

My Ulimit is set to unlimited so it doesn’t seem to be a bash problem.
Of course I could cut in sub-blocks and then concatenate those blocks but I wonder if I could fond a better workaround first and also understand why this problems occur.

Thanks!

What happens if you type getconf ARG_MAX from the terminal. I wonder if you are exceeding your ARG_MAX. You may want to use a simple Python script instead of fslmerge. Be aware that a lot of tools may have issues with Gzip files that exceed 2Gb. For such a huge dataset it may be better to save as uncompressed .nii instead of a compressed .nii.gz.

getconf ARG_MAX gives me 4611686018427387903, which seemed to me to be way above what I needed.

Personally, I would try NiBabel’s concat_images.

If you want to stick with fslmerge, you should ask your question on the FSL jiscmail - the FSL developers are extremely responsive.