?
Solved

File moving script

Posted on 2013-11-07
6
Medium Priority
?
326 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 2000 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 85

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Hire Technology Freelancers with Gigs

Work with 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

In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
This article will show you step-by-step instructions to build your own NTP CentOS server.  The network diagram shows the best practice to setup the NTP server farm for redundancy. ┬áThis article also serves as your NTP server documentation.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses

850 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