Help with Complex Bash Script

I have an FTP server with thousands of Invoices. All Invoices are in a folder called /volume1/MBSInvoices/

Monthly invoices are added to that folder every month.

Here is a sample filename of the Invoices:

invoice_1_20170101_10010052_10020052_10030052_JOHNDOE.pdf 

the Account ID is the 6th segment of the name ( 10030052 )

I would like to create a bash script that does the following:

Check the 6th segment in the file name. If a folder with that name doesn't exist, create one. Otherwise don't create one.

Move those Invoices to their correspondent folders based on the 6th segement on the invoice name

Example:

invoice_1_20170101_10010052_10020052_10030052_JOHNDOE.pdf 

The script will check the file name, specifically the 6th segment.
If the a folder named 10030052 doesn't exist, create a new one.
Then move the file invoice_1_20170101_10010052_10020052_10030052_JOHNDOE.pdf to folder 10030052

If the folder named 10030052 already exists,
move the file invoice_1_20170101_10010052_10020052_10030052_JOHNDOE.pdf to folder 10030052

The script needs to do this for over 600 files. The script will be run once a month as new monthly invoices become avaiable.

Thanks

Welcome to the forum.

Any attempts / ideas / thoughts from your side?

1 Like

Hi RudiC, I have no idea as I'm a newbie to bash scripting.

Thanks

Badr

Well, try

for FN in *.pdf
   do   TMP=${FN#*_*_*_*_*_}
        TMP=${TMP%%_*}
        mkdir "$TMP"
        echo mv -v "$FN" "$TMP"
   done

The echo is for safety reasons; remove it if you're happy with the results it proposes. No error checking is done; add some decent check before using it in production. Esp., we make use of the behaviour of mkdir failing without further complications should the directory already exist, but it could fail for other reasons.

#!/bin/bash
BASE_DIR=/Users/karan/Practice_Shell_Scripting/Move_Invoice
SOURCE_DIR=/Users/karan/Practice_Shell_Scripting/Move_Invoice/Source_DIR
TARGET_DIR=/Users/karan/Practice_Shell_Scripting/Move_Invoice/Target_DIR

rm ${BASE_DIR}/FIle_list.txt

find ${SOURCE_DIR}/ -name "*.pdf" >> ${BASE_DIR}/FIle_list.txt

for i in `cat ${BASE_DIR}/FIle_list.txt`
do
	FILE_NAME=`basename $i`
	FOLDER_NAME=`basename $i| awk -F "_" '{print $6}'`
	echo ${FILE_NAME}
	echo ${FOLDER_NAME}
	if [ -d "${TARGET_DIR}/${FOLDER_NAME}" ]
	then
		mv $i ${TARGET_DIR}/${FOLDER_NAME}/
	else
		mkdir -p ${TARGET_DIR}/${FOLDER_NAME}
		mv $i ${TARGET_DIR}/${FOLDER_NAME}/
	fi
done

To keep the forums high quality for all users, please take the time to format your posts correctly.

First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags

```text
 and 
```

by hand.)

Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.

Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums

kpkanani, Thanks a Million. The scripts works exactly the way we need it to.

Rudi, Thanks for your help too.

Badr