0

This question already has an answer here:

I have almost 6000 directories with thousands of files:

all/recup_dir.1/1.txt
all/recup_dir.1/2.jpg
...
all/recup_dir.5987/1.txt
all/recup_dir.5987/2.txt
...

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?

marked as duplicate by Kiwy, Hauke Laging, Timothy Martin, Jesse_b, Bananguin Apr 6 '18 at 10:11

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 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
1

find solution:

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

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

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