Solved

File moving script

Posted on 2013-11-07
6
309 Views
Last Modified: 2013-11-14
Hello experts,

I have tens of thousands of files stored in this location in a CentOs server:

/var/gsm/

All file names are like this:

123_45678_2013-11-29-15-45-30.gsm

This file name represents two numbers (123 and 45678) and a full date (2013/11/29) and a time (15:45:30)

Now I have created new directories like:

/usr/gsm/2013/01/
/usr/gsm/2013/02/
/usr/gsm/2013/03/
...etc

I need a script to move files from the old directory to their corresponding new directories depending on year and month as mentioned above. The given file example should be moved to the directory /usr/gsm/2013/11/

The script should also rename files upon moving, replacing dashes ( - ) with underscores ( _ ), so the given file example should finally be:

/usr/gsm/2013/11/123_45678_2013_11_29_15_45_30.gsm

Note that the first two sections (123) and (45678) can vary in length. They may be 3,4,5 or more digits. The only wan to recognise sections is by hyphens and underscores.

Would any body write the required script for me?

Thanks in advance.
0
Comment
Question by:Muhajreen
  • 4
6 Comments
 
LVL 12

Accepted Solution

by:
tel2 earned 500 total points
ID: 39632945
Hi Muhajreen,

cd to the source directory of .gsm files.
Then run the following script:

#!/bin/bash

find . -maxdepth 1 -name "*.gsm" |\
while read FILE1
do
        FILE1=$(echo $FILE1 | cut -c3-)
        FILE2=$(echo $FILE1 | sed 's/-/_/g')
        YYYY_MM=$(echo $FILE1 | sed 's/^[0-9][0-9]*_[0-9][0-9]*_\([0-9][0-9][0-9][0-9]-[0-9][0-9]\).*/\1/')
        YYYY=$(echo $YYYY_MM | cut -c-4)
        MM=$(echo $YYYY_MM | cut -c6-)
        echo "Moving $FILE1 to /usr/gsm/$YYYY/$MM/$FILE2"
        #mv $FILE1 usr/gsm/$YYYY/$MM/$FILE2
done

Open in new window

NOTE: If you're happy with what it says it will do, then remove the "#" before the "mv..." and run it again.
Might be best to start with a test directory of a few files.

Got to go.
tel2
0
 
LVL 84

Expert Comment

by:ozo
ID: 39632953
#!/bin/bash                                                                                                                                              
cd /var/gsm/
for f in *_*_*-*-*-*-*-*.gsm ; do
  d=${f##*_}
  d=${d/-//}
  d=${d%%-*}
  mv $f $d/${f//-/_}
done
0
 
LVL 12

Expert Comment

by:tel2
ID: 39632959
PS: My script's line 12:
    #mv $FILE1 usr/gsm/$YYYY/$MM/$FILE2
should have been:
    #mv $FILE1 /usr/gsm/$YYYY/$MM/$FILE2
Leading slash.
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 12

Expert Comment

by:tel2
ID: 39635394
Thanks for the points, Muhajreen.  How long did it run for?


Nice work, ozo.  Concise and to the point, as usual.  Much nicer than my old-school and probably inefficient attempt.

I think your line:
    mv $f $d/${f//-/_}
should have read:
    mv $f /usr/gsm/$d/${f//-/_}
Remember, the files are moving from /var/gsm to /usr/gsm/...etc...

Would that
    for f in *_*_*-*-*-*-*-*.gsm
line work with tens of thousands of files?  I would have expected it to fail like "ls *.gsm" would have, which is why I did it the way I did with find.

Yes, I made an assumption about the names of the gsm files when I simply used "*.gsm" in my find.
0
 

Author Comment

by:Muhajreen
ID: 39635791
Thank you too tel2.

In fact I could understand your script more than zoo's one, that's why I used it. It took about 5 minutes, I have an SSD.

Please try to answer my other question here because now I want to insert DB entries for the same moved files to a MySQL database.
0
 
LVL 12

Expert Comment

by:tel2
ID: 39636391
> Please try to answer my other question here because now I want to insert DB entries for the same moved files to a MySQL database.
Looks as if some other experts have got it under control, Muhajreen.  Let me know if not.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question