LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Linux Bulk renaming files (https://www.linuxquestions.org/questions/linux-newbie-8/linux-bulk-renaming-files-4175533614/)

jazziao 02-10-2015 10:48 AM

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!

suicidaleggroll 02-10-2015 11:12 AM

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
  mv ${i}* some-subdirectory/
done


jazziao 02-10-2015 11:22 AM

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.

suicidaleggroll 02-10-2015 11:43 AM

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
destdir=/var/spool/asterisk/voicemail/default/6239/INBOX

lastfile=$(ls $destdir/msg????.WAV | tail -n 1)
lastname=$(basename $lastfile)
lastnum=${lastname:3:4}
offset=$(expr $lastnum + 1)
for file in $srcdir/msg????.WAV; do
  thisname=$(basename $file)
  thisnum=${thisname:3:4}
  newnum=$(expr $offset + $thisnum)
  newprefix=msg$(printf %04d $newnum)
  echo mv ${newprefix}.* $destdir
done

Run it as-is to make sure it's doing what you expect, and then remove the echo and run it again to actually move the files

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.

rtmistler 02-10-2015 01:08 PM

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.