Bash to copy subfolder and files to matching directory


Login or Register to Reply

 
Thread Tools Search this Thread
# 1  
Old 1 Week Ago
Bash to copy subfolder and files to matching directory

The bash executes but returns no results and the set -xv showed while the $run variable in blue, was extracted correctly, the $match value in green, was not, rather both values in home/cmccabe/Desktop/f1 were extracted not just the matching.

There will always be an exact match from the $run to the $match but there may be more than one. In all cases I am trying to cp the $run to the corresponding $match. When a match is found the contents of the $run subfolder and .zip and copied to $match. I havent added this as I am not sure how. Thank you Smilie.


run directory ---- this is $run ----
Code:
R_2019_01_23_13_22_58_user_S5-0271-96-v5.6_Oncomine_Childhood_Cancer_Research_DNA_and_Fusions   --- top level ---
   f1  --- subfolder under R_2019_01_23_13_22_58_user_S5-0271-96-v5.6_Oncomine_Childhood_Cancer_Research_DNA_and_Fusions ---
    zip --- zip file in f1 ---
   f2   --- subfolder under R_2019_01_23_13_22_58_user_S5-0271-96-v5.6_Oncomine_Childhood_Cancer_Research_DNA_and_Fusions ---
    zip --- zip file in f2 ---
   f3   --- subfolder under R_2019_01_23_13_22_58_user_S5-0271-96-v5.6_Oncomine_Childhood_Cancer_Research_DNA_and_Fusions ---
    zip --- --- zip file in f3 ---

match directory ---- this is $match ----
Code:
R_2019_01_23_13_22_58_user_S5-0271-96-v5.6_Oncomine_Childhood_Cancer_Research_DNA_and_Fusions
R_2019_01_23_13_22_58_user_S5-0271-96-v5.6_Oncomine_Childhood_Cancer_Research_DNA_and_Fusions

Code:
# grab run directory
set -xv
for dir in /home/cmccabe/medex.logs/ponc/*/; do
  if [ -d $dir ]; then
     run=$(basename $dir)
  fi
# find match and copy run
cd /home/cmccabe/Desktop/f1
match=$(find * -maxdepth 1 -type d)
   if [ $run = $match ]
    then
   #cp -v $run /home/cmccabe/Desktop/f1/$match
  fi
done

bash [too many arguments

set -xv

Code:
+ '[' R_2019_01_23_13_22_58_user_S5-0271-96-v5.6_Oncomine_Childhood_Cancer_Research_DNA_and_Fusions = R_2019_01_23_13_22_58_user_S5-0271-93-v5.6_Oncomine_Childhood_Cancer_Research_DNA_and_Fusions R_2019_01_23_13_22_58_user_S5-0271-96-v5.6_Oncomine_Childhood_Cancer_Research_DNA_and_Fusions ']'


Last edited by cmccabe; 1 Week Ago at 10:26 AM.. Reason: fixed format
# 2  
Old 1 Week Ago
Your code typo corrected. It is POSIX shell compliant, and it works with Bash too.

Code:
#!/bin/sh

# POSIX shell compliant.

# grab run directory.
set -xv

for dir in /home/cmccabe/medex.logs/ponc/*/ ; do


  if [ -d "$dir" ] ; then
     run="$(basename "$dir")"
  fi


  # find match and copy run.
  # the program exits 1, in case the directory can not be accessed, or does not exist.
  cd /home/cmccabe/Desktop/f1 || exit 1


  # https://github.com/koalaman/shellcheck/wiki/SC2035
  match="$(find ./* -maxdepth 1 -type d)"


  if [ "$run" = "$match" ]
  then
    # cp -v "$run" /home/cmccabe/Desktop/f1/"$match"
  fi


done

This User Gave Thanks to johnprogrammer For This Post:
cmccabe (1 Week Ago)
# 3  
Old 1 Week Ago
I think I finished your program. Test it, and tell us if it works.

Code:
#!/bin/sh

# POSIX shell compliant.

# grab run directory.
set -xv

for dir in /home/cmccabe/medex.logs/ponc/*/ ; do


  if [ -d "$dir" ] ; then
     run="$(basename "$dir")"
  fi


  # the program exits 1, in case the directory can not be accessed, or does not exist.

  cd /home/cmccabe/Desktop/f1 || exit 1


  # https://github.com/koalaman/shellcheck/wiki/SC2035
  
  findOutput="$(find ./* -maxdepth 1 -type d)"


  # find match and copy run.

  for match in $findOutput
  do
  
    if [ "$run" = "$match" ]
    then

      cp -Rv "$run" /home/cmccabe/Desktop/f1/"$match"
  
    fi
  
  done


done

exit 0


Last edited by johnprogrammer; 1 Week Ago at 07:11 PM..
This User Gave Thanks to johnprogrammer For This Post:
cmccabe (1 Week Ago)
# 4  
Old 1 Week Ago
The script executes and moves the copies the matching directory (parent) only. The sub-folders within it are not copied over.
So the filename in green below is copied but not the subdirectories within it. Thank you Smilie.

Code:
R_2019_01_23_13_22_58_user_S5-0271-96-v5.6_Oncomine_Childhood_Cancer_Research_DNA_and_Fusions   --- top level ---
   f1  --- subfolder under R_2019_01_23_13_22_58_user_S5-0271-96-v5.6_Oncomine_Childhood_Cancer_Research_DNA_and_Fusions ---
    zip --- zip file in f1 ---
   f2   --- subfolder under R_2019_01_23_13_22_58_user_S5-0271-96-v5.6_Oncomine_Childhood_Cancer_Research_DNA_and_Fusions ---
    zip --- zip file in f2 ---
   f3   --- subfolder under R_2019_01_23_13_22_58_user_S5-0271-96-v5.6_Oncomine_Childhood_Cancer_Research_DNA_and_Fusions ---
    zip --- --- zip file in f3 ---

match directory ---- this is $match ----
Code:
R_2019_01_23_13_22_58_user_S5-0271-93-v5.6_Oncomine_Childhood_Cancer_Research_DNA_and_Fusions
R_2019_01_23_13_22_58_user_S5-0271-96-v5.6_Oncomine_Childhood_Cancer_Research_DNA_and_Fusions


Last edited by cmccabe; 1 Week Ago at 09:17 AM..
# 5  
Old 1 Week Ago
See the following proposal, with some comments:
Code:
#!/bin/sh

# grab run directory.

# POSIX shell compliant.

# external commands to be found in
PATH=/bin:/usr/bin

# debug:
#set -xv

# dryrun:
dryrun="echo"

# exit in case the directory can not be accessed
cd /home/cmccabe/Desktop/f1 || exit 1

for dir in /home/cmccabe/medex.logs/ponc/*/
do

  # the */ filters for directories, the following checks for a null match
  [ -d "$dir" ] || continue

  run=$(basename "$dir")

  # find match and copy run.

  # https://github.com/koalaman/shellcheck/wiki/SC2035
  # A * glob can overflow "too many arguments"
  # get two levels * and */*
  #match=$(find . -mindepth 1 -maxdepth 2 -type d)
  #if [ "$run" = "$match" ]
  # can this ever be true? $match is newline-separated!
  
  # test one level; the 2nd level would be */"$run", it could loop thru both with
  #for match in "$run" */"$run"
  match=$run
  if [ -d "$match" ]
  then
    # the following will copy to $match/$run/
    #$dryrun cp -Rv "$dir" "$match"
    # the following will copy to $match/
    $dryrun cp -Rv "$dir" "$(dirname "$match")"
  fi

done

This User Gave Thanks to MadeInGermany For This Post:
cmccabe (1 Week Ago)
# 6  
Old 1 Week Ago
There are logical errors in the code.

For example
Code:
for dir in /home/cmccabe/medex.logs/ponc/*/ ; do

  if [ -d "$dir" ] ; then
     run="$(basename "$dir")"
  fi

$run is always *

Please tell where the source directories to be copied are located (the full directory path), and what is the target directory (the full directory path).

Last edited by johnprogrammer; 1 Week Ago at 10:08 AM..
This User Gave Thanks to johnprogrammer For This Post:
cmccabe (1 Week Ago)
# 7  
Old 1 Week Ago
Quote:
run is always *
No it's not - dir cycles thru the matches!
This User Gave Thanks to MadeInGermany For This Post:
cmccabe (1 Week Ago)
Login or Register to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
Bash - Find files excluding file patterns and subfolder patterns jcdole Shell Programming and Scripting 2 09-21-2017 11:25 AM
Delete all files if another files in the same directory has a matching occurrence of a specific word kridhick Shell Programming and Scripting 7 11-19-2014 05:47 AM
Need help in finishing a bash script for listing subfolder by size in a large folder ultimo Shell Programming and Scripting 2 12-04-2013 05:27 AM
Copy files matching multiple conditions NarayanaPrakash Shell Programming and Scripting 5 10-16-2013 07:43 AM
[SOLVED] Copy subfolder to another directory preserving parent name alex2005 UNIX for Dummies Questions & Answers 2 09-16-2013 03:49 PM
Bash script to copy apache log files to client directory gregraven Shell Programming and Scripting 3 09-06-2013 12:58 PM
Copy the files in directory and sub folders as it is to another directory. santosh2626 Shell Programming and Scripting 4 02-22-2013 01:14 AM
Unable to copy files due to many files in directory before4 Red Hat 2 09-05-2012 02:36 AM
copy all files matching the request and change the extension at the same time vacuity93 UNIX for Dummies Questions & Answers 16 12-24-2011 04:10 AM
Link multiple files from different subfolder to a new subfolder total_ysf Shell Programming and Scripting 6 07-19-2011 04:13 AM
Look for distinct files under a directory matching a pattern b.paramanatti Solaris 2 10-12-2010 08:32 AM
Find php files within any matching directory name? macwise7 UNIX for Dummies Questions & Answers 4 09-24-2010 04:45 AM
Delete all files if another files in the same directory has a matching occurence of a specific word premier_de Shell Programming and Scripting 2 11-20-2009 04:48 AM
Simple BASH shell script to rename webcam jpg and copy into a new directory. robfindlay Shell Programming and Scripting 4 03-22-2009 01:10 AM
[Help]RegEx, Putty, Copy Files Insensitive Matching, SSH Lamonte Shell Programming and Scripting 11 11-05-2008 06:36 PM