LinuxQuestions.org

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

suicidaleggroll 02-17-2012 11:07 AM

Quote:

Originally Posted by shayno90 (Post 4604164)
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'?

do is the opening of the for loop. What I wrote is actually a 4 line script rolled into one line using semicolons, ie:
Code:

for i in *.xsd
do
  mv "$i" "${i/.xsd/.oval}"
done

Chances are the sudo is what's messing things up, since it's "sudoing" the for, and not the rest. I never use sudo, so I can't really test it, but this might be the change you need:

Code:

for i in *.xsd; do sudo mv "$i" "${i/.xsd/.oval}"; done

shayno90 03-01-2012 06:35 PM

Code:

for i in *.xsd; do sudo mv "$i" "${i/.xsd/.oval}"; done
Thanks for the input on this suicidaleggroll!

sag47 03-02-2012 08:39 AM

Quote:

Originally Posted by suicidaleggroll (Post 4605158)
Yes it will, the quotes cover that. I use it all the time.

No it won't. Do an example yourself and see.

Code:

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

You will get "old not found" and "file.old not found" errors when you run that for loop. Unless you've changed your field separator in the IFS variable to see newlines as the field separator that for loop does not work on file names with spaces. The for loop separates arguments based on spaces. That's why I gave you the while read line loop.

suicidaleggroll 03-02-2012 09:42 AM

Quote:

Originally Posted by sag47 (Post 4616838)
No it won't. Do an example yourself and see.

Code:

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

You will get "old not found" and "file.old not found" errors when you run that for loop. Unless you've changed your field separator in the IFS variable to see newlines as the field separator that for loop does not work on file names with spaces. The for loop separates arguments based on spaces. That's why I gave you the while read line loop.

RHEL:
Code:

[eggroll@picard test]$ touch old\ file.old
[eggroll@picard test]$ touch old-file.old
[eggroll@picard test]$ for i in *.old; do mv "$i" "${i/.old/.new}"; done
[eggroll@picard test]$ ls
old file.new  old-file.new

I have also tested it successfully on CentOS, Fedora, Mint, OpenSUSE, Cygwin, and BusyBox. None of them have modified IFS. I just set up the Mint box not 24 hours ago, haven't modified anything on it really, and it works fine. If I had access to more distros, I would try them as well, but so far I haven't found a single distro that it doesn't work on, including the limited ones used on embedded systems like the GESBC-9260S and Gumstix.

You would be right if I had written it like:
Code:

for i in $(ls *.old); do mv "$i" "${i/.old/.new}"; done
but that's why I didn't write it like that. "for i in *.old" does not work the same way as "for i in $(ls *.old)", and does not suffer from the problem you're describing, at least not on any system I've ever used.

sag47 03-02-2012 01:14 PM

Quote:

Originally Posted by suicidaleggroll (Post 4616892)
You would be right if I had written it like:
Code:

for i in $(ls *.old); do mv "$i" "${i/.old/.new}"; done
but that's why I didn't write it like that. "for i in *.old" does not work the same way as "for i in $(ls *.old)", and does not suffer from the problem you're describing, at least not on any system I've ever used.

Ah I had it confused with the ls command as I've tried that in the past. My bad.


All times are GMT -5. The time now is 07:50 AM.