Mr Teeth
Mr Teeth

Reputation: 1299

find: paths must precede expression

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.

Upvotes: 2

Views: 3490

Answers (2)

Hai Vu
Hai Vu

Reputation: 40688

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.

Upvotes: 2

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798526

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

Upvotes: 1

Related Questions