[plug] Mass moving files with a script

Ben Woods woodsb02 at gmail.com
Wed Dec 6 07:32:22 WST 2006


Hi guys,

I have a bunch of files in subdirectories which i want to move into a
different directory structure based on their filename.

I currently have directories such as:
C/
D/
Q/

And the files are named something like:
C/135-C-1A.doc
C/8747-C-1A.doc
D/7485-D-1A.doc
Q/8747-Q-1A.doc
Q/8747-Q-1B.doc

Instead of having them in directories which are named by the letter
between the 2 hyphens, I want to move them into directories which are
name by the number prior to the first hyphen.

Eg, in the above example:
3 Directories would be made:
135/
7485/
8747/

And the file would be moved into the appropriate directory and then
the current directories (which would then be empty) would be deleted.
There should be no overlap in the filenames (there clearly will be in
the directory names to create though), but if there was an overlap,
the script should not overwrite the files.

As far as i can tell, this would involve the following steps:
- For each file name in any of the subdirectories in the current
directory, find the string prior to the first hyphen.
- If there is no folder by that name already existing, create it.
- Move the file into this folder (but do not replace anything that may
already exist)
- If the folder it was in is now empty, rmdir it

I have about 10,000 files to do this to... there should be no spaces
in the filenames etc... anyone good at bash scripting that can help me
out? The bit I am struggling with is really the string recognition /
analysis at the start.

Cheers!


-- 
From: Benjamin Woods
woodsb02 at gmail.com
Registered Linux User #372573
Registered Ubuntu User #6395



More information about the plug mailing list