0

I have a directory structure produced during archiving. The archiving system can't be changed. Whenever a duplicate directory occurs the archiver appends an integer suffix to the directory name. My goal is to collapse these so that there is only one directory per name (no suffixes) and sub-directories are merged.

# Current Structure
/
+-- Folder_Name/
    +-- 20170913/
        +-- File One
+-- Folder_Name_1/
    +-- 20170913/
        +-- File One
        +-- File Two
+-- Folder_Name_2/
    +-- 20170915/
        +-- File Three
+-- Folder_Name_3/
    +-- 20170918/
        +-- File Four

# What I would like to achieve
/
+-- Folder_Name/
    +-- 20170913/
        +-- File One
        +-- File Two
    +-- 20170915/
        +-- File Three
    +-- 20170918/
        +-- File Four

Lots of folders are created during archiving (it is an ultrasound machine saving a monthly backup of all images) and there are often duplicates produced. At the same time I am using ImageMagick to convert the bitmap files to png in order to keep file sizes down.

I have tried to do this with rsync using the --remove-source-files option, but I haven't been able to find a way to match these directories for rsync (I don't think it supports regex). I then tried to use find to pipe the output into rsync but can't work out a) the regex that would achieve that; and b) how to provide the suffixed and un-suffixed directory names. I am thinking I need to use variables for this, but am not experienced with shell scripting.

Am I taking the right approach here? If so, could someone point me to a resource or tutorial that can walk me through setting this up? If not then what tools should I be looking at using?

0

I'm pretty sure a simple

cp -R Folder_Name_*/* Folder_Name

will do all the merge for you. After you checked the result, you can remove the source directories.

If you have multiple folders to merge, build a for loop around it:

for folder in *_1; do
  folder="${folder%_*}"
  cp -R "$folder"_* "$folder"
  # rm -rf "$folder"_*
done

(rm commented out until verified)

  • Thanks for the response but it would be too time consuming to do this with each folder name that matched that pattern. Each folder name equates to a patient name (John_Doe, John_Doe_1, Jane_Smith, Jane_Smith_1, etc). There are hundreds of these. – walkerbox Sep 19 '17 at 11:51
  • You could add a loop to @Philippos solution. You need to select the actual names: ls | grep -v "_[0-9]*$" then run the command over those names: for i in $( ls | grep -v "_[0-9]*$" ); do cp -R $i_*/* $i; done – Dave Jennings Sep 19 '17 at 11:59
  • Thanks folks. Works perfectly. I've tweaked Philippos code very slightly to make the cp cp -R "$folder"_*/. "$folder" to make sure the Folder_Name_* doesn't get placed within Folder_Name. – walkerbox Sep 19 '17 at 12:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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