Linux Bulk renaming files
Hello Folks.
I'm searching for a easy way to rename multiple files from CLI but didn't find any easy way for me so I'm reaching out to you guys for help. This is what I want to do (from CLII or script). I want to move files with a sequence number on the name of the files (msg0000, msg0001, msg0002 and so on) to let's say msg0066, msg0067 and so on. Each of this file name has two other files (msg0000.wav, msg0000.WAV and msg0000.txt). The idea is to move them from one directory to another and following a sequence in the file names, is there a way I can do this pain free? Any help on this matter will be greatly appreciates and I'm talking about over 100 files I need to move following the sequence of the receiving directory. Thanks! |
You need to be a lot more specific. What, exactly, do you want to move where, exactly?
You can do whatever it is with a script, but nobody can provide much guidance based on that vague description alone. Code:
for i in msg????; do |
Thanks for your reply. Let me try to be a little more specific as per your request:
I have a bunch of files on the following directory: /var/spool/asterisk/voicemail/default/6288/INBOX I want to move files to: /var/spool/asterisk/voicemail/default/6239/INBOX The files on the 6288 directory are as follows: msg0000.WAV msg0000.wav msg0000.txt it continues with 0002, 0003 and so on. On the receiving directory (/var/spool/asterisk/voicemail/default/6239/INBOX) I have the same set of files, the last one there is msg0065 (.WAV, .wav and .txt). The idea is to move all the files from the first directory into the receiving directory, following the filename sequence on the receiving directory. Is this clear enough guys? Thanks for your support. |
Yes that's much better, thanks.
There are a lot of ways to accomplish that, you should start to think about failure modes and how you want to handle them, it might influence how the script ends up being written. For example, what about when you hit 9999. Do you want to move to 10000, or go back to 0000 and overwrite the previous 0000? What if there's a missing number on the source or receive side, say you have 0010 and 0012, but no 0011? What if you have 0000.WAV and 0000.txt, but no 0000.wav? Is it possible for any of these files to have spaces or "illegal" characters? Will it always be "msg" or could it be some other string? Just some things to think about. Something to get you started: Code:
srcdir=/var/spool/asterisk/voicemail/default/6288/INBOX Again, you'll need to consider failure modes and various exceptions, and how you want the code to react. This solution may not be exactly what you want, it could require some tweaking here or there. It's just meant to be a jumping off point. |
There's a rename command which does work, however if you have things like _copy of etc, it gets dicey. Basically you have to understand REGEX pretty well. I have a few examples, unfortunately on my home computer and they're highly specific to renaming like the JPEG files which I get from my camera to meaningful names.
I would suggest the rename command, but also suggest that you divide an conquer first, which is to say that you sub-directory different intended name targets so you can be careful that it doesn't go hogwild and rename every file, including ones which you didn't intent to touch. I don't think it's ever going to be pain free unless the file names are 100% highly deterministic and you can write a script to do it for you. |
All times are GMT -5. The time now is 06:38 PM. |