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.
|
With Perl rename:
Code:
$ ls -1Q |
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. |
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/_/ /' If you would prefer a GUI tool, I am aware of KRename, but have not used it. |
You have originally said:
Quote:
The process to switch any single character for another should work for virtually all characters. |
Most involved several lines to making a script and was to involved for what I wanted. Issue has since been resolved.
|
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). |
Quote:
|
The solution was yours:
Code:
find Music -type f -name '*.mp3' -print | rename 'y/_/ /' 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. |
Quote:
Quote:
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. |