Re: remove double quote character from file names

From: Sysadmin Lists <sysadmin.lists_at_mailfence.com>
Date: Thu, 16 Feb 2023 08:35:35 UTC
> ----------------------------------------
> From: Sysadmin Lists <sysadmin.lists@mailfence.com>
> Date: Feb 15, 2023, 11:20:09 PM
> To: <freebsd-questions@freebsd.org>
> Cc: Per olof Ljungmark <peo@nethead.se>
> Subject: Re: remove double quote character from file names
> 
> 
>  ----------------------------------------
> > From: Per olof Ljungmark <peo@nethead.se>
> > Date: Feb 11, 2023, 6:58:50 AM
> > To: <freebsd-questions@freebsd.org>
> > Subject: remove double quote character from file names
> > 
> > 
> > Hi all,
> > 
> > A little help on the way, I need to find and remove the double quote (") 
> > character from all files in a directory structure containing hundreds of 
> > thousands of files.
> > 
> > I am sure plenty of you have done this before... I've gotten as far as
> > 
> > find . -type f -name '*"*' -exec rename 's|"|in|g' {} \;
> > find: rename: No such file or directory
> > 
> > The find part works but not renaming so I'm missing something there.
> > 
> > Thanks,
> > Per
> > 
> > 
> 
> Just to throw in an awk-themed solution:
> $ ls -1 | awk '/"/ { system("mv -v '\''" $0 "'\'' " $0) }'
> 
> $ touch "\"foo bar\"" \"baz\" \".zap\" xyz abc
> $ ls -1A
> ".zap"
> "baz"
> "foo bar"
> abc
> xyz
> $ ls -1 | awk '/"/ { system("mv -v '\''" $0 "'\'' " $0) }'
> ".zap" -> .zap
> "baz" -> baz
> "foo bar" -> foo bar
> $ ls -1A
> .zap
> abc
> baz
> foo bar
> xyz
> 
> There's a clever use of the existing double-quotes in the filenames in the renaming.
Here's what the shell sees:
$ ls -1 | awk '/"/ { print("mv -v '\''" $0 "'\'' " $0) }'
mv -v '".zap"' ".zap"
mv -v '"baz"' "baz"
mv -v '"foo bar"' "foo bar"

"Directory structure." In that case,
find . -type f -execdir awk -v file='{}' 'BEGIN { if (file ~ /"/) system("mv -v '\''" file "'\'' " file) }' \;
or
find . -type f -name \"\*\" -execdir awk -v file='{}' 'BEGIN { system("mv -v '\''" file "'\'' " file) }' \;

Again, replacing system() with print() shows what the shell sees.


-- 
Sent with https://mailfence.com  
Secure and private email