Recursive copy folder script under Ubuntu - you can copy the selected folder and its children to the target path

Virtue wins talent, which is called a gentleman; Talent wins virtue, which is called a villain—— "Zizhi Tongjian · Tang Ji"

I Antecedents - reduce duplication of effort

Previously, when deploying the running environment of development software in ubuntu, it was necessary to copy and paste the contents in different folders to the specific location of the new machine. In order to solve the problem of wasting time by repeatedly placing files manually, we studied how to complete the work with scripts; Let's briefly introduce the script function first.

II Consequences - no longer worry about ubuntu multi folder multi-path replication

The following script can be used to recursively copy the folder and its children to be copied, that is, all the contents in the source folder can be completely copied to the target folder. If multiple source file paths are copied to multiple target paths, you need to configure multiple pieces of the same program or slightly modify the program (the method will be mentioned later).

III Experience - take a folder (including children) copied to the target path as an example

#!/bin/bash
#cd /
#Source folder path
home_doc_path="/home/exe"
#Destination folder path
goal_doc_path="/opt/exe"

function ergodic_file()
{
#Judge whether the target folder exists. If it does not exist, create it
   if [ ! -d ${goal_doc_path} ]; then
      mkdir -p ${goal_doc_path}
      #Print folder creation information
      echo "make new: ${goal_doc_path}"
   fi

#Copy main process
   #Determine whether the source path file exists
   if [ -d ${home_doc_path} ]; then
      #Traverse the sub file / sub folder under the extracted folder and assign it to file_exe
      for file_exe in `ls ${home_doc_path}`; do
         #Print the name and full path of the sub file / sub folder, which has been blocked
         #echo "edc1: ${file_exe}"
         #echo "edc2: ${home_doc_path}"/"${file_exe}"
         #Assign the full pathname of the source file to total_file
         total_file=${home_doc_path}"/"${file_exe}
         #If the source file just traversed is a folder, execute the then program, otherwise execute the else program
         if [ -d ${total_file} ]; then
            #Create the folder containing the source file at the destination path
            mkdir -p ${goal_doc_path}"/"${file_exe}
            #Print destination folder path information
            #echo "edc3: ${goal_doc_path}"/"${file_exe}"
            #Recursively copy folders$ The first after is CP_ doc_ The first parameter of file is $1, and the second parameter is $2
            cp_doc_file ${total_file} ${file_exe}
         else
            #Copy corresponding files directly
            cp ${home_doc_path}"/"${file_exe} ${goal_doc_path}
            #Print copied file path
            #echo "edc4: ${home_doc_path}"/"${file_exe}"
         fi 
      done
   else
      echo "home_exe_path do not have"
   fi

#config library file
sudo ldconfig
sudo ldconfig
}

function cp_doc_file()
{
   #Print parameter 1 and parameter 2
   #echo "cef1: $1"
   #echo "cef2: $2"
   #Assign the content under parameter 1 folder to file
   for file in `ls $1`; do
       #Print path information
       #echo "cef3: $1"/"${file}"
       #Assign the full path of the file or folder to be copied to total_file
       total_file=$1"/"${file}
       #Determine whether the source folder is missing files
       if [ -d ${total_file} ]; then
          #Create a folder that does not exist under the destination folder
          mkdir -p ${goal_doc_path}"/"${2}"/"${file}
          #Print folder path information
          #echo "cef4: ${goal_doc_path}"/"${2}"/"${file}"
          #Recursive replication
          cp_doc_file ${total_file} $2"/"${file}
       else
          #Copy file contents directly
          cp ${total_file} ${goal_doc_path}"/"${2}
          #Print path information copied to the destination file
          #echo "cef5: ${total_file}"
       fi
   done 
}
#Script call run function
ergodic_file

IV Usage Note and extension

  1. When in use, the statements that print information can be unshielded or masked as needed.
  2. You can copy the above script program directly to the ". txt" file, and then change the suffix of the file to ". sh".
  3. Before running the script, you need to use "chmod +x file name" to set the script as a runnable file.
  4. If the copied target path requires advanced permissions, you can use "sudo. / filename" to run it. If you don't need it, you can directly use ". / filename" to run it.
  5. When you need to copy multiple source path files to multiple target paths, in addition to the methods mentioned above, you can also slightly modify the "ergodic_file" function to contain two input variables (one is the source folder path and the other is the target folder path), and then call the parametric function ergodic for many times_ File.

You are welcome to comment or exchange private messages in the comment area.

Keywords: Linux shell Ubuntu bash

Added by markmax33 on Wed, 16 Feb 2022 18:43:20 +0200