Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove escaped characters in filenames?

Tags:

linux

bash

perl

I would like to remove escaped characters from filenames, so these examples

=Web_Help_Desk_Pro%26Lite.pdf
=Windows_7_%2b_s-drev.pdf

would become

=Web_Help_Desk_ProLite.pdf
=Windows_7__s-drev.pdf

Does anyone know how to do this in either Perl or BASH?

like image 630
Sandra Schlichting Avatar asked Jan 17 '23 20:01

Sandra Schlichting


2 Answers

If $file is your filename:

my $file = '=Web_Help_Desk_Pro%26Lite.pdf';
$file =~ s/%[0-9a-f]{2}//gi;

i.e. replace % followed by two hex characters with the empty string.

like image 120
Alnitak Avatar answered Jan 25 '23 15:01

Alnitak


To just remove the percent sign and the following two hex digits:

$path =~ s/%[\da-f][\da-f]//gi;
like image 35
ErikR Avatar answered Jan 25 '23 15:01

ErikR