Solved

File moving script

Posted on 2013-11-07
6
304 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 11

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 11

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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 11

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 11

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

760 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

18 Experts available now in Live!

Get 1:1 Help Now