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