LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
LinkBack Search this Thread
Old 02-16-2012, 09:37 AM   #1
shayno90
Member
 
Registered: Oct 2009
Distribution: Debian Lenny 2.6.26 Ubuntu Lucid Lynx 10.04 Windows 7
Posts: 177
Blog Entries: 1

Rep: Reputation: 21
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.
 
Old 02-16-2012, 10:00 AM   #2
sag47
Senior Member
 
Registered: Sep 2009
Location: Philly, PA
Distribution: Kubuntu x64, RHEL, Fedora Core, FreeBSD, Windows x64
Posts: 1,328
Blog Entries: 33

Rep: Reputation: 329Reputation: 329Reputation: 329Reputation: 329
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.

Last edited by sag47; 02-16-2012 at 10:11 AM.
 
Old 02-16-2012, 10:04 AM   #3
shayno90
Member
 
Registered: Oct 2009
Distribution: Debian Lenny 2.6.26 Ubuntu Lucid Lynx 10.04 Windows 7
Posts: 177
Blog Entries: 1

Original Poster
Rep: Reputation: 21
Quote:
Originally Posted by sag47 View Post
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.
 
Old 02-16-2012, 10:09 AM   #4
suicidaleggroll
Senior Member
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 2,414

Rep: Reputation: 883Reputation: 883Reputation: 883Reputation: 883Reputation: 883Reputation: 883Reputation: 883
Code:
for i in *.old; do mv "$i" "${i/.old/.new}"; done
 
2 members found this post helpful.
Old 02-16-2012, 10:13 AM   #5
shayno90
Member
 
Registered: Oct 2009
Distribution: Debian Lenny 2.6.26 Ubuntu Lucid Lynx 10.04 Windows 7
Posts: 177
Blog Entries: 1

Original Poster
Rep: Reputation: 21
Quote:
Originally Posted by sag47 View Post
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?
 
Old 02-16-2012, 10:16 AM   #6
sag47
Senior Member
 
Registered: Sep 2009
Location: Philly, PA
Distribution: Kubuntu x64, RHEL, Fedora Core, FreeBSD, Windows x64
Posts: 1,328
Blog Entries: 33

Rep: Reputation: 329Reputation: 329Reputation: 329Reputation: 329
Quote:
Originally Posted by suicidaleggroll View Post
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

Last edited by sag47; 02-16-2012 at 10:18 AM.
 
1 members found this post helpful.
Old 02-16-2012, 10:19 AM   #7
shayno90
Member
 
Registered: Oct 2009
Distribution: Debian Lenny 2.6.26 Ubuntu Lucid Lynx 10.04 Windows 7
Posts: 177
Blog Entries: 1

Original Poster
Rep: Reputation: 21
Quote:
Originally Posted by suicidaleggroll View Post
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'?
 
Old 02-16-2012, 10:28 AM   #8
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 242Reputation: 242Reputation: 242
Will change extension for all .xsd files to .oval
Code:
cd /dir
rename .xsd .oval *.xsd
 
1 members found this post helpful.
Old 02-16-2012, 10:31 AM   #9
shayno90
Member
 
Registered: Oct 2009
Distribution: Debian Lenny 2.6.26 Ubuntu Lucid Lynx 10.04 Windows 7
Posts: 177
Blog Entries: 1

Original Poster
Rep: Reputation: 21
Thumbs up

Quote:
Originally Posted by sag47 View Post
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

Last edited by shayno90; 02-16-2012 at 10:35 AM.
 
Old 02-16-2012, 10:34 AM   #10
shayno90
Member
 
Registered: Oct 2009
Distribution: Debian Lenny 2.6.26 Ubuntu Lucid Lynx 10.04 Windows 7
Posts: 177
Blog Entries: 1

Original Poster
Rep: Reputation: 21
Quote:
Originally Posted by Cedrik View Post
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.
 
Old 02-16-2012, 10:35 AM   #11
sag47
Senior Member
 
Registered: Sep 2009
Location: Philly, PA
Distribution: Kubuntu x64, RHEL, Fedora Core, FreeBSD, Windows x64
Posts: 1,328
Blog Entries: 33

Rep: Reputation: 329Reputation: 329Reputation: 329Reputation: 329
Quote:
Originally Posted by Cedrik View Post
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
 
Old 02-16-2012, 10:38 AM   #12
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 242Reputation: 242Reputation: 242
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/.
 
Old 02-16-2012, 10:48 AM   #13
shayno90
Member
 
Registered: Oct 2009
Distribution: Debian Lenny 2.6.26 Ubuntu Lucid Lynx 10.04 Windows 7
Posts: 177
Blog Entries: 1

Original Poster
Rep: Reputation: 21
Quote:
Originally Posted by Cedrik View Post
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/' *
 
Old 02-16-2012, 11:02 AM   #14
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 242Reputation: 242Reputation: 242
Whatever works
 
Old 02-17-2012, 10:59 AM   #15
suicidaleggroll
Senior Member
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 2,414

Rep: Reputation: 883Reputation: 883Reputation: 883Reputation: 883Reputation: 883Reputation: 883Reputation: 883
Quote:
Originally Posted by sag47 View Post
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

Last edited by suicidaleggroll; 02-17-2012 at 11:04 AM.
 
1 members found this post helpful.
  


Reply

Tags
bash scripting, directory, extension, file, replace


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
convert multiple file in directory: ascii to hex in perl fad216 Programming 3 04-10-2011 12:47 PM
Bash script to copy specified extension file from a directory? sokha Programming 11 03-15-2010 02:51 AM
Linux file extension vs Dos file Extension? manaa Linux - Newbie 6 02-12-2009 04:19 PM
batch file to convert every file in a directory with mencoder. toben Linux - Software 1 01-31-2008 05:28 PM
file to convert from pictClipping extension to jpeg - macutils dumpthecore Linux - Newbie 0 11-12-2003 01:07 AM


All times are GMT -5. The time now is 01:27 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration