LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices



Reply
 
Search this Thread
Old 09-28-2012, 07:50 PM   #1
jocala
LQ Newbie
 
Registered: Sep 2012
Posts: 4

Rep: Reputation: Disabled
bash: bulk rename files


Howdy,

I have a bunch of files that have ._. embedded in the filename, e.g. file._.name.txt. I'd like to remove the _. so I end up with file.name.txt. Can someone show me an example with bash or mmv?

Thanks!
 
Old 09-28-2012, 09:31 PM   #2
John VV
Guru
 
Registered: Aug 2005
Posts: 13,477

Rep: Reputation: 1803Reputation: 1803Reputation: 1803Reputation: 1803Reputation: 1803Reputation: 1803Reputation: 1803Reputation: 1803Reputation: 1803Reputation: 1803Reputation: 1803
have a look at "sed"
Code:
man sed
--- and ---
sed --help
from the sed sourced
http://sed.sourceforge.net/sed1line.txt
 
Old 09-28-2012, 09:48 PM   #3
jocala
LQ Newbie
 
Registered: Sep 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by John VV View Post
have a look at "sed"
Code:
man sed
--- and ---
sed --help
from the sed sourced
http://sed.sourceforge.net/sed1line.txt
Thanks for the reply, perhaps I wasn't clear. I don't want to make changes within a text file. I want to change the file's name: mv file.-.name.txt file.name.txt. My first thought was mmv, but I can't grok the correct pattern for replacing .-. in a filename.

I need to do the mv/rename operation above on a directory full of files that have .-. embedded in the file name. I don't see how a line editor like sed will help me do this. If it can, do you have an example?
 
Old 09-29-2012, 12:20 AM   #4
ArfaSmif
Member
 
Registered: Oct 2008
Location: Brisbane Australia
Distribution: always back to Fedora
Posts: 241

Rep: Reputation: 50
Quote:
Originally Posted by jocala View Post
Thanks for the reply, perhaps I wasn't clear. I don't want to make changes within a text file. I want to change the file's name: mv file.-.name.txt file.name.txt. My first thought was mmv, but I can't grok the correct pattern for replacing .-. in a filename.

I need to do the mv/rename operation above on a directory full of files that have .-. embedded in the file name. I don't see how a line editor like sed will help me do this. If it can, do you have an example?
Think about this :-

echo file._.name.txt | sed s/_.//g
 
Old 09-29-2012, 01:10 AM   #5
jocala
LQ Newbie
 
Registered: Sep 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
Thanks for the replies.
 
Old 09-29-2012, 02:35 AM   #6
linux_BSD
Member
 
Registered: Sep 2012
Posts: 47

Rep: Reputation: 4
@ jocal

Code:
for i in *._.*; do echo mv "$i" "$(echo $i | sed 's/_.//g')"; done
Run this code to get a preview of what mv will do to your files. I created 3 files with the ._.name.txt to test the code. I ran the code above to test the results and here is the output.

mv file1._.name.txt file1.name.txt
mv file2._.name.txt file2.name.txt
mv file3._.name.txt file3.name.txt

As you can see it does what you want. Nothing has been alter to the files because the first echo command only prints command syntax. It shows you a preview of what it will do to the files.

To really make the changes to your files remove the first echo command after do. The code will now be:

Code:
for i in *._.*; do mv "$i" "$(echo $i | sed 's/_.//g')"; done
PS: Whenever you do a bulk rename to your files it is always a good idea to precede the command syntax with echo to get a preview. Then remove it if satisfy with the preview output.

-Cheers

Last edited by linux_BSD; 09-29-2012 at 02:56 AM.
 
Old 09-29-2012, 06:01 AM   #7
H_TeXMeX_H
Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269
Code:
bash-4.1$ ls
file._.name.txt  file1._.name.txt  file2._.name.txt
bash-4.1$ rename '_.' '' *.txt
bash-4.1$ ls
file.name.txt  file1.name.txt  file2.name.txt
 
  


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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
rename bulk files scofiled83 Programming 11 06-26-2009 11:16 PM
Trouble with making a bash script to read in different files and rename output files. rystke Linux - Software 1 05-07-2009 09:00 AM
bulk rename files atiman Linux - Newbie 5 03-05-2009 01:42 AM
shell command for bulk 'rename' ashrafulkarim Linux - Newbie 5 12-17-2008 04:46 AM
rename files using bash? ferradura Linux - General 4 09-14-2007 04:35 AM


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