One of my development server 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 same system. When tried to move file with mv command, received the following error.
-bash: /bin/mv: Argument list too long
The “Argument list too long” error, generally comes, when we passed a large number of parameters to single command. A system variable ARG_MAX defines the Maximum Character Length of Arguments In a shell command.
The Solution
The quick solution is to use xargs command line utility or find command with -exec … {}. Both commands breaks a large command in smaller and complete the job without errors.
- Using find with xargs – The following command will move all files with .txt extension to destination directory.
find . -name '*.txt' | xargs mv --target-directory=/path/to/dest_dir/
- Using find with exec – You can also use exec to perform the same task.
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 current directory only use
-maxdepth
followed by limit number to sub-directories.find . -name '*.txt' -maxdepth 1 -exec mv {} /path/to/dest_dir/ \;
You can find the max limit with command getconf ARG_MAX
on shell.
1 Comment
Great info. That will be useful for my website.