Move matching file to folder in same directory


Login or Register to Reply

 
Thread Tools Search this Thread
# 8  
Old 2 Weeks Ago
Quote:
Originally Posted by cmccabe
Code:
Auto_user_S5_0271-95-v5.6_Oncomine_Childhood_Cancer_Research_DNA_and_Fusions.pdf  ---> R_2019_01_30_14_24_53_user_S5-0271-95-v5.6_Oncomine_Childhood_Cancer_Research_DNA_and_Fusions
Auto_user_S5_0271-96-v5.6_Oncomine_Childhood_Cancer_Research_DNA_and_Fusions.pdf ---> R_2019_01_30_14_24_53_user_S5-0271-96-v5.6_Oncomine_Childhood_Cancer_Research_DNA_and_Fusions

Yes I am just trying to move or copy the .pdf with a partial match (portion in green) to the folder with the partial match (other portion in green).

... ... ...
But the portions shown in green don't match. The green portion in the PDF files contain two underscores and two hyphens. The green portion in the directories contain one underscore and three hyphens.
This User Gave Thanks to Don Cragun For This Post:
cmccabe (2 Weeks Ago)
# 9  
Old 2 Weeks Ago
Quote:
But the portions shown in green don't match. The green portion in the PDF files contain two underscores and two hyphens. The green portion in the directories contain one underscore and three hyphens.
I missed that, should be:

pdf ---> folder
0271-96-v5.6 0271-96-v5.6
0271-95-v5.6 0271-95-v5.6

Thank you Smilie.
# 10  
Old 2 Weeks Ago
Maybe something a little faster and easier (i.e., no invocations of awk, basename, cut, find, or sort), like:
Code:
#!/bin/bash
set -xv
cd /home/cmccabe/folder || exit 1
for pdf in *.pdf
do	# Extract common component from the name...
	key=${pdf#*_*_*_}	# Get rid of 1st 3 underscores and everything
				# before them.
	key=${key%%_*}		# Get rid of the next underscore and everything
				# after it.

	# Copy PDF file to directory with a name containing the key.
	cp "$pdf" *"$key"*/
done

This was tested with both bash and ksh on macOS Mojave, version 10.14.3 and seems to do what you want with the two sample PDF files and the two sample directory names provided in this thread. The PDF files were copied to the expected directories.
This User Gave Thanks to Don Cragun For This Post:
cmccabe (2 Weeks Ago)
# 11  
Old 2 Weeks Ago
works perfect.... thank you Smilie.

What does pdf# in the key do? Thank you Smilie.
# 12  
Old 2 Weeks Ago
In the code:
Code:
for pdf in *.pdf
do	# Extract common component from the name...
	key=${pdf#*_*_*_}	# Get rid of 1st 3 underscores and everything
				# before them.

$pdf expands to the name of the PDF file being processed on this iteration through the for loop. The first value assigned to key is the expansion of the contents of the value assigned pdf with the shortest string matching the filename matching pattern *_*_*_ removed from the beginning of the string if that pattern matches the value assigned to pdf, otherwise it returns the entire value assigned to pdf. So, with your sample PDF filename:
Code:
Auto_user_S5_0271-95-v5.6_Oncomine_Childhood_Cancer_Research_DNA_and_Fusions.pdf

used on the first time through the loop, key is initially assigned the value:
Code:
0271-95-v5.6_Oncomine_Childhood_Cancer_Research_DNA_and_Fusions.pdf

without the text shown in red that is matched by the pattern *_*_*_ and on the 2nd time though the loop with pdf assigned the 2nd PDF filename:
Code:
Auto_user_S5_0271-96-v5.6_Oncomine_Childhood_Cancer_Research_DNA_and_Fusions.pdf

key will initially be assigned the value:
Code:
0271-96-v5.6_Oncomine_Childhood_Cancer_Research_DNA_and_Fusions.pdf

without the text marked in red that matches the same filename matching pattern.
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
Request for Shell script to move files from Subfolder to Parent folder and delete sub folder srivarun15 Shell Programming and Scripting 6 06-13-2018 03:24 PM
Shell script cannot create directory and move the file to that directory digioleg54 Shell Programming and Scripting 9 04-12-2017 02:57 AM
Move files from one folder to another along with directory indicator gnnsprapa Shell Programming and Scripting 6 03-23-2017 03:10 PM
Move file in to directory- script primo102 Shell Programming and Scripting 6 11-21-2014 07:23 PM
Move files from Space Folder to other folder kannansoft1985 Shell Programming and Scripting 2 08-22-2014 11:59 AM
How to move file from one directory to other of only particular user? lakers646 UNIX for Dummies Questions & Answers 4 02-17-2014 04:08 PM
Script to move one folder to multiple folder... peekuabc Shell Programming and Scripting 9 08-21-2013 06:02 AM
want to move set of file from one folder to another folder natraj005 Shell Programming and Scripting 6 08-23-2011 08:30 AM
Assistance with shell script to check file type and move to a folder. bwcberb UNIX for Dummies Questions & Answers 0 09-20-2010 10:36 AM
FTPS Script to move a file to Unix Folder phani333 Shell Programming and Scripting 1 07-22-2010 01:11 AM
Move the latest or older File from one directory to another Directory pp_ayyanar Shell Programming and Scripting 1 03-24-2010 03:22 PM
Search through subfolders and move them into separate folder on the base of file size infiant Shell Programming and Scripting 12 10-30-2009 08:00 AM
Move a file from windows directory to unix directory kingpeejay Shell Programming and Scripting 1 06-19-2009 01:31 PM
File Management: How do I move all JPGS in a folder structure to a single folder? guptaxpn Shell Programming and Scripting 4 06-11-2009 02:41 AM
Move the file from one folder to another folder sollins Shell Programming and Scripting 1 12-11-2008 03:03 PM