-1

I've 2 folders. Folder 1 has some files arranged in some sub folders. Folder 2 has same-filenamed-files (but different size) but not arranged in any sub folder. I want to arrange folder 2 files like folder 1. Is there a quick way to do that? I'm using Linux.

kan
  • 1
  • 3
    There's no existing tool to do what you want. You'll have to write a script. Hint: a simple algorithm to do this is to iterate through the sub-directories of Folder1 and, for each sub-directory: 1. create the same subdir in Folder2; 2. get the list of filenames in the sub-dir 3. move the same filenames in Folder2 to Folder2/subdir. Write your script as a dry-run (i.e. only print what it would do) until you're sure it's going to work correctly. If you write it in shell, remember to double-quote your variables. – cas Apr 14 '22 at 13:30
  • Does your system offer the tree command? – RudiC Apr 14 '22 at 18:19
  • yes, using Linux. – kan Apr 16 '22 at 04:48

1 Answers1

0

Mayhap something along these lines will do:

cd "$folder1"
echo "mkdir -p " $(find . -type d | tee /tmp/dirs) > /tmp/tmpscript
find . | awk -F"/" 'FNR == NR {DIR[$0]; next} !($0 in DIR) && !(NF == 2) {print "cp \"" $NF "\" \"" $0"\""}' /tmp/dirs - >> /tmp/tmpscript
cd "$folder2"
less /tmp/tmpscript
# rm /tmp/dirs /tmp/tmpscript

It creates a temporary script that first creates the subdirectory structure under folder2, then cps every single file from there into its pertaining subdir. Change cp to mv if need be. Scrutinize the tmpscript first (with e.g. less) before actually running it. As a last step, the temp files should be removed. No effort has been done to optimize the copying action; every single file is handled on its own. Improvements could be done like collecting all files bound to one subdir into one copy command.

RudiC
  • 8,969