LinuxQuestions.org
Visit Jeremy's Blog.
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 01-11-2012, 09:44 AM   #1
shibaa
LQ Newbie
 
Registered: Jan 2012
Posts: 4

Rep: Reputation: Disabled
Replacing underscore with a space in a file name recursively.


I have seen several threads about replacing a space with an underscore, however I want to do exactly the opposite, I wants to remove the underscores and replace them with a space.

Last edited by shibaa; 01-12-2012 at 05:49 AM. Reason: edited to add solved.
 
Old 01-11-2012, 09:58 AM   #2
Telengard
Member
 
Registered: Apr 2007
Location: USA
Distribution: Kubuntu 8.04
Posts: 579
Blog Entries: 8

Rep: Reputation: 147Reputation: 147
With Perl rename:

Code:
$ ls -1Q
"file_1"
"file_2"
"file_3"
$ rename 'y/_/ /' *                                                      
test$ ls -1Q
"file 1"
"file 2"
"file 3"
$
If you need recursion, you can use the find command.
 
Old 01-11-2012, 02:49 PM   #3
shibaa
LQ Newbie
 
Registered: Jan 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
Ok so this lists all my files: find Music -name '*.mp3' and this will list files after rename, rename -n 'y/_/ /' * But having an issue combining the 2, tried find Music -name '*.mp3' -exec COMMAND rename -n 'y/_/ /' * and get error find: missing argument to `-exec'
I am assuming I need to use the exec option of find but not sure of syntax I need.
 
Old 01-11-2012, 03:06 PM   #4
Telengard
Member
 
Registered: Apr 2007
Location: USA
Distribution: Kubuntu 8.04
Posts: 579
Blog Entries: 8

Rep: Reputation: 147Reputation: 147
Did you not read the manual for rename? It accepts file names from STDIN. You should be able to do something like this:

Code:
find /PATH/TO/ -type f -name 'PATTERN' -print | rename 'y/_/ /'
EDIT
If you would prefer a GUI tool, I am aware of KRename, but have not used it.

Last edited by Telengard; 01-11-2012 at 04:43 PM. Reason: alternate solution
 
Old 01-11-2012, 11:27 PM   #5
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,561

Rep: Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939
You have originally said:
Quote:
I have seen several threads about replacing a space with an underscore
What was confusing about switching the characters in the examples you found?
The process to switch any single character for another should work for virtually all characters.
 
Old 01-12-2012, 05:49 AM   #6
shibaa
LQ Newbie
 
Registered: Jan 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
Most involved several lines to making a script and was to involved for what I wanted. Issue has since been resolved.
 
Old 01-12-2012, 08:51 AM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948
For the record, while the *nix OS and filesystems are very flexible in what characters can be used in filenames (pretty much anything except "/" and null), using any character that's considered special by the shell can be problematic.

For that reason, most experienced users try to avoid having spaces and other shell-reserved characters in their names. It tends to make operations in the cli console much smoother when you don't have to quote/escape every third character.

So my advice would be to keep the underscores where they are, unless you have a real good reason not to. In fact, I'd even go further and suggest avoiding all non-ascii characters whenever possible. Typing such entries can be quite impossible from a cli-only terminal (with no X system running).
 
Old 01-12-2012, 11:40 AM   #8
Telengard
Member
 
Registered: Apr 2007
Location: USA
Distribution: Kubuntu 8.04
Posts: 579
Blog Entries: 8

Rep: Reputation: 147Reputation: 147
Quote:
Originally Posted by shibaa View Post
Most involved several lines to making a script and was to involved for what I wanted. Issue has since been resolved.
You mentioned reading other threads related to your request. For the sake of helping others who may find this thread in the future, what was your solution?
 
Old 01-12-2012, 12:59 PM   #9
shibaa
LQ Newbie
 
Registered: Jan 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
The solution was yours:

Code:
find Music -type f -name '*.mp3' -print | rename 'y/_/ /'
Where Music is the folder holding all my music and it worked great, did recursive, changed all my files, are for the few files that I had duplicates of without the underscore it came back with a list of files that would not changed as file already existed.

This is the first time I had piped output. I have seen it done before, did try with improper syntax on a previous attempt and that when I got looking at the exec option on find. But the pipe worked great.

As for other posts in thread warning that underscore is better in file names. i would agree, but the app I am using searches for music by name, and present an issue for people to have to type a multi part name with underscores hoping to match exactly, by having the space the app finds files the way it should. For example searching Hotel California will not find anything as it was looking for Hotel_California. you could do Hotel or California separately but not as "Hotel California" which is preferred.

The app in question is opensource so perhaps in the future I can make it translate the underscores to spaces in the array of files it holds so can still perform easier user search but maintain the underscore in file names. For the time being the removal of underscores seems fastest option.

edit: would like to add that the process took about 2 seconds to convert 12k songs, that what I love about cli is the speed and power, unfortunately I am still a noob and still learning.

Last edited by shibaa; 01-12-2012 at 01:03 PM.
 
Old 01-12-2012, 01:38 PM   #10
Telengard
Member
 
Registered: Apr 2007
Location: USA
Distribution: Kubuntu 8.04
Posts: 579
Blog Entries: 8

Rep: Reputation: 147Reputation: 147
Quote:
Originally Posted by shibaa View Post
This is the first time I had piped output.
...
unfortunately I am still a noob and still learning.
Had I known, maybe I could have been more helpful. See the links in my signature for Bash documentation and a very good tutorial. Many more CLI tutorials are listed in this article of my blog. Hope you'll find something you like.

Quote:
the app I am using searches for music by name, and present an issue for people to have to type a multi part name with underscores hoping to match exactly, by having the space the app finds files the way it should. For example searching Hotel California will not find anything as it was looking for Hotel_California. you could do Hotel or California separately but not as "Hotel California" which is preferred.
IMHO, this kind of contextual information should be included in your help request. The more relevant information you provide, the more likely you are to get a helpful answer.

Anyway, it is good to know your problem was resolved.

EDIT
I almost forgot to mention, there is a very nice article linked in David the H.'s signature. It is intended to guide new LQ users in writing an effective help request.

Last edited by Telengard; 01-12-2012 at 01:47 PM.
 
  


Reply

Tags
find, rename


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
how do i convert file names to lower case and remove the underscore from the name? asilentmurmur Linux - Newbie 16 10-30-2012 06:52 PM
[SOLVED] Replacing space characters w/ '\ ' using tr NirvanaII Programming 9 06-22-2010 05:20 PM
sed, replacing underscore with whitespace fjkum Programming 3 10-31-2007 12:09 AM
renaming files recursively by replacing a character in filename etechnophile Programming 3 05-04-2007 11:26 PM
replacing space in filenames rooster13 Linux - General 2 07-18-2003 04:35 AM


All times are GMT -5. The time now is 07:49 PM.

Main Menu
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