LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-10-2015, 11:48 AM   #1
jazziao
LQ Newbie
 
Registered: Dec 2013
Posts: 2

Rep: Reputation: Disabled
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!
 
Old 02-10-2015, 12:12 PM   #2
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,362

Rep: Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004
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
 
Old 02-10-2015, 12:22 PM   #3
jazziao
LQ Newbie
 
Registered: Dec 2013
Posts: 2

Original Poster
Rep: Reputation: Disabled
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.

Last edited by jazziao; 02-10-2015 at 12:24 PM.
 
Old 02-10-2015, 12:43 PM   #4
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,362

Rep: Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004
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.
 
Old 02-10-2015, 02:08 PM   #5
rtmistler
Moderator
 
Registered: Mar 2011
Location: Sutton, MA. USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu
Posts: 4,484
Blog Entries: 10

Rep: Reputation: 1643Reputation: 1643Reputation: 1643Reputation: 1643Reputation: 1643Reputation: 1643Reputation: 1643Reputation: 1643Reputation: 1643Reputation: 1643Reputation: 1643
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Seeking a good bulk file renaming program 3lud13 Linux - Software 2 10-07-2012 01:07 PM
renaming files in Linux shorte85 Linux - Newbie 12 05-26-2010 11:25 AM
Renaming Windows Files from Linux Lonely_warrior Linux - General 3 07-13-2004 10:58 PM
Bulk File Renaming nevereverend Linux - Newbie 11 07-03-2004 06:28 PM


All times are GMT -5. The time now is 06:25 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration