LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-03-2009, 09:23 AM   #1
smithjb
LQ Newbie
 
Registered: Jan 2009
Posts: 5

Rep: Reputation: 0
Using Sed to Rename A Group of Files


Hello,

I have a lot of files on a Fedora 9 box that contains '+' in the middle of the file name and I would like to remove them. I believe this should be doable using sed. Here's what I've got so far:

Code:
ls -d *+* | sed 's/+//g'
This however returns the correct results, but if I do another ls, nothing was really changed.

What am I missing?

Thanks,
 
Old 05-03-2009, 09:37 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Using sed that way, you don't really rename the files, but you simply parse the output of the ls command. To change file names, you can try the rename command (from inside the directory you have the files with +):
Code:
rename "+" "" *
 
Old 05-03-2009, 09:43 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
You can also use substring replacement in bash:
Code:
for file in *+*
do
  mv $file ${file//"+"/""}
done
or if you want to use sed:
Code:
for file in *+*
do
  mv $file $(echo $file | sed 's/+//g')
done
 
Old 05-03-2009, 09:48 AM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
SED processes whatever you give it, and displays it on "STDOUT"---by default, your terminal window. It does not change filenames---that is done with the "mv" command.

why "ls -d" ?

I think you need something like this:
for filename in *; do newname= $(sed 's/+//g' $filename); mv $filename $newname; done

To drill down in the directory tree, use "$(ls -R) instead of "*"
 
Old 05-03-2009, 09:50 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by pixellany View Post
To drill down in the directory tree, use "$(ls -R) instead of "*"
Maybe the OP does not want to descend into directories, hence the -d option to avoid directory listing. Maybe.
 
Old 05-03-2009, 09:00 PM   #6
smithjb
LQ Newbie
 
Registered: Jan 2009
Posts: 5

Original Poster
Rep: Reputation: 0
Thanks everyone for your help.

The following works on some of the files:

Code:
for file in *+*
do
  mv $file $(echo $file | sed 's/+//g')
done
But I have a several files like "Judge+++++ 1-23-98" which I receive:

Quote:
mv: target `1-23-98.DOC' is not a directory
Even if I try to also remove the space along with the '+' I receive:

Quote:
mv: target `Judge+++++1-23-98.DOC' is not a directory
Thanks!
 
Old 05-03-2009, 09:27 PM   #7
Robhogg
Member
 
Registered: Sep 2004
Location: Old York, North Yorks.
Distribution: Debian 7 (mainly)
Posts: 653

Rep: Reputation: 97
The problem is that the space causes mv to see everything after it as a separate file (or directory) name (in both parameters). If you put double-quotes around the parameters, it will "protect" the space, and you may want to alter the sed command to take account of the multiple +'s and space:
Code:
mv "$file" "$(echo $file | sed 's/[+ ]*//g')"
You could also slap the person sending you these files with a wet fish, until they start using sensible file names .
 
  


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
How to rename a vol group? binary_0011 Linux - General 18 01-13-2013 10:33 AM
sed/awk group on first column Eddie Adams Linux - General 4 04-09-2009 10:23 AM
sed: howto group regex's into AND/OR clauses? jhwilliams Linux - Software 5 08-01-2007 02:19 PM
how to rename files using sed? seimour Linux - Newbie 1 05-02-2007 01:46 AM
rename a group of files to a single extention Lleb_KCir Linux - General 9 02-20-2005 12:40 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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