Linux – Argument list too long for xargs/exec

bashexecfindlinuxxargs

I'm working on a CentOS server and I have to move around and cat together millions of files. I've tried many incarnations of something like the below, but all of them fail with an argument list too long error.

command:

find ./ -iname out.* -type f -exec mv {} /home/user/trash
find ./paramsFile.* -exec cat > parameters.txt 

error:

-bash: /usr/bin/find: Argument list too long
-bash: /bin/cat: Argument list too long

or

echo ./out.* | xargs -I '{}' mv /home/user/trash
(echo ./paramsFile.* | xargs cat) > parameters.txt  

error:

xargs: argument line too long
xargs: argument line too long              

The second command also never finished. I've heard some things about globbing, but I'm not sure I understand it completely. Any hints or suggestions are welcome!

Best Answer

You have multiple mistakes. You should escape the * globbing. You have to put {} between quotes (for filename security), and you have to end the -exec with \;.

find ./ -iname out.\* -type f -exec mv "{}" /home/user/trash \;
find -name ./paramsFile.\* -exec cat "{}" >> parameters.txt \;

The problem here is that * is matching all the files in your directory, thus giving you the error. If find locates the files instead of shell globbing, xargs gets individual filenames that it can use to construct lines of the correct length.

Related Question