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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
|
02-16-2012, 09:37 AM
|
#1
|
|
Member
Registered: Oct 2009
Distribution: Debian Lenny 2.6.26 Ubuntu Lucid Lynx 10.04 Windows 7
Posts: 169
Rep:
|
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.
|
|
|
|
02-16-2012, 10:00 AM
|
#2
|
|
Senior Member
Registered: Sep 2009
Location: Philly, PA
Distribution: Kubuntu x64, RHEL, Fedora Core, FreeBSD, Windows x64
Posts: 1,061
|
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.
|
|
|
|
02-16-2012, 10:04 AM
|
#3
|
|
Member
Registered: Oct 2009
Distribution: Debian Lenny 2.6.26 Ubuntu Lucid Lynx 10.04 Windows 7
Posts: 169
Original Poster
Rep:
|
Quote:
Originally Posted by sag47
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.
|
|
|
|
02-16-2012, 10:09 AM
|
#4
|
|
Senior Member
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 1,656
|
Code:
for i in *.old; do mv "$i" "${i/.old/.new}"; done
|
|
|
2 members found this post helpful.
|
02-16-2012, 10:13 AM
|
#5
|
|
Member
Registered: Oct 2009
Distribution: Debian Lenny 2.6.26 Ubuntu Lucid Lynx 10.04 Windows 7
Posts: 169
Original Poster
Rep:
|
Quote:
Originally Posted by sag47
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?
|
|
|
|
02-16-2012, 10:16 AM
|
#6
|
|
Senior Member
Registered: Sep 2009
Location: Philly, PA
Distribution: Kubuntu x64, RHEL, Fedora Core, FreeBSD, Windows x64
Posts: 1,061
|
Quote:
Originally Posted by suicidaleggroll
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.
|
02-16-2012, 10:19 AM
|
#7
|
|
Member
Registered: Oct 2009
Distribution: Debian Lenny 2.6.26 Ubuntu Lucid Lynx 10.04 Windows 7
Posts: 169
Original Poster
Rep:
|
Quote:
Originally Posted by suicidaleggroll
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'?
|
|
|
|
02-16-2012, 10:28 AM
|
#8
|
|
Senior Member
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140
|
Will change extension for all .xsd files to .oval
Code:
cd /dir
rename .xsd .oval *.xsd
|
|
|
1 members found this post helpful.
|
02-16-2012, 10:31 AM
|
#9
|
|
Member
Registered: Oct 2009
Distribution: Debian Lenny 2.6.26 Ubuntu Lucid Lynx 10.04 Windows 7
Posts: 169
Original Poster
Rep:
|
Quote:
Originally Posted by sag47
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.
|
|
|
|
02-16-2012, 10:34 AM
|
#10
|
|
Member
Registered: Oct 2009
Distribution: Debian Lenny 2.6.26 Ubuntu Lucid Lynx 10.04 Windows 7
Posts: 169
Original Poster
Rep:
|
Quote:
Originally Posted by Cedrik
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.
|
|
|
|
02-16-2012, 10:35 AM
|
#11
|
|
Senior Member
Registered: Sep 2009
Location: Philly, PA
Distribution: Kubuntu x64, RHEL, Fedora Core, FreeBSD, Windows x64
Posts: 1,061
|
Quote:
Originally Posted by Cedrik
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
|
|
|
|
02-16-2012, 10:38 AM
|
#12
|
|
Senior Member
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140
|
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/.
|
|
|
|
02-16-2012, 10:48 AM
|
#13
|
|
Member
Registered: Oct 2009
Distribution: Debian Lenny 2.6.26 Ubuntu Lucid Lynx 10.04 Windows 7
Posts: 169
Original Poster
Rep:
|
Quote:
Originally Posted by Cedrik
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/' *
|
|
|
|
02-16-2012, 11:02 AM
|
#14
|
|
Senior Member
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140
|
Whatever works 
|
|
|
|
02-17-2012, 10:59 AM
|
#15
|
|
Senior Member
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 1,656
|
Quote:
Originally Posted by sag47
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.
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 05:21 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|