Centos: Merge directories while removing a common subdirectory in all subs

36 Views Asked by At

Very weird and specific question I know but would save me a big headache.

Is it possible to merge directories while removing a specific sub in each sub but keeping all files? Example

I have

/first/1/files/1.jpg, 2.jpg, 3.jpg
/first/2/files/1.jpg, 2.jpg, 3.jpg

I also have

/second/1/3.jpg, 4.jpg, 5.jpg
/second/2/3.jpg, 4.jpg, 5.jpg

I want to merge /first into /second while also removing the inbetween /files/ directory in /first but keeping all the jpgs. On top of that, I don't want 3.jpg to be overwritten in /second/

1

There are 1 best solutions below

3
On

If timestamps don't matter then you could do something like:

find /second -name '*.jpg' -exec touch +
for fdir in /first/*/files; do
    sdir=${fdir%/files};
    sdir=${sdir#/tmp/first};
    sdir=/tmp/second${sdir};
    cp -u "$fdir/"* "$sdir/";
done

though that's not exactly what I'd call a good solution.

You could also do this manually testing for existing files while looping and copying like this:

for ffile in /first/*/files/*.jpg; do
    # Expand the variable and delete '/files'
    sfile=${ffile/\/files}
    # Expand the variable and remove the '/first' prefix
    sfile=${sfile#/tmp/first}
    # Prepend the new '/second' direcory.
    sfile=/second${sfile}
    # If the exist do nothing. It if doesn't then copy.
    [ -f "$sfile" ] || cp -u "$ffile" "$sfile"
done

I feel like there is probably a clever way to do this with rsync or cpio but I don't know what that is.