Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 289
  • Last Modified:

extension changing

i want to change all the files' extensions to another one in an directory
with a shell prompt ..
it will be executed like
a.sh c cpp and all the files' extensions will be converted from c to cpp??

thanx to your all..
0
btocakci
Asked:
btocakci
3 Solutions
 
brettmjohnsonCommented:
Use the 'rename' command.  From the man page:

NAME
        rename - bulk rename files

SYNOPSIS
        rename fromtext totext files...

DESCRIPTION
        rename will rename the specified files by replacing the
        first occurrence of fromtext in their name with totext.

        For example, given the files foo1, ..., foo9, foo10, ..., foo278,
        the commands

          rename foo foo0 foo?
          rename foo foo0 foo??

        will turn them into foo001, ..., foo009, foo010, ..., foo278.

        And

          rename .htm .html *.htm

        will fix the extension of your html files.
0
 
alextoftCommented:
Syntax:
changeext <old-ext> <new-ext>
Example:
changeext c cpp

Save this to /usr/local/bin/changeext and chmod it a+x

#!/bin/bash
for file in *.$1 ; do mv $file `echo $file | sed 's/\(.*\.\)$1/\1$2/'` ; done
0
 
devrick0Commented:
Or you could just skip the script, skip adding in extra variables, and just run a single line in the directory like this (there's plenty of ways to do what you're looking to do, it's just a matter of finding what you like best).

$ ls                                                                                                                                      
1.c  2.c  3.c  4.c  5.c
$ ls | sed -n 's/\(.*\)c/mv \1c \1cpp/p' | bash                                                                                          
$ ls                                                                                                                                      
1.cpp  2.cpp  3.cpp  4.cpp  5.cpp
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now