LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Convert all file extensions in one directory to another file extension (http://www.linuxquestions.org/questions/linux-newbie-8/convert-all-file-extensions-in-one-directory-to-another-file-extension-929713/)

shayno90 02-16-2012 10:37 AM

Convert all file extensions in one directory to another file extension
 
I would like to convert about 40 files which are all of the same file extension to another file extension within the same directory or into a new directory.

e.g.

/usr/local/share/ovaldi/xml has 40 files with ".xsd" file extension

so either do this:

/usr/local/share/ovaldi/xml with all 40 files with ".oval" file extension
or
/usr/local/share/ovaldi/xml1 with all 40 files with ".oval" file extension

I have done something similar with individual files before but not a whole directory:

cat filename | awk -F ","{ print $4 } for example

Is it possible to do something similar to change the file extensions of all files within a directory or into a new directory?

Thanks in advance.

sag47 02-16-2012 11:00 AM

Code:

ls -1 | while read file;do mv "$file" "`echo $file | sed 's/\.xsd$/\.oval/'`";done
Where ls -1 is the number one.

There's also a built in way to modify variables in bash. These methods work and account for spaces in file names. Here's the variable modifier method..

Code:

ls -1 | while read file;do mv "$file" "${file%.xsd}.oval";done
Take your pick, they both work in bash.

shayno90 02-16-2012 11:04 AM

Quote:

Originally Posted by sag47 (Post 4604148)
Code:

ls -1 | while $file;do mv "$file" "`echo $file | sed 's/\.xsd$/\.xml/'`";done
There's also a built in way to modify variables in bash but that method works and accounts for spaces in file names.

I found this also but it did not work with an error related to the "do" part of the statement

for f in *.xsd; do mv $f `basename $f .xsd`.oval; done;

I will try you script above.

suicidaleggroll 02-16-2012 11:09 AM

Code:

for i in *.old; do mv "$i" "${i/.old/.new}"; done

shayno90 02-16-2012 11:13 AM

Quote:

Originally Posted by sag47 (Post 4604148)
Code:

ls -1 | while read file;do mv "$file" "`echo $file | sed 's/\.xsd$/\.oval/'`";done
Where ls -1 is the number one.

There's also a built in way to modify variables in bash. These methods work and account for spaces in file names. Here's the variable modifier method..

Code:

ls -1 | while read file;do mv "$file" "${file%.xsd}.oval";done
Take your pick, they both work in bash.

I "cd" to the correct directory but it did not work (did not specify the filename in the command)

user@user:/usr/local/share/ovaldi/xmltest$ sudo ls -1 | while $file;do mv "$file" "${file%.xsd}.oval";done
mv: cannot `' a `stat': No such file or directory

Did I miss something here?

sag47 02-16-2012 11:16 AM

Quote:

Originally Posted by suicidaleggroll (Post 4604157)
Code:

for i in *.old; do mv "$i" "${i/.old/.new}"; done

This method won't work if any of the file names have spaces in them.

@shayno90: Yea I edited my original code. I accidentally mis-formatted it.

Although I now realize that your directory may have other files in it so here's a better way to list it.

Code:

ls -1 *.xsd | while read file;do mv "$file" "${file%.xsd}.oval";done

shayno90 02-16-2012 11:19 AM

Quote:

Originally Posted by suicidaleggroll (Post 4604157)
Code:

for i in *.old; do mv "$i" "${i/.old/.new}"; done

Attempted it without sudo and it seemed to work:

mv: cannot `windows-system-characteristics-schema.xsd' change to `windows-system-characteristics-schema.oval': Permission denied
mv: cannot `xmldsig-core-schema.xsd' change to `xmldsig-core-schema.oval': Permission denied

but after sudoing got this then:

user@user:/usr/local/share/ovaldi/xmltest$ sudo for i in *.xsd; do mv "$i" "${i/.xsd/.oval}"; done
bash: Syntax error beside "do" cannot continue with it.

What is the issue with 'do'?

Cedrik 02-16-2012 11:28 AM

Will change extension for all .xsd files to .oval
Code:

cd /dir
rename .xsd .oval *.xsd


shayno90 02-16-2012 11:31 AM

Quote:

Originally Posted by sag47 (Post 4604160)
This method won't work if any of the file names have spaces in them.

@shayno90: Yea I edited my original code. I accidentally mis-formatted it.

Although I now realize that your directory may have other files in it so here's a better way to list it.

Code:

ls -1 *.xsd | while read file;do mv "$file" "${file%.xsd}.oval";done

Ya your script worked great! There were only 2 files with different extensions
oval-definitions-schematron.xsl (unchanged)
results_to_html.xsl (file name format exception and unchanged)

Every other file was in this format xx-xx-xx.xsl and is now xx-xx-xx.oval.

I only need to move the .oval to another directory hence the need to to change the file extension

shayno90 02-16-2012 11:34 AM

Quote:

Originally Posted by Cedrik (Post 4604175)
Will change extension for all .xsd files to .oval
Code:

cd /dir
rename .xsd .oval *.xsd


Ya I was looking for some command similar to that, thanks for this also.

sag47 02-16-2012 11:35 AM

Quote:

Originally Posted by Cedrik (Post 4604175)
Will change extension for all .xsd files to .oval
Code:

cd /dir
rename .xsd .oval *.xsd


The man page for my rename command says it requires a perl regex.

Code:

rename 's/\.xsd$/.oval/' *.xsd

Cedrik 02-16-2012 11:38 AM

Ah a different rename program then... In my man page for rename command, there is only a note:
Code:

SEE ALSO
      mmv(1), mv(1)
AVAILABILITY
      The rename command is part of the util-linux package and  is  available
      from ftp://ftp.kernel.org/pub/linux/utils/util-linux/.


shayno90 02-16-2012 11:48 AM

Quote:

Originally Posted by Cedrik (Post 4604191)
Ah a different rename program then... In my man page for rename command, there is only a note:
Code:

SEE ALSO
      mmv(1), mv(1)
AVAILABILITY
      The rename command is part of the util-linux package and  is  available
      from ftp://ftp.kernel.org/pub/linux/utils/util-linux/.


It is already installed for:

DESCRIPTION
"rename" renames the filenames supplied according to the rule specified as the first argument. The perlexpr argument is a Perl expression which is expected
to modify the $_ string in Perl for at least some of the filenames specified. If a given filename is not modified by the expression, it will not be
renamed. If no filenames are given on the command line, filenames will be read via standard input.

For example, to rename all files matching "*.bak" to strip the extension, you might say

rename 's/\.bak$//' *.bak

To translate uppercase names to lower, you'd use

rename 'y/A-Z/a-z/' *

Cedrik 02-16-2012 12:02 PM

Whatever works :)

suicidaleggroll 02-17-2012 11:59 AM

Quote:

Originally Posted by sag47 (Post 4604160)
This method won't work if any of the file names have spaces in them.

Yes it will, the quotes cover that. I use it all the time.

Code:

[eggroll@comp test]$ ls
blah 2 3 4.txt  help me.txt  what.txt
[eggroll@comp test]$ for i in *.txt; do mv "$i" "${i/.txt/.new}"; done
[eggroll@comp test]$ ls
blah 2 3 4.new  help me.new  what.new



All times are GMT -5. The time now is 09:43 PM.