• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 800
  • Last Modified:

Script Error: mv: invalid option --1

The script changes the extention on file names. It is as follows:

for i in 'ls *.aaa | cut -f1 -d '; do
   mv $i.aaa $i.bbb;

getting an error when run

mv: invalid option --1
  • 3
  • 2
1 Solution
Your cut command delimiter is wrong.  I think you wanted -d .
Your script will also fail if a filename contains spaces or multiple
filename extensions.

I recently wrote this script for a client that was transitioning from
Linux to Mac OS X (which lacks the Linux 'rename' command):

# This Bash shell script emulates the linux 'rename (1)' command
# The "usage" man page help  was shamelessly stolen from Linux.
# 04 Aug 2004  Brett Johnson  

# Must have at least three args: fromtext, totext, file...
if [ $# -lt 3 ] ; then
  cat <<END
        rename - bulk rename files

        rename fromtext totext files...

        rename will rename the specified files by replacing the
        first occurrence of fromtext in their name with totext.

        For example, given the files foo1, ..., foo9, foo10, ..., foo278,
        the commands

          rename foo foo0 foo?
          rename foo foo0 foo??

        will turn them into foo001, ..., foo009, foo010, ..., foo278.


          rename .htm .html *.htm

        will fix the extension of your html files.


  shift 2

  for f in "$@" ; do mv $f ${f/$fromtext/$totext} ; done
DBDevOneAuthor Commented:
Thanks for teh comment. This is my first script. There are no spaces or multiple extentions. How would I modify my script to eliminate the error?
A better way would be:

for f in *.aaa; do
  mv $f `basename $f .aaa`.bbb
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

DBDevOneAuthor Commented:
What is basename? Can you list and example?
From 'man basename':

basename - strip directory and suffix from filenames

       Print  NAME  with  any leading directory components removed.  If speci-
       fied, also remove a trailing SUFFIX.

My example uses its ability to strip a specified SUFFIX (.aaa) from a file name. By executing the command in a subshell `basename $f .aaa` I get the portion of the filename up to the suffix returned to which I append .bbb. So `basename this-is-the-file.aaa .aaa` would return this-is-the-file and
`basename this-is-the-file.aaa .aaa`.bbb would yield this-is-the-file.bbb.

Make sense?
DBDevOneAuthor Commented:
Thank you for your answers. Brett, I am sure yours was great too, I chose jlevie's because it helped me find the cause of the error I got.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now