Home > Mobile >  How to copy specific files from multiple directories into one directory
How to copy specific files from multiple directories into one directory

Time:12-02

Structure of my directories and files is:

Folder1:
        File1
        Folder2:
               File2
               File3
               File4
               Folder3:
                       File5
                       File6
                       File7
                       file8

What i want to acheive is to copy File1, File2, File6 and File7 into one Folder4. I need a script for that because i have multiple sets like that and i want to have a set made of different Folder4 in a folder 5:

Folder5:
        Folder4a:
                File1, File2, File6, File7
        Filder4b:
                File1, File2,File6, File7
      

etc. all the files have the same prefix but different extensions and contain different informations that are linked together.

Thanks a lot for any hepl!

#!/bin/bash

export output_dir='./Folder5'

if [ ! -e ${output_dir} ]
then
    echo ${output_dir}
    mkdir ${output_dir}
fi


for i in `ls file1`
do
    export folder=`echo ${i} | awk -F . '{print $1}'`
    if [ ! -f ${folder} ]
    then
        mkdir ./${output_dir}/${folder}
        cp ${i} ./${output_dir}/${folder}
    fi
done

this created Folder5, and in Folder1 where my File1 is it copies its prefix creates the Folder4 named as the prefix of file1 and copies file1 into that folder. And now i have an issue how do i access other files in subfolders and how do i copy them into folder4.

CodePudding user response:

Using the shell and find, Something like:

#!/usr/bin/env bash

##: Save the files that needs to be move in an array.
files_to_move=(
  file1
  file2
  file6
  file7
)

##: Save the name of folder4 in a variable and export it
##: In order for `find` to use it.
destination=folder4
export destination

##: Set the files to be moved in a format that find understands.
for i in "${!files_to_move[@]}"; do
  files =(-o -name "${files_to_move[$i]}")
done

##: Execute find and feed the file names to it and copy it
##: To folder4 aka "$destination"
find . -type f \( "${files[@]:1}" \) -exec sh -c '
  cp -v -- "$@" "$destination"' _ {}  

##: Save folder5 in a variable and create if it does not exists.
final_destination=folder5
[[ ! -d "$final_destination"  ]] &&
mkdir -vp "$final_destination"

##: Copy folder4 in folder5 with the desired folder names.
cp -rv "$destination/" "$final_destination/${destination}a"
cp -rv "$destination/" "$final_destination/${destination}b"


Tested against the files/directories created by the script below.

#!/usr/bin/env bash

mkdir -p /tmp/testing
cd /tmp/testing || exit
mkdir -vp folder{1..4}
touch folder1/file1
touch folder2/file{2..4}
touch folder3/file{5..8}

And running the first script inside /tmp/testing

CodePudding user response:

#!/usr/bin/env bash

mkdir -p /tmp/testing cd /tmp/testing || exit mkdir -vp folder{1..4} touch folder1/file1 touch folder2/file{2..4} touch folder3/file{5..8}

i tried this one and the structure of the files is not like the one I have. In my case, folder3 is inside folder2. this script creates folder 2-4 inside folder1.

and entering subfolders, then copying files from it and then exiting and moving to another subfolder to copy is the part I have issues with. In the code I posted with my question I am only able to copy file 1 wich is in the most top directory.

  • Related