Batch changing file extensions

We are moving from an OpenVMS server to a Unix server and I have a problem with ftp'ing files.

When I ftp the VMS server from the Unix server, I need to "mget" some files, for example "mget test_file*.txt;". The semicolon is necessary because OpenVMS has multiple versions of the file (eg test_file.txt;1, test_file.txt;2, etc).

When I do this i end up with the file "test_file.txt;2". I then need to convert this to remove the semicolon.

I have tried various methods and have found one that works :

for file in *.txt*; do
noext="${file%.}"
mv "$file" "${noext#
.}.txt"
done

However, I have problems with this :

  1. it renames ALL .txt files even if they have no ";" and version number.
  2. it will only do one file extension at a time.
  3. it is inefficient

What I want is to say "for all files with a semicolon in the file extension, rename the file to everything to the left of the semicolon".

Better yet, "give me all files matching *.txt; from theVMS server, and create them on the Unix server without the ;"

Any suggestions would be appreciated.

Try this:

ls *.*\;* | awk -F';' '{ printf("mv \"%s\" \"%s\"\n", $0, $1) }' > rename.sh

Check if the output is what you want and then:

sh rename.sh

However, the code must be changed a bit if you have more than one semicolon in file names.

The foolowing code should do the job.

for file in `ls ./*\;*`
do
   mv "$file" "${file%;*}"
done

danmero: *bling*, Useless Use of ls in Backticks.

for file in *\;*
do
  mv "$file" "${file%;*}"
done

cdines: The best-case scenario, as you note, would be to find a ftp client which understands the VMS file naming convention and can convert it to Unix, but I have never heard of one which would do that. Ask in VMS circles, though; they would know.

era: you got me, my bad:)

great, thanks a lot guys !