Move matching file to folder in same directory


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Move matching file to folder in same directory
# 8  
Old 03-04-2019
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:
# 9  
Old 03-04-2019
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 03-04-2019
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:
# 11  
Old 03-04-2019
works perfect.... thank you Smilie.

What does pdf# in the key do? Thank you Smilie.
# 12  
Old 03-04-2019
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 Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
Login or Register to Ask a Question