PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Serveur - Sécurité et techniques > comp.unix.shell > how to escape special chars in filenames
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

how to escape special chars in filenames

Réponse
 
LinkBack Outils de la discussion
Vieux 21/08/2006, 02h08   #1
Jerry Fleming
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut how to escape special chars in filenames

I have a list (about 600) of files whose names contain special
characters, such as spaces, &, (. Under bash, they have to be escaped. I
was wondering if there is any command, or even a piece of sed, which can
be used to escape them.
  Réponse avec citation
Vieux 21/08/2006, 02h20   #2
Chris F.A. Johnson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: how to escape special chars in filenames

On 2006-08-21, Jerry Fleming wrote:
> I have a list (about 600) of files whose names contain special
> characters, such as spaces, &, (. Under bash, they have to be escaped. I
> was wondering if there is any command, or even a piece of sed, which can
> be used to escape them.


How are you using the names?

The usual method is to quote the file name:

for file in ./*
do
cat "$file"
done

--
Chris F.A. Johnson, author <http://cfaj.freeshell.org>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
  Réponse avec citation
Vieux 21/08/2006, 03h31   #3
Jerry Fleming
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: how to escape special chars in filenames

Chris F.A. Johnson wrote:
> On 2006-08-21, Jerry Fleming wrote:
>> I have a list (about 600) of files whose names contain special
>> characters, such as spaces, &, (. Under bash, they have to be escaped. I
>> was wondering if there is any command, or even a piece of sed, which can
>> be used to escape them.

>
> How are you using the names?
>
> The usual method is to quote the file name:
>
> for file in ./*
> do
> cat "$file"
> done
>

I am using the names with scp:

scp special_file root@host:~/"special_file"

Here I have to quote the destination special_file twice, one with double
quotes, one with escapes. Only double quotes won't work. I am wondering
how to escape special chars of filenames in a script.
  Réponse avec citation
Vieux 21/08/2006, 03h45   #4
Chris F.A. Johnson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: how to escape special chars in filenames

On 2006-08-21, Jerry Fleming wrote:
> Chris F.A. Johnson wrote:
>> On 2006-08-21, Jerry Fleming wrote:
>>> I have a list (about 600) of files whose names contain special
>>> characters, such as spaces, &, (. Under bash, they have to be escaped. I
>>> was wondering if there is any command, or even a piece of sed, which can
>>> be used to escape them.

>>
>> How are you using the names?
>>
>> The usual method is to quote the file name:
>>
>> for file in ./*
>> do
>> cat "$file"
>> done
>>

> I am using the names with scp:
>
> scp special_file root@host:~/"special_file"
>
> Here I have to quote the destination special_file twice, one with double
> quotes, one with escapes. Only double quotes won't work. I am wondering
> how to escape special chars of filenames in a script.


Use escaped quotes:

scp special_file root@host:~/"\"special file\""

scp special_file root@host:~/"\"$file\""


--
Chris F.A. Johnson, author <http://cfaj.freeshell.org>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
  Réponse avec citation
Vieux 21/08/2006, 03h56   #5
Xicheng Jia
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: how to escape special chars in filenames

Jerry Fleming wrote:
> Chris F.A. Johnson wrote:
> > On 2006-08-21, Jerry Fleming wrote:
> >> I have a list (about 600) of files whose names contain special
> >> characters, such as spaces, &, (. Under bash, they have to be escaped. I
> >> was wondering if there is any command, or even a piece of sed, which can
> >> be used to escape them.

> >
> > How are you using the names?
> >
> > The usual method is to quote the file name:
> >
> > for file in ./*
> > do
> > cat "$file"
> > done
> >

> I am using the names with scp:
>
> scp special_file root@host:~/"special_file"
>
> Here I have to quote the destination special_file twice, one with double
> quotes, one with escapes. Only double quotes won't work. I am wondering
> how to escape special chars of filenames in a script.


If you dont want to change the destination filename, then use 'dot' is
enough, and no need to add ~/ to specify home directory(default).

scp "$special_file" root@host:.

But if the SRC of scp command isn't a shell variable, then using
single-quotes is safer:

scp 'special_file' root@host:.

you can also use TAB auto-completion, shell can add proper backslashes
for you.

Xicheng

  Réponse avec citation
Vieux 21/08/2006, 04h29   #6
Jerry Fleming
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: how to escape special chars in filenames

Xicheng Jia wrote:
> Jerry Fleming wrote:
>> Chris F.A. Johnson wrote:
>>> On 2006-08-21, Jerry Fleming wrote:
>>>> I have a list (about 600) of files whose names contain special
>>>> characters, such as spaces, &, (. Under bash, they have to be escaped. I
>>>> was wondering if there is any command, or even a piece of sed, which can
>>>> be used to escape them.
>>> How are you using the names?
>>>
>>> The usual method is to quote the file name:
>>>
>>> for file in ./*
>>> do
>>> cat "$file"
>>> done
>>>

>> I am using the names with scp:
>>
>> scp special_file root@host:~/"special_file"
>>
>> Here I have to quote the destination special_file twice, one with double
>> quotes, one with escapes. Only double quotes won't work. I am wondering
>> how to escape special chars of filenames in a script.

>
> If you dont want to change the destination filename, then use 'dot' is
> enough, and no need to add ~/ to specify home directory(default).
>
> scp "$special_file" root@host:.
>
> But if the SRC of scp command isn't a shell variable, then using
> single-quotes is safer:
>
> scp 'special_file' root@host:.
>
> you can also use TAB auto-completion, shell can add proper backslashes
> for you.
>
> Xicheng
>

Thanks for all your . But I am using scp in a non-interactive
script, so auto-completion is no , and I do want the destination
filename to be different than the source. I have to escape the special
chars with backslashes. The problem is that I don't know where to insert
the backslashes in the filenames and where.
  Réponse avec citation
Vieux 21/08/2006, 04h54   #7
Jon LaBadie
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: how to escape special chars in filenames

Jerry Fleming wrote:
> Xicheng Jia wrote:
>> Jerry Fleming wrote:
>>> Chris F.A. Johnson wrote:
>>>> On 2006-08-21, Jerry Fleming wrote:
>>>>> I have a list (about 600) of files whose names contain special
>>>>> characters, such as spaces, &, (. Under bash, they have to be
>>>>> escaped. I
>>>>> was wondering if there is any command, or even a piece of sed,
>>>>> which can
>>>>> be used to escape them.
>>>> How are you using the names?
>>>>
>>>> The usual method is to quote the file name:
>>>>
>>>> for file in ./*
>>>> do
>>>> cat "$file"
>>>> done
>>>>
>>> I am using the names with scp:
>>>
>>> scp special_file root@host:~/"special_file"
>>>
>>> Here I have to quote the destination special_file twice, one with double
>>> quotes, one with escapes. Only double quotes won't work. I am wondering
>>> how to escape special chars of filenames in a script.

>>
>> If you dont want to change the destination filename, then use 'dot' is
>> enough, and no need to add ~/ to specify home directory(default).
>>
>> scp "$special_file" root@host:.
>>
>> But if the SRC of scp command isn't a shell variable, then using
>> single-quotes is safer:
>>
>> scp 'special_file' root@host:.
>>
>> you can also use TAB auto-completion, shell can add proper backslashes
>> for you.
>>
>> Xicheng
>>

> Thanks for all your . But I am using scp in a non-interactive
> script, so auto-completion is no , and I do want the destination
> filename to be different than the source. I have to escape the special
> chars with backslashes. The problem is that I don't know where to insert
> the backslashessed in the filenames and where.



sed 's/[^x]/\\&/g'

  Réponse avec citation
Vieux 21/08/2006, 04h58   #8
Xicheng Jia
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: how to escape special chars in filenames

Jerry Fleming wrote:
> Xicheng Jia wrote:
> > Jerry Fleming wrote:
> >> Chris F.A. Johnson wrote:
> >>> On 2006-08-21, Jerry Fleming wrote:
> >>>> I have a list (about 600) of files whose names contain special
> >>>> characters, such as spaces, &, (. Under bash, they have to be escaped. I
> >>>> was wondering if there is any command, or even a piece of sed, which can
> >>>> be used to escape them.
> >>> How are you using the names?
> >>>
> >>> The usual method is to quote the file name:
> >>>
> >>> for file in ./*
> >>> do
> >>> cat "$file"
> >>> done
> >>>
> >> I am using the names with scp:
> >>
> >> scp special_file root@host:~/"special_file"
> >>
> >> Here I have to quote the destination special_file twice, one with double
> >> quotes, one with escapes. Only double quotes won't work. I am wondering
> >> how to escape special chars of filenames in a script.

> >
> > If you dont want to change the destination filename, then use 'dot' is
> > enough, and no need to add ~/ to specify home directory(default).
> >
> > scp "$special_file" root@host:.
> >
> > But if the SRC of scp command isn't a shell variable, then using
> > single-quotes is safer:
> >
> > scp 'special_file' root@host:.
> >
> > you can also use TAB auto-completion, shell can add proper backslashes
> > for you.
> >
> > Xicheng
> >

> Thanks for all your . But I am using scp in a non-interactive
> script, so auto-completion is no , and I do want the destination
> filename to be different than the source. I have to escape the special
> chars with backslashes. The problem is that I don't know where to insert
> the backslashes in the filenames and where.


You don't need to insert backslashes, use the way Chris mentioned in
his previous post(some minor modification):

scp 'src_special_name' root@host:"'dest_special_name'"

scp "$src_special_val" root@host:"'$dest_special_val'"

Be careful with the positions of single-quotes(inside) and
double-quotes(outside) in the DESC part.

Good luck,
Xicheng

  Réponse avec citation
Vieux 21/08/2006, 09h17   #9
Stachu 'Dozzie' K.
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: how to escape special chars in filenames

On 21.08.2006, Jerry Fleming <jerry@dd.comlk> wrote:
>>> I am using the names with scp:
>>>
>>> scp special_file root@host:~/"special_file"
>>>
>>> Here I have to quote the destination special_file twice, one with double
>>> quotes, one with escapes. Only double quotes won't work. I am wondering
>>> how to escape special chars of filenames in a script.

[...]
> Thanks for all your . But I am using scp in a non-interactive
> script, so auto-completion is no , and I do want the destination
> filename to be different than the source. I have to escape the special
> chars with backslashes. The problem is that I don't know where to insert
> the backslashes in the filenames and where.


And how about getting that done different way? Copy all files from local
host to remote and rename them on remote host?

tar zcf - files ... | ssh root@host 'cd destination; tar zxf -; now-rename-files'

--
Stanislaw Klekot
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 16h55.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,22691 seconds with 17 queries