Move files and folders whilst retaining directory structure

bash

#1

I need to move a list of directories (and all their contents) from one location to another whilst preserving the tree structure. As an example I need to move:

/mnt/sda1/A/Amorphis/Silent Waters

to

/mnt/sda1/needstagging/A/Amorphis/Silent Waters

As I need to process a list of folders I’ve tried to automate this using a bash script:

#!/bin/bash
 
# populate ./dirstomove.list with a list of folders you want moved into target folder
#
 
dirstomove=./dirstomove.list
 
clear
echo "Files to process:  " $(wc -l $dirstomove)
echo ""
while [ -s $dirstomove ] ; do
    currentdirtomove=$(head -1 $dirstomove)

    echo $currentdirtomove > /tmp/currentdirtomove
    sed 's|^.........||' /tmp/currentdirtomove > /tmp/targetdir
    targetdir=/mnt/sda1/needstagging`cat /tmp/targetdir`
    echo $targetdir

#    now move currentdirtomove to targetdir
#    mv "/$currentdirtomove" "$targetdir"



    # remove current file from list
    sed -i.prev '1d' $dirstomove
done

The part I’m not sure of is how to get the full directory path moved. Any tips?


#2

Can you post sample dirstomove.list file?

cat dirstomove.list 

#3

Here’s an excerpt:

mnt/ntfs/d3/zConsolidated/tagged/anisa/various - bump xx/
mnt/ntfs/d3/zConsolidated/tagged/2017 12 25//VA/Various - Mix 04/
mnt/ntfs/d2/zConsolidated/rippedcd/2017 12 25/Various - Mix 05/

This is what I ended up doing:

#!/bin/bash

# populate ./dirstomove.list with a list of folders you want moved into target folder
#

dirstomove=./dirstomove.list

clear
echo "Files to process:  " $(wc -l $dirstomove)
echo ""
while [ -s $dirstomove ] ; do
    currentdirtomove=$(head -1 $dirstomove)

    echo $currentdirtomove > /tmp/currentdirtomove
    sed 's|^.........||' /tmp/currentdirtomove > /tmp/targetdir

    targetdir=/mnt/tmpsata/zdupes/`cat /tmp/targetdir`
    targetdir=`dirname "$targetdir"`

    echo SRC: /$currentdirtomove
    echo DST: $targetdir

    mkdir -p "$targetdir"
    mv "/$currentdirtomove" "$targetdir"/

    # remove current file from list
    sed -i.prev '1d' $dirstomove
done