0

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?

xRobot
  • 101
  • 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 03 '18 at 13:32
  • @stefan I could rename it with 1-1.txt, 1-2.txt, 1-3.txt, ecc – xRobot Apr 03 '18 at 14:08

2 Answers2

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
stefan
  • 1,111