Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

File moving script

Posted on 2013-11-07
6
Medium Priority
?
325 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 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
Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses

618 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