I have almost 6000 directories with thousands of files:


and I want to move all .txt files in the all/txt folder. I have used this command:

mv **/*.txt txt

but I get this error:

bash /bin/mv arg list too long

How can I do?

  • 1
    if you just move all .txt files from your example to a common folder, the ones with the same basenames (e.g., 1.txt) will overwrite each other. How do you want to resolve this conflict? – stefan Apr 3 '18 at 13:32
  • @stefan I could rename it with 1-1.txt, 1-2.txt, 1-3.txt, ecc – xRobot Apr 3 '18 at 14:08

find solution:

find all -type f -name "*.txt" ! -path "all/txt/*" -exec echo mv -t all/txt '{}' \;
| improve this answer | |

Maybe try this. To avoid moving files just moved to all/txt, move them to a new txt directory outside of all, then move txt under all. Here we go:

$ mkdir txt

The next one will only print all move commands. Check that you like what you see:

$ find all | sed -rn 's#^all/recup_dir.([^/]*)/([^/]*).txt$#mv -n "&" "txt/\1-\2.txt"#p'
mv -n "all/recup_dir.20/2.txt" "txt/20-2.txt"
mv -n "all/recup_dir.20/1.txt" "txt/20-1.txt"
mv -n "all/recup_dir.19/5.txt" "txt/19-5.txt"
mv -n "all/recup_dir.19/4.txt" "txt/19-4.txt"

When satisfied, run them by appending | sh:

$ find all | sed -rn 's#^all/recup_dir.([^/]*)/([^/]*).txt$#mv -n "&" "txt/\1-\2.txt"#p' | sh

then put txt where it belongs:

$ mv txt all
| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.