argument list too long

How to Fix “mv: Argument List too Long” Error

Every Linux system offers mv command to move files and folders from one location to another on each system. But sometimes, while using mv command, you may get the error “mv: argument list too long”. In this article, we will learn why we get this error and how to fix this error.

How to Fix “mv: Argument List too Long” Error

We get this error when we pass too many arguments to mv command. There is a system variable ARG_MAX that defines the maximum character length that can be passed to any shell command, including mv command. When we end up passing more arguments than what is specified here, we get this error. You can find the value of ARG_MAX with the following shell command.

$ getconf ARG_MAX

There are several ways to solve this problem.

1. Using wildcard characters

If you want to move multiple files with similar filenames to a single location, you can use wildcard characters to specify them in your mv command. For example, let us say you have 200 files with filenames file1.txt, file2.txt…file200.txt in /home/ubuntu and you want to move them to /home/data, then you can do so with the following command.

$ mv /home/ubuntu/file*.txt /home/data

In the above case, you don’t need to specify each filename separately but can cover them using wildcard characters.

You can also use wildcard characters to specify files present in different locations. Here is the command to move all .txt files from /home/ubuntu and /home/projects to /home/data.

 $ mv /home/ubuntu/*.txt /home/projects/*.txt /home/data

2. Using recursive copy

If you have too many files and folders in your present working directory and want to move them all to another destination, you can simply move up a folder and use -r option with mv command to move all the contents of your folder to the destination. Let us say you have more than 100 files & folders in your present working directory, you can easily copy them all to /home/data with the following command.

$ mv -r ../* /home/data

3. Using find with exec

find command allows you to find files & folders using various conditions, on your system. It also allows you to execute commands on the found files, using -exec option.

Here is the command to find all *.txt files on your system and move them to /home/data.

$ find . -name '*.txt' -exec mv {} /home/data/ \;

The above command will also navigate your present working directory’s subdirectories to find files. If you want to restrict find command to only the present working directory, then you can use -maxdepth option as shown below.

$ find . -name '*.txt' -maxdepth 1 -exec mv {} /home/data/ \;

4. Using find with xargs

Similarly, you can also use find command with xargs to move multiple files at once. Here is the command to find and move all *.txt files to /home/data.

$ find . -name '*.txt' | xargs mv --target-directory=/home/data/ 

In the above command, find command’s output is piped to xargs command, that creates separate mv statements for each file in the find command’s output, and executes them one by one.

In this article, we have learnt how to fix mv : argument list too long error. You can use these commands in almost every Linux system since they are universally available.

Also read:

How to Repair MySQL Databases & Tables
How to Optimize MySQL Tables
cURL command to call REST APIs
How to Remove WWW from URL in Apache
How to Delete Git Tags

Leave a Reply

Your email address will not be published. Required fields are marked *