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

How can I copy many files?

 

It tells me -bash: /usr/bin/mv: Argument list too long

Add Comment
1 Answer(s)
The error message `-bash: /usr/bin/mv: Argument list too long` typically occurs when you are trying to move a large number of files using the `mv` command in a way that expands to an argument list beyond the system's limit. This is often because of using a wildcard like `*` to match files, resulting in a command line that's too long for the shell to handle. To handle moving (or copying) many files without hitting this limit, you can use several different approaches: ### 1. Using `find` with `xargs` The `find` command can generate a list of all files that match certain criteria, and `xargs` can be used to execute a command on that list in a way that avoids the argument list limit: ```bash find . -type f -name '*' -print0 | xargs -0 mv -t /target/directory ``` - The `-print0` option for `find` and the `-0` option for `xargs` ensure that filenames with special characters (such as spaces) are properly handled. - The `-t` option for `mv` specifies the target directory. ### 2. Using `find` with `-exec` ```bash find . -type f -exec mv {} /target/directory \; ``` - Here, `{}` is replaced by the path of each file found. - This command will execute `mv` for each file individually, so it can be slower if you have a very large number of files. ### 3. Use a Loop ```bash for f in *; do mv "$f" /target/directory; done ``` - This loop will go through each file one by one and move it. - Note the use of double quotes around `$f` to properly handle file names with spaces. ### 4. Using `rsync` `rsync` is another powerful tool primarily used for copying and syncing files both locally and remotely, but it can also be used in place of `mv` like so: ```bash rsync -av --remove-source-files ./ /target/directory/ ``` - The `-av` flag enables archive mode and verbosity. - The `--remove-source-files` option deletes the source files after copying, effectively mimicking the move operation. ### 5. Increase the Argument List Size In some cases, you may increase the allowed size of the argument list. The maximum size on modern systems can be quite large, but if you need to adjust it, you can typically do so with the `ulimit` command. However, this may only be a temporary fix or not work at all if you frequently deal with a large number of files. Using one of the first four methods is the recommended approach to deal with the limitation and allows for better handling of a large set of files without modifying system settings. Remember to replace `/target/directory` with the actual directory where you want to move your files.
Answered on July 26, 2024.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.