LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Replacing underscore with a space in a file name recursively. (https://www.linuxquestions.org/questions/linux-newbie-8/replacing-underscore-with-a-space-in-a-file-name-recursively-923254/)

shibaa 01-11-2012 09:44 AM

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.

Telengard 01-11-2012 09:58 AM

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.

shibaa 01-11-2012 02:49 PM

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.

Telengard 01-11-2012 03:06 PM

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.

grail 01-11-2012 11:27 PM

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.

shibaa 01-12-2012 05:49 AM

Most involved several lines to making a script and was to involved for what I wanted. Issue has since been resolved.

David the H. 01-12-2012 08:51 AM

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).

Telengard 01-12-2012 11:40 AM

Quote:

Originally Posted by shibaa (Post 4572677)
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?

shibaa 01-12-2012 12:59 PM

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.

Telengard 01-12-2012 01:38 PM

Quote:

Originally Posted by shibaa (Post 4572995)
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.


All times are GMT -5. The time now is 06:19 PM.