URGENT!!! bash script to sort files into folder according to a string in the filename

Hi all.

I am very new to linux scripting and i have a task i can only solve with a script.
I need to sort files base on the date string in their filenames and create a folder using the same date string then move the files to their respective folders.

Scenario:
Folder Path: /test/testfiles
Files:
data20120106_101_015118.unl.bz2
data20120107_101_016118.unl.bz2
data20120108_101_015618.unl.bz2
data20120109_101_015118.unl.bz2
data20120109_101_015118.unl.bz2
data20120109_101_015118.unl.bz2
data20120110_101_015118.unl.bz2
data20120110_101_015118.unl.bz2
data20120110_101_015118.unl.bz2
data20120111_101_015118.unl.bz2
data20120111_101_015118.unl.bz2
data20120112_101_015118.unl.bz2
data20120112_101_015118.unl.bz2

I need to create a folder e.g: 20120110 and then move all files containing 20120110 e.g data20120110_101_015118.unl.bz2 into the created folder. I have to do this for several
files as far back as 2010. All files have the same filename format as above.

Thanks for your help.

Everyone at the UNIX and Linux Forums gives their best effort to reply to all questions in a timely manner. For this reason, posting questions with subjects like "Urgent!" or "Emergency" and demanding a fast reply are not permitted in the regular forums.

For members who want a higher visibility to their questions, we suggest you post in the Emergency UNIX and Linux Support Forum. This forum is given a higher priority than our regular forums.

Posting a new question in the Emergency UNIX and Linux Support Forum requires forum Bits. We monitor this forum to help people with emergencies, but we do not not guarantee response time or best answers. However, we will treat your post with a higher priority and give our best efforts to help you.

If you have posted a question in the regular forum with a subject "Urgent" "Emergency" or similar idea, we will, more-than-likely, close your thread and post this reply, redirecting you to the proper forum.

Of course, you can always post a descriptive subject text, remove words like "Urgent" etc. (from your subject and post) and post in the regular forums at any time.

Thank you.

The UNIX and Linux Forums

2 Likes