I am currently working with formatting file names. Below I have a while loop that runs through file names and replaces white spaces with _
and keeps anything after the hyphen untouched. Now the issue is looping through the results of the find
command in order to format the names. When running this in a script the out put is the following :
find: paths must precede expression: rename
Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]
rename.sh
while read -r file
do
new_file=$(echo "$file" | sed -re 's/^([^-]*)-\s*([^\.]*)/\L\1\E-\2/' -e 's/ /_/g' -e 's/_-/-
/g')
echo "mv '$file' '$new_file'"
done < <(find ./ -type f rename)
Filename before:
'./My File - KeEp.txt'
File name after:
'./my_file-KeEp.txt'
What are you intending
rename
to do infind ./ -type f rename
?find
is, as the error message is telling you, interpreting that as a path (to search) and then telling you that adding paths after arguments is not allowed.That being said, and as @Beta indicates in his comment, using a
while
loop here is unnecessary (and fragile).Try this instead:
or this to actually do it:
To explain that a bit:
'\''
bit is how you escape a single quote inside a single quoted string (you end the string, escape a single quote and start the string again).-
in- {}
is to pad the$0
argument that is the first argument tobash
when run with-c
and other arguments. (Without that the script would need to use$0
instead of$1
to access the filename.)Oh, and your original script used single quotes around variables you wanted to have expanded, that wasn't going to work when you pulled the double quotes from the echo command out.
I think your main bug is the "rename" argument to the find command, I do not understand why you have put it there. There are a few things you could also improve, for example not trying to rename files which would have the same name after it, and also instead of "./" you can simply write ".". Also note that your script does not "keep anything after the hyphen untouched" because it changes the spaces even after that, even in your example filename.
I would still use a while loop, as spawning a bash + sed for each rename can be eliminated this way. I used the -name argument for find to only work on filenames containing a hyphen.
Something like this: