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 > changing extention
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

changing extention

Réponse
 
LinkBack Outils de la discussion
Vieux 24/03/2008, 12h43   #1
sid
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut changing extention

I want to know if it it possible to change a filename extention to
another one using a shell script. eg. Reading a foo.c file and
renaming it to foo.cpp using a script and this is read using a for
loop, so say $i=foo.c, now the $i has to be changed to get foo.cpp.
I've been able to do it using 'basename' is there some other way to do
it?
  Réponse avec citation
Vieux 24/03/2008, 13h04   #2
pk
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: changing extention

sid wrote:

> I want to know if it it possible to change a filename extention to
> another one using a shell script. eg. Reading a foo.c file and
> renaming it to foo.cpp using a script and this is read using a for
> loop, so say $i=foo.c, now the $i has to be changed to get foo.cpp.
> I've been able to do it using 'basename' is there some other way to do
> it?


Please provide sample input and expected output.

--
All the commands are tested with bash and GNU tools, so they may use
nonstandard features. I try to mention when something is nonstandard (if
I'm aware of that), but I may miss something. Corrections are welcome.
  Réponse avec citation
Vieux 24/03/2008, 13h19   #3
Hermann Peifer
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: changing extention

sid wrote:
> I want to know if it it possible to change a filename extention to
> another one using a shell script. eg. Reading a foo.c file and
> renaming it to foo.cpp using a script and this is read using a for
> loop, so say $i=foo.c, now the $i has to be changed to get foo.cpp.
> I've been able to do it using 'basename' is there some other way to do
> it?


I would do this in bash:

for f in *.c; do mv "$f" "${f%.c}".cpp; done

Hermann
  Réponse avec citation
Vieux 24/03/2008, 23h06   #4
Chris F.A. Johnson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: changing extention

On 2008-03-24, sid wrote:
> I want to know if it it possible to change a filename extention to
> another one using a shell script. eg. Reading a foo.c file and
> renaming it to foo.cpp using a script and this is read using a for
> loop, so say $i=foo.c, now the $i has to be changed to get foo.cpp.
> I've been able to do it using 'basename' is there some other way to do
> it?


Since all you need to do is add 'pp':

mv "$i" "${i}pp"

If you want to change any extension to '.cpp':

mv "$i" "${i%.*}.cpp"

--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell/>
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
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 00h32.


É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,10658 seconds with 12 queries