LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 08-13-2014, 02:23 PM   #1
Sigma721
LQ Newbie
 
Registered: Aug 2014
Posts: 1

Rep: Reputation: Disabled
Renaming directories in loop


On one of our Raspbian(similar to Debian) boxes we have a directory containing a number of folders(this is not constant, we are adding and removing folders all the time). The folders will be named something like this:

- Live
- Live 1
- Live 2
- Live 3
- etc

What we need to do, is to automatically rename folder "Live" to the first open number(in example above 4), then reduce every folders number by 1(so that Live1 becomes Live, Live2 becomes Live1, and so on). This will be repeated a number of times every day.

Do you know how to do this?

Regards
 
Old 08-13-2014, 02:58 PM   #2
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
Something like this

Put /path/to/Live* in a bash array
Then 'extract' the digit from the last member of array
Move Live to 'Last'.tmp
Then for each element of array, move to digit-1 (if 1, no digit)
Finally moving the Live(x).tmp to Live(x)

you may notice that bash arrays start at 0, (Live) , element 22 (Live22)

${#array[@]} will give you total number of elements 23 means last number is 22

Links

http://tldp.org/LDP/abs/html/arrays.html
http://mywiki.wooledge.org/BashGuide/Arrays

'Hunt around' on both those sites for general bash stuff

Edit;
Example bash string substitution, , for the mv stuff

Last edited by Firerat; 08-13-2014 at 03:07 PM.
 
Old 08-13-2014, 03:19 PM   #3
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,649
Blog Entries: 4

Rep: Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934
This sounds rather much like what the Linux/Unix logrotate utility does already!
 
1 members found this post helpful.
Old 08-13-2014, 03:26 PM   #4
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
I didn't even think of that

Logrotate probably easier

Still wouldn't hurt to learn a bit'o'bash
 
  


Reply



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
[SOLVED] specific file name renaming in all sub directories Jack_R Linux - Newbie 15 04-07-2011 02:35 AM
[SOLVED] Renaming Directories Problem Crowey Programming 50 05-11-2010 07:04 PM
Renaming directories with Regex? Possible? smaddox Linux - General 9 11-09-2009 06:34 PM
loop through directories and move files ratcateme Programming 7 11-18-2008 02:33 AM
dumb newbie question about renaming directories Lleb_KCir Linux - General 2 10-26-2004 08:37 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 09:44 PM.

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
Open Source Consulting | Domain Registration