Sponsored Content
Top Forums Shell Programming and Scripting Move matching file to folder in same directory Post 303031612 by Don Cragun on Saturday 2nd of March 2019 09:48:12 PM
Old 03-02-2019
Quote:
Originally Posted by cmccabe
Code:
cmccabe@Satellite-M645:~$ set -xv
cmccabe@Satellite-M645:~$ dir=/home/cmccabe/folder
dir=/home/cmccabe/folder
+ dir=/home/cmccabe/folder
cmccabe@Satellite-M645:~$ for file1 in $dir/*.pdf; do
for file1 in $dir/*.pdf; do
> # Grab file prefix
# Grab file prefix
>   bname=`basename $file1` # strip of path
  bname=`basename $file1` # strip of path
>   pre="$(echo $bname|cut -d_ -f1,2,3,4)" # remove after fourth underscore
  pre="$(echo $bname|cut -d_ -f1,2,3,4)" # remove after fourth underscore
>   pdf="$(echo $pre|awk -F- '{print $2"-"$3}')" # split on - and print 2 and third field
  pdf="$(echo $pre|awk -F- '{print $2"-"$3}')" # split on - and print 2 and third field
> # Find matching folder
# Find matching folder
>   folder=$(find /home/cmccabe/folder/*/ -type d)
  folder=$(find /home/cmccabe/folder/*/ -type d)
>   fpre="$(echo "$folder"|cut -d_ -f1,2,3,4,5,6,7,8,9)" # remove after ninth underscore
  fpre="$(echo "$folder"|cut -d_ -f1,2,3,4,5,6,7,8,9)" # remove after ninth underscore
>   fmatch="$(echo "$fpre"|awk -F- '{print $3"-"$4}')"  # split on - and print third and fourth field
  fmatch="$(echo "$fpre"|awk -F- '{print $3"-"$4}')"  # split on - and print third and fourth field
>      if [[ $pdf = $fmatch ]] # only execute file---folder value match
     if [[ $pdf = $fmatch ]] # only execute file---folder value match
>       then
      then
>    cp $dir/$pdf.pdf $dir/$fmatch
   cp $dir/$pdf.pdf $dir/$fmatch
> fi
fi
> done
done
+ for file1 in '$dir/*.pdf'
basename $file1
++ basename /home/cmccabe/folder/Auto_user_S5_0271-95-v5.6_Oncomine_Childhood_Cancer_Research_DNA_and_Fusions.pdf
+ bname=Auto_user_S5_0271-95-v5.6_Oncomine_Childhood_Cancer_Research_DNA_and_Fusions.pdf
echo $bname|cut -d_ -f1,2,3,4
++ echo Auto_user_S5_0271-95-v5.6_Oncomine_Childhood_Cancer_Research_DNA_and_Fusions.pdf
++ cut -d_ -f1,2,3,4
+ pre=Auto_user_S5_0271-95-v5.6
echo $pre|awk -F- '{print $2"-"$3}'
++ echo Auto_user_S5_0271-95-v5.6
++ awk -F- '{print $2"-"$3}'
+ pdf=95-v5.6
find /home/cmccabe/folder/*/ -type d
++ find /home/cmccabe/folder/R_2019_01_30_14_24_53_user_S5-0271-95-v5.6_Oncomine_Childhood_Cancer_Research_DNA_and_Fusions/ /home/cmccabe/folder/R_2019_01_30_14_24_53_user_S5-0271-96-v5.6_Oncomine_Childhood_Cancer_Research_DNA_and_Fusions/ -type d
+ folder='/home/cmccabe/folder/R_2019_01_30_14_24_53_user_S5-0271-95-v5.6_Oncomine_Childhood_Cancer_Research_DNA_and_Fusions/
/home/cmccabe/folder/R_2019_01_30_14_24_53_user_S5-0271-96-v5.6_Oncomine_Childhood_Cancer_Research_DNA_and_Fusions/'
echo "$folder"|cut -d_ -f1,2,3,4,5,6,7,8,9
++ echo '/home/cmccabe/folder/R_2019_01_30_14_24_53_user_S5-0271-95-v5.6_Oncomine_Childhood_Cancer_Research_DNA_and_Fusions/
++ cut -d_ -f1,2,3,4,5,6,7,8,9
/home/cmccabe/folder/R_2019_01_30_14_24_53_user_S5-0271-96-v5.6_Oncomine_Childhood_Cancer_Research_DNA_and_Fusions/'
+ fpre='/home/cmccabe/folder/R_2019_01_30_14_24_53_user_S5-0271-95-v5.6
/home/cmccabe/folder/R_2019_01_30_14_24_53_user_S5-0271-96-v5.6'
echo "$fpre"|awk -F- '{print $3"-"$4}'
++ echo '/home/cmccabe/folder/R_2019_01_30_14_24_53_user_S5-0271-95-v5.6
/home/cmccabe/folder/R_2019_01_30_14_24_53_user_S5-0271-96-v5.6'
++ awk -F- '{print $3"-"$4}'
+ fmatch='95-v5.6
96-v5.6'
+ [[ 95-v5.6 = 95-v5.6
96-v5.6 ]]
+ for file1 in '$dir/*.pdf'
basename $file1
++ basename /home/cmccabe/folder/Auto_user_S5_0271-96-v5.6_Oncomine_Childhood_Cancer_Research_DNA_and_Fusions.pdf
+ bname=Auto_user_S5_0271-96-v5.6_Oncomine_Childhood_Cancer_Research_DNA_and_Fusions.pdf
echo $bname|cut -d_ -f1,2,3,4
++ echo Auto_user_S5_0271-96-v5.6_Oncomine_Childhood_Cancer_Research_DNA_and_Fusions.pdf
++ cut -d_ -f1,2,3,4
+ pre=Auto_user_S5_0271-96-v5.6
echo $pre|awk -F- '{print $2"-"$3}'
++ echo Auto_user_S5_0271-96-v5.6
++ awk -F- '{print $2"-"$3}'
+ pdf=96-v5.6
find /home/cmccabe/folder/*/ -type d
++ find /home/cmccabe/folder/R_2019_01_30_14_24_53_user_S5-0271-95-v5.6_Oncomine_Childhood_Cancer_Research_DNA_and_Fusions/ /home/cmccabe/folder/R_2019_01_30_14_24_53_user_S5-0271-96-v5.6_Oncomine_Childhood_Cancer_Research_DNA_and_Fusions/ -type d
+ folder='/home/cmccabe/folder/R_2019_01_30_14_24_53_user_S5-0271-95-v5.6_Oncomine_Childhood_Cancer_Research_DNA_and_Fusions/
/home/cmccabe/folder/R_2019_01_30_14_24_53_user_S5-0271-96-v5.6_Oncomine_Childhood_Cancer_Research_DNA_and_Fusions/'
echo "$folder"|cut -d_ -f1,2,3,4,5,6,7,8,9
++ echo '/home/cmccabe/folder/R_2019_01_30_14_24_53_user_S5-0271-95-v5.6_Oncomine_Childhood_Cancer_Research_DNA_and_Fusions/
/home/cmccabe/folder/R_2019_01_30_14_24_53_user_S5-0271-96-v5.6_Oncomine_Childhood_Cancer_Research_DNA_and_Fusions/'
++ cut -d_ -f1,2,3,4,5,6,7,8,9
+ fpre='/home/cmccabe/folder/R_2019_01_30_14_24_53_user_S5-0271-95-v5.6
/home/cmccabe/folder/R_2019_01_30_14_24_53_user_S5-0271-96-v5.6'
echo "$fpre"|awk -F- '{print $3"-"$4}'
++ echo '/home/cmccabe/folder/R_2019_01_30_14_24_53_user_S5-0271-95-v5.6
/home/cmccabe/folder/R_2019_01_30_14_24_53_user_S5-0271-96-v5.6'
++ awk -F- '{print $3"-"$4}'
+ fmatch='95-v5.6
96-v5.6'
+ [[ 96-v5.6 = 95-v5.6
96-v5.6 ]]

And, in that trace output do you see that the if test is always failing as shown in red above because fnmatch is being set to two values as shown in orange and then compared to $pdf which only contains a single value?

And, note that your copy command:
Code:
   cp $dir/$pdf.pdf $dir/$fmatch

can't do what you want it to do if $fmatch expands to two directory names.

Does this tell you what you need to do next?
This User Gave Thanks to Don Cragun For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Move the file from one folder to another folder

Hi, I have a requirement to move a file from one folder(a) to another folder(b) only when folder (b) have a write permission. Folder permission is 755 If the permission is otherthan 755 we need to come out of the loop I will appreciate your help Thanks Soll (1 Reply)
Discussion started by: sollins
1 Replies

2. Shell Programming and Scripting

File Management: How do I move all JPGS in a folder structure to a single folder?

This is the file structure: DESKTOP/Root of Photo Folders/Folder1qweqwasdfsd/*jpg DESKTOP/Root of Photo Folders/Folder2asdasdasd/*jpg DESKTOP/Root of Photo Folders/Folder3asdadfhgasdf/*jpg DESKTOP/Root of Photo Folders/Folder4qwetwdfsdfg/*jpg DESKTOP/Root of Photo... (4 Replies)
Discussion started by: guptaxpn
4 Replies

3. Shell Programming and Scripting

Move a file from windows directory to unix directory

Move a file from windows directory to unix directory, is this possible? if it is, can someone help me on this? Thanks! God bless! (1 Reply)
Discussion started by: kingpeejay
1 Replies

4. Shell Programming and Scripting

Search through subfolders and move them into separate folder on the base of file size

Hello guys I am sure that you will help me on this issue as you did earlier::) Scenario : I have a folder named "XYZ". It consist many sub-folders and subfolder contain severals files. there may be abc.dat in each subfolder. Now i want to seperate subfolders on follwing conditions- if abc.dat... (12 Replies)
Discussion started by: infiant
12 Replies

5. Shell Programming and Scripting

Move the latest or older File from one directory to another Directory

I Need help for one requirement, I want to move the latest/Older file in the folder to another file. File have the datetimestamp in postfix. Example: Source Directory : \a destination Directory : \a\b File1 : xy_MMDDYYYYHHMM.txt (xy_032120101456.txt) File2: xy_MMDDYYYYHHMM.txt... (1 Reply)
Discussion started by: pp_ayyanar
1 Replies

6. Shell Programming and Scripting

FTPS Script to move a file to Unix Folder

Dear Experts, I need to connect to a FTPS Server and move the files from FTPS folder "/SAP/Out" to Unix directory "/SAP/In". I need to run this script on Unix directory...Script should get the files from FTPS folder and place that in specified Unix Directory. Thanks In Advance. (1 Reply)
Discussion started by: phani333
1 Replies

7. Shell Programming and Scripting

want to move set of file from one folder to another folder

Hi all, let me explain my requirments i am having 5 folder with different name for eg) abc , cdf , efd, rtg, ead each 5 folders contain 15 files i want to move 10 files to some other folder, remain 5 files should be there in the same folder. give me some suggestion on this. (6 Replies)
Discussion started by: natraj005
6 Replies

8. Shell Programming and Scripting

Move files from one folder to another along with directory indicator

Hi All, I have directory and it has multiple sub directories and all these sub directories contains many files. i want to move all these files to one different directory. But after moving files i should be able to recognize which file belongs to which directory. Is there any way to achieve... (6 Replies)
Discussion started by: gnnsprapa
6 Replies

9. Shell Programming and Scripting

Shell script cannot create directory and move the file to that directory

I have a script, which is checking if file exists and move it to another directory if then mkdir -p ${LOCL_FILES_DIR}/cool_${Today}/monthly mv report_manual_alloc_rpt_A_I_ASSIGNMENT.${Today}*.csv ${LOCL_FILES_DIR}/cool_${Today}/monthly ... (9 Replies)
Discussion started by: digioleg54
9 Replies

10. Shell Programming and Scripting

Request for Shell script to move files from Subfolder to Parent folder and delete sub folder

Hi Team, I am new to shell script and there is a requirement where files should be moved from Subfolder to parent folder. Eg: parent folder --> /Interface/data/test/IN Sub folder -->/Interface/data/test/IN/Invoice20180607233338 Subfolder will be always with timestamp... (6 Replies)
Discussion started by: srivarun15
6 Replies
All times are GMT -4. The time now is 07:49 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy