Back to Blog

MV: Argument List Too Long

Mon, 21 Oct 2024, by Syrsly

If you ever have to move files via command line on a LInux server, you might just use something simple like this:

mv [absolute path to old directory] [absolute path to destination directory]

However, once you have more than a few thousand files in a directory, you'll likely run into the "Argument list too long" error. This error will imply your command has too many arguments, but what it really means is the mv command can't loop through that many files. As a workaround, you can use the following command:

find [absolute path to old directory] -type f | xargs -i mv "{}" [absolute path to destination directory]

The command basically is find files and pipe the results into mv command using xargs. This works quite well but takes quite a bit longer than a straight mv command. You can count the files in the old directory in a separate session using the following commands:

cd [absolute path to old directory]
ls -1 | wc -l

The count will indicate how much is left to go through in the other mv command. This can also be helpful when troubleshooting which directories have too many files in them. I tend to avoid putting everything into a single directory whenever possible so we don't get stuck in a situation where we don't know which files to keep and can't handle listing out the files easily.