LinuxQuestions.org
Review your favorite Linux distribution.
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, 06: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, 08:31 PM   #2
John VV
LQ Muse
 
Registered: Aug 2005
Location: A2 area Mi.
Posts: 17,621

Rep: Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651
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, 08: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-28-2012, 11:20 PM   #4
ArfaSmif
Member
 
Registered: Oct 2008
Location: Brisbane Australia
Distribution: Fedora, Centos, Manjaro
Posts: 317

Rep: Reputation: 70
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, 12:10 AM   #5
jocala
LQ Newbie
 
Registered: Sep 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
Thanks for the replies.
 
Old 09-29-2012, 01: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 01:56 AM.
 
Old 09-29-2012, 05:01 AM   #7
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
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 10:16 PM
Trouble with making a bash script to read in different files and rename output files. rystke Linux - Software 1 05-07-2009 08:00 AM
bulk rename files atiman Linux - Newbie 5 03-05-2009 12:42 AM
shell command for bulk 'rename' anon201 Linux - Newbie 5 12-17-2008 03:46 AM
rename files using bash? ferradura Linux - General 4 09-14-2007 03:35 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

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