Blog / Linux/ How to Fix 'Argument list too long' Error with mv Command in Linux (CentOS)

How to Fix 'Argument list too long' Error with mv Command in Linux (CentOS)

Linux系统(CentOS)解决mv命令“Argument list too long”参数过长错误

Problem Description

On Linux systems like CentOS, when using the mv command to move a directory containing a large number of files (e.g., from /old to /new), you may encounter the error: Argument list too long.

Root Cause

This error typically occurs because the source directory (e.g., /old) contains too many files. When executing a command like mv /old/* /new/, the shell attempts to pass all matched filenames as arguments to the mv command at once, exceeding the system kernel's limit for the total length of command-line arguments.

Solutions

The core idea is to avoid passing all filenames at once, instead processing files individually or in batches. Here are several reliable methods:

Method 1: Use find with -exec

This is the most direct and universal approach. The find command executes the mv command separately for each file found, bypassing the argument length limit.

find /old -type f -exec mv {} /new ;

Explanation:

  • find /old: Search within the /old directory.
  • -type f: Limit to regular files. Omit this if you need to move everything (including subdirectories).
  • -exec mv {} /new ;: Execute mv for each found file. {} represents the current file path, and ; terminates the -exec command.

Method 2: Use find with xargs

The xargs command converts standard input into arguments in batches, offering better efficiency when combined with find.

find /old -type f -print0 | xargs -0 mv -t /new

Explanation:

  • -print0 and -0: These options use null characters to separate filenames, safely handling names with spaces or special characters.
  • mv -t /new: The -t option specifies the target directory (/new), so all arguments from xargs are treated as source files.

Method 3: Use a Loop with Wildcards (Bash)

In Bash, you can use a loop to process files incrementally.

for file in /old/*; do
    mv "$file" /new/
done

If the wildcard expansion itself is problematic, enable globstar for more control:

shopt -s globstar
for file in /old/**/*; do
    [[ -f "$file" ]] && mv "$file" /new/
done

Important Notes

  1. Target Directory: Ensure /new exists before running the command.
  2. Permissions: The user must have read access to source files and write access to the target directory.
  3. Verification: Test with echo first (e.g., find /old -type f -exec echo {} ;) to see which files would be moved.
  4. Cross-Filesystem Moves: If /old and /new are on different filesystems, mv performs a copy-and-delete, which can be slow. For massive files, consider rsync or a tar pipeline.

All methods effectively solve the Argument list too long error. Methods 1 and 2 are recommended for their robustness and wide applicability.

Post a Comment

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