2

I'm getting this error:

"find: paths must precede expression

Usage: find [-H] [-L] [-P] [path...] [expression]"

For this code I produced:

for subdir in `find ./$file/ -name "*.$@"`
do
new_ext=`echo $subdir | sed "s/\(.*\.\)$/\1$new/"`
mv $subfile $new_ext
done

What i'm trying to do with the code above is rename files extensions in current and sub-directories without having to enter the old file extension.

Any help pointing out what i'm doing wrong would be grateful.

1

2 Answers 2

2

Here is my attempt:

for old_extension in "$@"
do  
    find ./$file -name "*.$old_extension" | while read old_file
    do
        new_file=${old_file%$old_extension}new
        echo mv "$old_file" "$new_file"
    done
done
  • The output of the find command is read by the while loop, one line at a time, each line is assigned to the variable $old_file
  • The ${old_file%$old_extension} construct removes the extension, then the new extension 'new' is appended to $new_file
  • The echo mv ... line will output to the screen the for visual verification
  • Once you are happy with the result, you can remove the echo and run the script again to really do the damage. Remember: please check the result before removing the echo.
  • Note that I have tested my solution against files with and without embedded spaces.
2
  • 1
    Yes, the original code is terrible for various reasons. But you've completely missed the point that the find command doesn't work. May 22, 2011 at 2:51
  • @Ignacio: I have edited the code to take in account of multiple extensions. The find command should work now, provide that Mr. Teeth set the value for the file variable.
    – Hai Vu
    May 22, 2011 at 2:55
1

"*.$@" surely doesn't do what you want. Build the command in an array.

2
  • I was thinking should I just name it as "$@"? I read what you linked me. Thanks.
    – Mr Teeth
    May 21, 2011 at 16:02
  • No. You still need multiple -name predicates (and -o, if you want it to work properly). May 21, 2011 at 16:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.