Batch rename of files.

General support questions
Post Reply
supertight
Posts: 171
Joined: 2017/02/07 21:47:51

Batch rename of files.

Post by supertight » 2018/02/20 21:07:24

I've got some files that are all

Code: Select all

Date - long file name with spaces
I'm trying to drop all the info except the date from the file name. I have:

Code: Select all

for file in *.*.2017*; do mv "$file" "${file/\ *./}" done
When I pass it, I get the ">" if I add ";" at the end. I get :

Code: Select all

mv: target ‘done’ is not a directory
I'm still new to this stuff. I need help finding the problem. Thank you.

pjsr2
Posts: 614
Joined: 2014/03/27 20:11:07

Re: Batch rename of files.

Post by pjsr2 » 2018/02/20 21:32:47

Code: Select all

for file in *.*.2017*; do mv "$file" "${file/\ *./}" done
In bash, a command ends on a newline or a semi-colon. As there is no newline or semi-colon before done the word done is interpreted as an argument to the mv command. And since your mv command has three or more arguments, the last argument must be a directory, hence the error message.

Use either:

Code: Select all

for file in *.*.2017*; do mv "$file" "${file/\ *./}" ; done
or

Code: Select all

for file in *.*.2017*; do
     mv "$file" "${file/\ *./}" 
done
Amongst my favorite links on bash programming are: http://www.tldp.org/LDP/abs/html/ and https://mywiki.wooledge.org/BashPitfalls

supertight
Posts: 171
Joined: 2017/02/07 21:47:51

Re: Batch rename of files.

Post by supertight » 2018/02/20 21:45:28

pjsr2 wrote:

Code: Select all

for file in *.*.2017*; do mv "$file" "${file/\ *./}" done
In bash, a command ends on a newline or a semi-colon. As there is no newline or semi-colon before done the word done is interpreted as an argument to the mv command. And since your mv command has three or more arguments, the last argument must be a directory, hence the error message.

Use either:

Code: Select all

for file in *.*.2017*; do mv "$file" "${file/\ *./}" ; done
or

Code: Select all

for file in *.*.2017*; do
     mv "$file" "${file/\ *./}" 
done
Amongst my favorite links on bash programming are: http://www.tldp.org/LDP/abs/html/ and https://mywiki.wooledge.org/BashPitfalls

Thank you, so very much. I'm working on learning both perl and bash. I will read the links supplied.

Post Reply