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 11-12-2013, 10:31 AM   #1
goltoof
LQ Newbie
 
Registered: Sep 2009
Posts: 7

Rep: Reputation: 0
batch renaming folders


I got a directory of folders named "No 35" No 36" "No 128" etc. I just want to take out the "No" part so they're just named the number. I'd alos like to add something before the number line "unit_" so the folder are named "unit_35" unit_36" etc..
 
Old 11-12-2013, 10:56 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Have a look at this:
Code:
while read THISDIR; do mv "$THISDIR" "${THISDIR/No /unit_}"; done < <( ls -bd No* )
You do need to be careful because your directory names contain spaces: Correct quoting and a modified ls are needed.

This: ls -bd No* makes sure that only files that start with No are listed (the -d prevents ls from going any deeper). The -b part escapes special characters (the spaces in your case).

Example run:
Code:
$ ls -l
drwxr-x--- 3 druuna druuna  4096 nov 12 16:54 No 1
drwxr-x--- 2 druuna druuna  4096 nov 12 16:53 No 10
drwxr-x--- 2 druuna druuna  4096 nov 12 16:53 No 110

$ while read THISDIR; do mv "$THISDIR" "${THISDIR/No /unit_}"; done < <( ls -bd No* )
$ ls -l
drwxr-x--- 3 druuna druuna 4096 nov 12 16:54 unit_1
drwxr-x--- 2 druuna druuna 4096 nov 12 16:53 unit_10
drwxr-x--- 2 druuna druuna 4096 nov 12 16:53 unit_110
 
Old 11-12-2013, 10:58 AM   #3
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,260

Rep: Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948
You could simplify things a bit

Code:
for i in No*; do mv "$i" "${i/No /unit_}"; done
No need for special escaping or while/reads when you incorporate the globbing directly into the for loop.
 
1 members found this post helpful.
Old 11-12-2013, 11:04 AM   #4
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
Just in case you have files that start No

Code:
for i in No*/; do mv "$i" "${i/No /unit_}"; done
/ so you only get directories
 
1 members found this post helpful.
Old 11-12-2013, 11:05 AM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
@suicidaleggroll: Old habits die hard.... For whatever reason I always overlook globbing directly into the for loop.

@goltoof: Both methods work, but I would use suicidaleggroll's method if I where you (simpler/more elegant).
 
Old 11-13-2013, 01:54 AM   #6
Inkit
Member
 
Registered: Feb 2011
Location: India
Distribution: Mint 10
Posts: 142

Rep: Reputation: 15
I'm not really good with the command line and use the bulk rename application to do this. It's an xfce application and gives you a before (file name as is) and after (file name after hitting apply) option which makes it easy for even the most simple minded to use. If you are on Gnome, it may pull in a lot of dependencies, but it's worth it, in my view.
 
1 members found this post helpful.
  


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
Batch file renaming Eddie Adams Linux - Newbie 13 08-12-2010 03:56 PM
help in batch renaming aapkae Linux - Newbie 5 02-13-2008 07:49 PM
batch renaming of files sycamorex Programming 9 08-13-2007 03:24 AM
Batch Renaming in bash xushi Programming 6 07-07-2005 04:24 PM
renaming batch of files linux_ub Linux - Newbie 6 10-27-2004 10:41 PM


All times are GMT -5. The time now is 02:30 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