LinuxQuestions.org
Help answer threads with 0 replies.
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 03-12-2013, 10:13 AM   #1
mddnix
Member
 
Registered: Mar 2013
Distribution: Redhat, Ubuntu
Posts: 516

Rep: Reputation: 139Reputation: 139
Unhappy Mass file renaming


i'm looking for how to rename files in mass quantity. basically i am completely moving from windows (ntfs) to CentOs (ext4). in windows i used to give spaces in between file names, now i want to replace spaces with "-" or "_", so that i get the full file name when i hit tab in terminal.

i've searched google and also looked into "similar threads" of LQ, but could not find answer for my specific requirement. either they are for particular directory or have a pattern/incremental in nature, for example renaming *.jpeg files, file1*.jpg file123.jpg etc.

i'm looking for solutions based on "find" command.

for example:

Code:
# find /dc/cbt/ -type f -iname "*bind*"

/dc/cbt/Books/DNS Bind Configuration in RHEL 6.pdf
/dc/cbt/Linux/Install & Configure BIND DNS Server in CentOS.mp4
/dc/cbt/Linux/YouTube/rhel 6 bind primary dns server in redhat linux.flv
i know how to display modified files, but don't know how to write them permanent. that's the problem

Code:
# find /dc/cbt/ -type f -iname "*bind*" | sed "s/ /_/ig" | tr "[:upper:]" "[:lower:]"

/dc/cbt/books/dns_bind_configuration_in_rhel_6.pdf
/dc/cbt/linux/install_&_configure_bind_dns_server_in_centos.mp4
/dc/cbt/linux/youtube/rhel_6_bind_primary_dns_server_in_redhat_linux.flv
thanks...

Last edited by mddnix; 03-12-2013 at 10:41 AM.
 
Old 03-12-2013, 11:12 AM   #2
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 840

Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
How about something like this?

Code:
while read -r name; do
    lower="${name,,}"
    newname="${lower// /_}"
    echo "$name --> $newname"
    # mv "$name" "$newname"
done < <(find /dc/cbt/ -type f -iname "*bind*")
Be very careful with this. Backup your data.
You may also look at the manpage of rename command, which is quite a useful tool for mass renaming
 
1 members found this post helpful.
Old 03-12-2013, 11:21 AM   #3
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
Code:
#!/bin/bash

find /dc/cbt/ -type f -iname "*bind*" | while read line; do
   oldfile=$line
   newfile=${oldfile// /_}
   newfile=${newfile,,}
   echo mv "$oldfile" "$newfile"
done
Once you're sure it's working correctly, remove the "echo" to actually rename the files.


edit: too slow
 
1 members found this post helpful.
Old 03-12-2013, 02:16 PM   #4
mddnix
Member
 
Registered: Mar 2013
Distribution: Redhat, Ubuntu
Posts: 516

Original Poster
Rep: Reputation: 139Reputation: 139
Thumbs up Finally...

Thanks millgates & suicidaleggroll. I tried both, and both works great!!!

while trying, i realised that certain files have multiple spaces. so i changed a little. i think when both scripts are translated to machine language, they will be same.

Code:
#!/bin/bash
while read -r name; do
    lower="${name,,}"
    newname=`echo "$lower" | sed 's/  */_/g'`
    echo "$name --> $newname"
    mv "$name" "$newname"
done < <(find /dc/cbt/ -type f -iname "*bind*")
Code:
#!/bin/bash
find /dc/cbt/ -type f -iname "*bind*" | while read line; do
   oldfile=$line
   newfile=`echo "$oldfile" | sed 's/  */_/g'`
   newfile=${newfile,,}
   mv "$oldfile" "$newfile"
done
 
Old 03-12-2013, 03:20 PM   #5
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 840

Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
As an alternative to sed you could use bash extglob:

Code:
shopt -s extglob
foo="a  b   c"

# replace spaces with underscores
echo "${foo//+( )/_}"
also, I would recommend to use the $( command ) syntax for command substitution. It handles nesting and double quotes nicely, unlike the backticks.
 
1 members found this post helpful.
Old 03-12-2013, 04:54 PM   #6
mddnix
Member
 
Registered: Mar 2013
Distribution: Redhat, Ubuntu
Posts: 516

Original Poster
Rep: Reputation: 139Reputation: 139
Quote:
shopt -s extglob
???
 
Old 03-12-2013, 09:11 PM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,260

Rep: Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328
Re shopt = shell options ie amend the way the shell works.
See http://linux.die.net/man/1/bash and search for shell builtin commands, then shopt, then extglob within that.
Its about 80% of the way down that page or google for examples etc.
 
Old 03-13-2013, 04:21 AM   #8
mddnix
Member
 
Registered: Mar 2013
Distribution: Redhat, Ubuntu
Posts: 516

Original Poster
Rep: Reputation: 139Reputation: 139
Thanks.
 
  


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
[SOLVED] Mass Renaming of Files? NotAComputerGuy Linux - Newbie 16 09-30-2012 12:50 PM
mass renaming in order KBriggs Linux - Newbie 5 06-17-2009 01:02 PM
Mass renaming? yanik Linux - General 6 02-16-2009 04:25 PM
mass file renaming sed/awk/mv asalford Linux - General 2 01-16-2008 06:53 PM
Bash - Mass file renaming problem smudge|lala Linux - Software 2 02-14-2007 07:02 PM


All times are GMT -5. The time now is 05:11 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration