Solved

File moving script

Posted on 2013-11-07
6
307 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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

The purpose of this article is to demonstrate how we can use conditional statements using Python.
Utilizing an array to gracefully append to a list of EmailAddresses
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)
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…

910 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now