You are not logged in.

#1 2010-01-02 08:22:15

gogi-goji
Member
From: Canada
Registered: 2009-10-20
Posts: 73
Website

[SOLVED] Find and replace quotes with underscores in file names

I was looking to replace all of the quotes in file names in a directory with underscores.  I seem to be having a problem doing so, though.  Running the find command:

find . -name '*[\`\'\"]*' -exec sh -c "mv -i '$0' '${0//[\`\'\"]/_}'" {} \;

I get a > inviting me to continue the command.  What am I doing wrong?

Last edited by gogi-goji (2010-01-03 02:19:13)


My (sporadically updated) blog
My miscellaneous dotfiles

Offline

#2 2010-01-02 09:27:12

JohannesSM64
Member
From: Norway
Registered: 2009-10-11
Posts: 623
Website

Re: [SOLVED] Find and replace quotes with underscores in file names

For just " and `

% for i (**/*) mv $i $(echo $i|sed 's|[`"]|_|')

Works great in zsh. For single quotes, no luck..
edit: Got it! but I have to do it by itself for some reason:

for i (**/*) mv $i $(echo $i|sed s/\'/_/)

Last edited by JohannesSM64 (2010-01-02 09:35:48)

Offline

#3 2010-01-02 15:39:57

tlvb
Member
From: Sweden
Registered: 2008-10-06
Posts: 297
Website

Re: [SOLVED] Find and replace quotes with underscores in file names

Bash and perl, file under ugly:...

$ find . -name "*[\`\"']*"| perl -e 'foreach (<stdin>) {s/([`"\x27])/\\\1/g; print; s/\\[`"\x27]/_/g; print}'|xargs -n2 mv

I need a sorted list of all random numbers, so that I can retrieve a suitable one later with a binary search instead of having to iterate through the generation process every time.

Offline

#4 2010-01-02 16:03:01

clickit
Member
From: Athens, Hellas
Registered: 2007-07-18
Posts: 93

Re: [SOLVED] Find and replace quotes with underscores in file names

I would like to suggest, if you prefer something else than command line, GPRename
find it here http://www.archlinux.org/packages/commu … /gprename/

Offline

#5 2010-01-02 17:26:42

dmz
Member
From: Sweden
Registered: 2008-08-27
Posts: 881
Website

Re: [SOLVED] Find and replace quotes with underscores in file names

prename my friend.

prename 's/"/_/g' */*

Offline

#6 2010-01-03 02:18:41

gogi-goji
Member
From: Canada
Registered: 2009-10-20
Posts: 73
Website

Re: [SOLVED] Find and replace quotes with underscores in file names

tlvb wrote:

Bash and perl, file under ugly:...

$ find . -name "*[\`\"']*"| perl -e 'foreach (<stdin>) {s/([`"\x27])/\\\1/g; print; s/\\[`"\x27]/_/g; print}'|xargs -n2 mv

God, that's ugly as hell, but worked perfectly.  Thank you!


My (sporadically updated) blog
My miscellaneous dotfiles

Offline

#7 2010-01-03 04:14:58

JohannesSM64
Member
From: Norway
Registered: 2009-10-11
Posts: 623
Website

Re: [SOLVED] Find and replace quotes with underscores in file names

gogi-goji wrote:
tlvb wrote:

Bash and perl, file under ugly:...

$ find . -name "*[\`\"']*"| perl -e 'foreach (<stdin>) {s/([`"\x27])/\\\1/g; print; s/\\[`"\x27]/_/g; print}'|xargs -n2 mv

God, that's ugly as hell, but worked perfectly.  Thank you!

Mine is less ugly and works perfectly.

Offline

#8 2010-01-04 00:27:08

lolilolicon
Member
Registered: 2009-03-05
Posts: 1,722

Re: [SOLVED] Find and replace quotes with underscores in file names

No, you don't want perl, or zsh, or all that bloat.

find . -name "*[\`\"']*" | while read i; do mv -i "$i" "${i//[\"\'\`]/_}"; done

I don't understand the -exec part of your code, but I'm 1000% sure it has to do with the (twisted) escaping.
Even if you get that line right, it will be f**king ugly. I've always hated all that eval crap, escaping being one of the reasons.


This silver ladybug at line 28...

Offline

#9 2010-01-04 09:21:20

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: [SOLVED] Find and replace quotes with underscores in file names

@lolilolicon: yeah the -exec part does give another level of quotes, so you can run into trouble with scripts inside -exec.

I didn't know you could give {} as an argument after sh -c and $0, that solves the problem of not knowing which quotes to use around it in the -exec part.

$  find -mindepth 1 -exec sh -c 'mv -v --backup=t "$0" "$(echo "$0" | sed s/[\\x27\"\`]/___/g)"' {} \;
mv: `./a b' and `./a b' are the same file
`./\'' -> `./___'
`./\'"\'' -> `./_________'
mv: `./$0' and `./$0' are the same file
mv: `./a' and `./a' are the same file
mv: `./$(echo crash)' and `./$(echo crash)' are the same file
`./"' -> `./___' (backup: `./___.~1~')
`./\'\'\'' -> `./_________' (backup: `./_________.~1~')

Or with the other substitution method, it doesn't interpret \x27 (you could use $(echo -e \\x27) or you have to use ...'\\\''...

$ find -mindepth 1 -exec sh -c 'echo mv -v --backup=t "$0" "${0//['\\\''\"\`]/___}"' {} \;
$ find -mindepth 1 -exec sh -c 'echo mv -v --backup=t "$0" "${0//[$(echo -e \\x27)\"\`]/___}"' {} \;
mv -v --backup=t ./a b ./a b
mv -v --backup=t ./' ./___
mv -v --backup=t ./___ ./___
mv -v --backup=t ./'"' ./_________
mv -v --backup=t ./$0 ./$0
mv -v --backup=t ./___.~1~ ./___.~1~
mv -v --backup=t ./a ./a
mv -v --backup=t ./$(echo crash) ./$(echo crash)
mv -v --backup=t ./_________ ./_________
mv -v --backup=t ./" ./___
mv -v --backup=t ./''' ./_________
mv -v --backup=t ./_________.~1~ ./_________.~1~

Offline

#10 2010-01-04 11:30:04

lolilolicon
Member
Registered: 2009-03-05
Posts: 1,722

Re: [SOLVED] Find and replace quotes with underscores in file names

Really clear presentation there!

Procyon wrote:

or you have to use ...'\\\''...

Or, for a slightly more accurate expression: '...'\\\''...'
Using \'"'"' or \'\'' in place of '\\\'' will work too. So I think I got it too. At least the usage.

Thanks, Procyon smile

Last edited by lolilolicon (2010-01-04 11:34:22)


This silver ladybug at line 28...

Offline

Board footer

Powered by FluxBB