One of my development servers contains millions of files under a single directory. To free the disk space, we decided to move to them a new folder created on another disk attached to the same system. When tried to move file with mv command, received the following error.

Advertisement

-bash: /bin/mv: Argument list too long

An argument list too long is a common problem with a bash that can happen when you have long command line parameters or arguments. You start running a script and it throws errors like “invalid command” or “too long”. The reason for this is that the shell is trying to read past the end of your argument list and there is no end of the input pipe to wait for. A system variable ARG_MAX defines the Maximum Character Length of Arguments In a shell command.

The Solution’s

The quick solution is to use xargs command line utility or find command with -exec … {}. Both commands break a large command into more minor and complete the job without errors.

  • find with xargs

    The following command will move all files with the “.txt” extension to the target directory. Here find will search all files with the “.txt” extension in the current directory as subdirectories. PIPE (|) will take the standard output of the find command and send it to the mv command as standard input. then mv will move files to the target directory one by one.

    find . -name '*.txt' | xargs mv --target-directory=/path/to/dest_dir/ 
    
  • find with exec

    Instead of using xargs, we can also use -the exec command. Here find will search files and exec will execute the mv command for each file one by one and move the file to the destination directory.

    find . -name '*.txt' -exec mv {} /path/to/dest_dir/ \;
    

    The default above commands will navigate recursively to the sub-directories. To limit the find to the current directory only use -maxdepth followed by a limit number to sub-directories.

    find . -name '*.txt' -maxdepth 1 -exec mv {} /path/to/dest_dir/ \;
    

The “Argument list too long” is a common error while handling a large number of files in bash scripts. You can find the max limit with the command getconf ARG_MAX on the shell.

Share.

1 Comment

Leave A Reply