LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to modify sets of filenames on cmd line? (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-modify-sets-of-filenames-on-cmd-line-256810/)

Kropotkin 11-19-2004 07:48 AM

How to modify sets of filenames on cmd line?
 
Hi all,

I am in the process of moving a large collection of MP3s from an OS/2 partition. First, I copied them all to CDRs and now I will copy them to my Fedora drive.

One hiccup: many of the filenames have accented characters (ie, é, ó, á) and they look like this under Linux:

Code:

Cuco Valoy - Coraz?n abierto.mp3 (invalid Unicode)
I have several hundred files like this and I'd rather not to have to fix them all manually. Is there a way of doing this on the command line with regular expressions or such like?

Thanks for your time and have a nice day...

rjlee 11-19-2004 08:23 AM

This problem is probably caused by having the wrong character set on the original OS/2 filesystem; try using the charmap=character-set option in /etc/fstab.

Without knowing which character set you were using, it's difficult to work out what the characters were or should be, so fixing the mount options of your original filesystem is the best place to start.

meblost 11-19-2004 08:33 AM

If you do know what you need to replace, the tr command will help. You could try something like this:

for foo in $(find . -type f -maxdepth 1);do mv $foo $(echo $foo | tr A a);done

This will rename each regular file in the current dir and replace all A's with a's.

Kropotkin 11-19-2004 09:12 AM

Quote:

Originally posted by rjlee
This problem is probably caused by having the wrong character set on the original OS/2 filesystem; try using the charmap=character-set option in /etc/fstab.

Without knowing which character set you were using, it's difficult to work out what the characters were or should be, so fixing the mount options of your original filesystem is the best place to start.

Hi, I was using codepages 437,850 under OS/2. I don't think they were "wrong" in the sense that they were correct under OS/2. However, for some reason they don't translate properly to Linux.

I can't find any documentation on using a charmap option either in /etc/fstab or when mounting a CDROM on the command line with mount. Are you sure this is possible?

Kropotkin 11-19-2004 09:52 AM

Quote:

Originally posted by meblost

for foo in $(find . -type f -maxdepth 1);do mv $foo $(echo $foo | tr A a);done

This looks very useful. However, when I try a somewhat simplifed version of it

Code:

for foo in $(find *.mp3);do mv $foo (echo $foo|tr A a);done
or even

Code:

mv coraz?n.txt (echo coraz?n.mp3|tr A a)
I get an error message: bash: syntax error near unexpected token `('
. Does this work under every shell?

scottman 11-19-2004 10:03 AM

You may have typoed the $ before the ()'s. Other possibilities would be using the * wildcard, or the ? single character substitution. The asterisk matches any and all characters, the ? matches any one character. This would work from the directory with all your mp3s.

Code:

cp ./*.mp3 /destination
Or just do a recursive copy if you have multiple directories. This will copy all files inside the parent directory.

Code:

cp -R /mnt/cdrom /destination
Or you could always attempt tab completion if typing out a single name.

edit: woops, sorry, misunderstood the first post, disregard

meblost 11-19-2004 12:24 PM

You need
for foo in $(find . -name *.mp3);do mv $foo $(echo $foo|tr A a);done

and

mv coraz?n.txt $(echo coraz?n.mp3|tr A a)

I am using bash. I'm sure the syntax is different for other shells. Also, It might be treating the '?' as a regular expression. Mabye you need to escape the '?' (ie substitute coraz?n.txt with coraz\?n.txt, etc.)

Kropotkin 11-19-2004 05:16 PM

Well, I can get this to work on a string without accented characters, but I can't get tr to filter the accented characters properly. For example, the ó in the word corazón is represented by an empty box on the command line (and a question mark if I just paste it in here). However, when you copy the file on the command line, the OS represents it with a \242.

Code:

$ cp Coraz?n.mp3 Coraz?n2.mp3
`Coraz\242n.mp3' -> `Coraz\242n2.mp3'

However, this does not work:

Code:

mv Coraz?n.mp3 $(echo Coraz?n.mp3 | tr \242 ó)
Sigh... :(

Kropotkin 11-20-2004 06:35 AM

solved another way
 
Hi all,

After tinkering with the shell stuff and not getting anywhere, I cobbled together a script in Rexx which does everything I need. The tricky bit was figuring out what exactly the illegal characters are, since Linux represents them all as an empty box on the command line. Well, they turn out to be just plain old ASCII codes. So, using the C2D() function in Rexx, I parse the filenames character by character looking for a buch of values like 130 ("é") and converting them to what Linux displays as the correct character. Probably some similar could be done in Python or Perl or perhaps even a shell script, but I am most familiar with Rexx.

Bye for now...


All times are GMT -5. The time now is 12:30 AM.