When we are processing a large number of files in Linux, such as copy, move, delete... if the number of files is really too large and we use wildcard character *
to match files, then we are likely see the following error:
Argument list too long
As shown in the error message, this is caused by too many parameters of the command we ordered (after all, it is a lot of files).
A simple and intuitive way is to use us to process these files recursively, or we can directly use the xargs
command to solve it-this is also one of the most recommended methods.
xargs
is an instruction that accepts parameters but executes the "rear command". The easiest way is to use find or ls to display the files, and then use xargs to execute the commands one by one. Several common situations are provided below:
1. We want to delete all .log files in folder A
find A/ -name "*.log" | xargs -i rm {}
2. We want to move all the .log files under folder A to folder B
find A/ -name "*.log" | xargs -i mv {} B/
3. We want to copy all the .log files under folder A to folder B
find A/ -name "*.log" | xargs -i cp {} B/
In this way, we can successfully avoid the Argument list too long problem.
References
- https://stackoverflow.com/questions/11289551/argument-list-too-long-error-for-rm-cp-mv-commands
- https://unix.stackexchange.com/questions/45583/argument-list-too-long-how-do-i-deal-with-it-without-changing-my-command
- https://askubuntu.com/questions/1048964/deleting-many-files-results-in-argument-list-too-long
- https://github.com/firebase/firebase-ios-sdk/issues/4577
- https://superuser.com/questions/391811/argument-list-too-long-error-for-rm-rf-on-a-directory-with-4000-files?rq=1
Read More
- [Solved][Linux] welcome to emergency mode! after logging in type “journalctl -xb” to view system logs “systemctl reboot” to reboot “systemctl default” to try again to boot into default mode. give root password for maintenance
- [Solved] Linux system cannot connect to Android Device
- [Solved][Linux] An error "File too large" happened when we put data in USB flash drive