Muhajreen
asked on
File moving script
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_2 9_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.
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
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
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
#mv $FILE1 usr/gsm/$YYYY/$MM/$FILE2
should have been:
#mv $FILE1 /usr/gsm/$YYYY/$MM/$FILE2
Leading slash.
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.
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.
ASKER
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.
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.
> 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.
Looks as if some other experts have got it under control, Muhajreen. Let me know if not.
cd /var/gsm/
for f in *_*_*-*-*-*-*-*.gsm ; do
d=${f##*_}
d=${d/-//}
d=${d%%-*}
mv $f $d/${f//-/_}
done