SOLVED: Error on executing bash script for exiftool

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
sneakyfox
Level 2
Level 2
Posts: 58
Joined: Sun Jul 19, 2020 7:15 am

SOLVED: Error on executing bash script for exiftool

Post by sneakyfox »

I have the following simple script which I (would like to) use for renaming image files from my camera:

Code: Select all

#!/bin/bash
exiftool -d %Y-%m-%d_%H.%M.%S%%-c.%%e "-filename<CreateDate" -overwrite_original ./
When I execute the script, I get the error: "Failed to execute child process. No such file or directory".

If I just copy the command into a terminal it works fine.

Why doesn't it work when executing the script?

Thanks :)
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 2 times in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
User avatar
JoeFootball
Level 13
Level 13
Posts: 4673
Joined: Tue Nov 24, 2009 1:52 pm
Location: /home/usa/mn/minneapolis/joe

Re: Error on executing bash script for exiftool

Post by JoeFootball »

sneakyfox wrote: Why doesn't it work when executing the script?
I'm not familiar with exiftool syntax, but it appears that within the script its interpreting one of the options as a (nonexistent) directory, which makes me think those double-quotes are the offenders.

Try escaping them via ...

Code: Select all

exiftool -d %Y-%m-%d_%H.%M.%S%%-c.%%e \"-filename<CreateDate\" -overwrite_original ./
This is all just a guess, so please ensure you're trying this on copies of the files and not the originals. :)
sneakyfox
Level 2
Level 2
Posts: 58
Joined: Sun Jul 19, 2020 7:15 am

Re: Error on executing bash script for exiftool

Post by sneakyfox »

Thank you for the suggestion. But I get the same error when adding the backslashes. I also tried with single quotes instead of double, but no change.
1000
Level 6
Level 6
Posts: 1039
Joined: Wed Jul 29, 2020 2:14 am

Re: Error on executing bash script for exiftool

Post by 1000 »

I have the following simple script which I (would like to) use for renaming image files from my camera:
Why you can not use mv

Code: Select all

mv old_file_name     new_file_name
Example

Code: Select all

#!/bin/bash

TIMESTAMP=$(date "+%Y-%m-%d_%H:%M:%S")
echo "$TIMESTAMP"
LIST_FILES=$(ls)

echo -----

while IFS= read -r LINE; do
	echo $LINE
	mv $LINE  ${TIMESTAMP}${LINE}
done <<< "$LIST_FILES"
You can use some protection against activation script.
For example, asking if the path is correct.
I went to the appropriate directory with example files, and I run script with path where script exist.

- You can cut part of name of file and paste new date.
- You can try change, for example with sed. https://stackoverflow.com/questions/340 ... mat-in-sed
sneakyfox
Level 2
Level 2
Posts: 58
Joined: Sun Jul 19, 2020 7:15 am

Re: Error on executing bash script for exiftool

Post by sneakyfox »

I am not sure, but are you aware that exiftool reads the exif tag on the image file and uses that information to rename it? It is not the date of the file itself that is of use, it is the tag that the camera adds to the image after the photo is taken.
1000
Level 6
Level 6
Posts: 1039
Joined: Wed Jul 29, 2020 2:14 am

Re: Error on executing bash script for exiftool

Post by 1000 »

Now I understand, thank you for the explanation.
User avatar
Termy
Level 12
Level 12
Posts: 4248
Joined: Mon Sep 04, 2017 8:49 pm
Location: UK
Contact:

Re: Error on executing bash script for exiftool

Post by Termy »

1000 wrote: Mon Dec 27, 2021 8:08 pm ...
It's greatly discouraged to use ls(1) in a script, typically because it's unreliable and entirely unnecessary, but it's also inefficient. For example, instead of the while read loop iterating over lines from the ls(1) output, you can just:

Code: Select all

for File in *; {
   ...
}
This ensures the filenames are protected and keeps things simple and reliable. Glob filename pattern matching is a very useful thing in shell programming, both in scripts and on the command-line.

I will typically do something like this, when wanting to do a more involved mv(1) operation on the command-line:

Code: Select all

for File in *.txt; { [[ -f $File ]] || continue; mv "$File" "${File%.txt}".sh; }
That would, for example, rename the extension of all text files in the CWD to shell script files.

Another tip: BASH >= 4.2 has date and time functionality built into it (akin to strftime()), so there's no need for the command substitution and running the date(1) program. The following would do the exact same thing, but far more efficiently and practically, using BASH itself:

Code: Select all

printf -v TIMESTAMP '%(%Y-%m-%d_%H:%M:%S)T' -1
Although the OP needn't make it so complex, as you can just use %F_%X within the parentheses.

The -v flag also allows you to perform shell variable assignment, assigning the variable to what would be the output of printf. This is available as of BASH 3.1, but you can also use it to assign to an array index as of BASH 4.1.

The -1 is for the associated field for the date/time format specification (%(...)T), and that value itself means the current time. In actuality, that field is the seconds since Epoch (Epoch is: 1970-01-01 01:00:00, at least in Linux), where -1 is a special value as a placeholder for the current time in seconds since Epoch.

Technically, the use of -1 is implied when no value is given, but to keep the fields sane and to help you remember how it works, it can be good to be explicit.

This allows printf to even format a specific time, based on the format you provide. For example printf '%(%F %X)T\n' $(( EPOCHSECONDS - 3600 )) would output the time 1 hour ago, although that command makes use of a variable available as of BASH 5.0.
I'm also Terminalforlife on GitHub.
t42
Level 11
Level 11
Posts: 3742
Joined: Mon Jan 20, 2014 6:48 pm

Re: Error on executing bash script for exiftool

Post by t42 »

sneakyfox wrote: Mon Dec 27, 2021 11:26 am Why doesn't it work when executing the script?
I tested your script and it works fine almost without modification.
Please try this one:

Code: Select all

#!/bin/bash
exiftool "-filename<CreateDate" -d %Y-%m-%d_%H.%M.%S%%-c.%%e -overwrite_original ./
Of course you need to place the script inside working directory, otherwise you need specify directory each time according with the exiftool syntax.
-=t42=-
MAlfare

Re: Error on executing bash script for exiftool

Post by MAlfare »

sneakyfox wrote: Mon Dec 27, 2021 11:26 am I have the following simple script which I (would like to) use for renaming image files from my camera:

Code: Select all

#!/bin/bash
exiftool -d %Y-%m-%d_%H.%M.%S%%-c.%%e "-filename<CreateDate" -overwrite_original ./
Why doesn't it work when executing the script?
Not an answer to your question, but I would (and did it in the past) use XnViewMP for this task.
I need not, as I import pictures from the camera with DarkTable, which can do this renaming also.
If you have no other use for WnView, I would also try a script.
sneakyfox
Level 2
Level 2
Posts: 58
Joined: Sun Jul 19, 2020 7:15 am

Re: Error on executing bash script for exiftool

Post by sneakyfox »

t42 wrote: Wed Jan 12, 2022 5:15 pm
sneakyfox wrote: Mon Dec 27, 2021 11:26 am Why doesn't it work when executing the script?
I tested your script and it works fine almost without modification.
Please try this one:

Code: Select all

#!/bin/bash
exiftool "-filename<CreateDate" -d %Y-%m-%d_%H.%M.%S%%-c.%%e -overwrite_original ./
Of course you need to place the script inside working directory, otherwise you need specify directory each time according with the exiftool syntax.
I get the same error when trying your version. Thanks for the reply though.

MAlfare wrote: Wed Jan 12, 2022 8:11 pm
sneakyfox wrote: Mon Dec 27, 2021 11:26 am I have the following simple script which I (would like to) use for renaming image files from my camera:

Code: Select all

#!/bin/bash
exiftool -d %Y-%m-%d_%H.%M.%S%%-c.%%e "-filename<CreateDate" -overwrite_original ./
Why doesn't it work when executing the script?
Not an answer to your question, but I would (and did it in the past) use XnViewMP for this task.
I need not, as I import pictures from the camera with DarkTable, which can do this renaming also.
If you have no other use for WnView, I would also try a script.
Thanks for the tip :)
t42
Level 11
Level 11
Posts: 3742
Joined: Mon Jan 20, 2014 6:48 pm

Re: Error on executing bash script for exiftool

Post by t42 »

sneakyfox wrote: Mon Jan 17, 2022 6:13 pm I get the same error when trying your version. Thanks for the reply though.
This script is working fine all the time while placed in the working directory. Check if an output of which bash is the same as the expression after #! in shebang:

Code: Select all

#!/bin/bash
exiftool "-filename<CreateDate" -d %Y-%m-%d_%H.%M.%S%%-c.%%e -overwrite_original ./
-=t42=-
sneakyfox
Level 2
Level 2
Posts: 58
Joined: Sun Jul 19, 2020 7:15 am

Re: Error on executing bash script for exiftool

Post by sneakyfox »

t42 wrote: Mon Jan 17, 2022 7:40 pm
sneakyfox wrote: Mon Jan 17, 2022 6:13 pm I get the same error when trying your version. Thanks for the reply though.
This script is working fine all the time while placed in the working directory. Check if an output of which bash is the same as the expression after #! in shebang:

Code: Select all

#!/bin/bash
exiftool "-filename<CreateDate" -d %Y-%m-%d_%H.%M.%S%%-c.%%e -overwrite_original ./
which bash prints /usr/bin/bash. Is that the problem?

Sorry about the late reply, I'm traveling and rarely on the computer.
t42
Level 11
Level 11
Posts: 3742
Joined: Mon Jan 20, 2014 6:48 pm

Re: Error on executing bash script for exiftool

Post by t42 »

sneakyfox wrote: Mon Jan 31, 2022 3:30 pm which bash prints /usr/bin/bash. Is that the problem?
It's not if you have /bin/bash in your system (as shebang in your script is #!/bin/bash ) - usually it is in both locations.

Code: Select all

file /bin/bash
/bin/bash: ELF 64-bit LSB shared object, x86-64 ...
file /usr/bin/bash
/usr/bin/bash: ELF 64-bit LSB shared object, x86-64 ...
-=t42=-
sneakyfox
Level 2
Level 2
Posts: 58
Joined: Sun Jul 19, 2020 7:15 am

Re: Error on executing bash script for exiftool

Post by sneakyfox »

t42 wrote: Mon Jan 31, 2022 3:52 pm
sneakyfox wrote: Mon Jan 31, 2022 3:30 pm which bash prints /usr/bin/bash. Is that the problem?
It's not if you have /bin/bash in your system (as shebang in your script is #!/bin/bash ) - usually it is in both locations.

Code: Select all

file /bin/bash
/bin/bash: ELF 64-bit LSB shared object, x86-64 ...
file /usr/bin/bash
/usr/bin/bash: ELF 64-bit LSB shared object, x86-64 ...
I get:

Code: Select all

file /bin/bash
/bin/bash: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=a6cb40078351e05121d46daa768e271846d5cc54, for GNU/Linux 3.2.0, stripped
t42
Level 11
Level 11
Posts: 3742
Joined: Mon Jan 20, 2014 6:48 pm

Re: Error on executing bash script for exiftool

Post by t42 »

... so no explanation of this issue ...for now. I check it on several installations and the script was always fine.
-=t42=-
rene
Level 20
Level 20
Posts: 12212
Joined: Sun Mar 27, 2016 6:58 pm

Re: Error on executing bash script for exiftool

Post by rene »

Expect this might again be a matter of poster using some ridculous Windows text editor that inserts a UTF-8 BOM header on the script, screwing up the shebang; can't at the moment find the previous instances of that issue but we've seen it before. I.e., please make sure from e.g.

Code: Select all

xxd -g1 scriptname.sh
that the first bytes are #!/bin/bash, i.e., 23 21 2f 62 69 6e 2f 62 61 73 68 0a and not that but prefixed with ef bb bf or similar.
sneakyfox
Level 2
Level 2
Posts: 58
Joined: Sun Jul 19, 2020 7:15 am

Re: Error on executing bash script for exiftool

Post by sneakyfox »

rene wrote: Tue Feb 01, 2022 9:57 am Expect this might again be a matter of poster using some ridculous Windows text editor that inserts a UTF-8 BOM header on the script, screwing up the shebang; can't at the moment find the previous instances of that issue but we've seen it before. I.e., please make sure from e.g.

Code: Select all

xxd -g1 scriptname.sh
that the first bytes are #!/bin/bash, i.e., 23 21 2f 62 69 6e 2f 62 61 73 68 0a and not that but prefixed with ef bb bf or similar.
The output of xxd:

Code: Select all

xxd -g1 rename_image_files.sh 
00000000: 23 21 2f 62 69 6e 2f 62 61 73 68 0d 0a 65 78 69  #!/bin/bash..exi
00000010: 66 74 6f 6f 6c 20 2d 64 20 25 59 2d 25 6d 2d 25  ftool -d %Y-%m-%
00000020: 64 5f 25 48 2e 25 4d 2e 25 53 25 25 2d 63 2e 25  d_%H.%M.%S%%-c.%
00000030: 25 65 20 22 2d 66 69 6c 65 6e 61 6d 65 3c 43 72  %e "-filename<Cr
00000040: 65 61 74 65 44 61 74 65 22 20 2d 6f 76 65 72 77  eateDate" -overw
00000050: 72 69 74 65 5f 6f 72 69 67 69 6e 61 6c 20 2e 2f  rite_original ./
00000060: 0d 0a 0d 0a                                      ....
t42 wrote: Tue Feb 01, 2022 6:43 am ... so no explanation of this issue ...for now. I check it on several installations and the script was always fine.
Thanks for trying :)
sneakyfox
Level 2
Level 2
Posts: 58
Joined: Sun Jul 19, 2020 7:15 am

Re: Error on executing bash script for exiftool

Post by sneakyfox »

Now it is working. I copied another script file that had been working, and edited it to contain the same text as in the OP. And it works. Seems that it was something with the file?

Thanks rene, for leading me in that direction.
rene
Level 20
Level 20
Posts: 12212
Joined: Sun Mar 27, 2016 6:58 pm

Re: Error on executing bash script for exiftool

Post by rene »

Yes; useful that you posted the full xxd output.

Although not a BOM header it's a DOS-style text file, i.e., with \r\n line-endings rather than UNIX-style \n. If I recreate your literal script here with

Code: Select all

rene@p55m:~$ xxd -r >rename_image_files.sh <<EOF
> 00000000: 23 21 2f 62 69 6e 2f 62 61 73 68 0d 0a 65 78 69  #!/bin/bash..exi
> 00000010: 66 74 6f 6f 6c 20 2d 64 20 25 59 2d 25 6d 2d 25  ftool -d %Y-%m-%
> 00000020: 64 5f 25 48 2e 25 4d 2e 25 53 25 25 2d 63 2e 25  d_%H.%M.%S%%-c.%
> 00000030: 25 65 20 22 2d 66 69 6c 65 6e 61 6d 65 3c 43 72  %e "-filename<Cr
> 00000040: 65 61 74 65 44 61 74 65 22 20 2d 6f 76 65 72 77  eateDate" -overw
> 00000050: 72 69 74 65 5f 6f 72 69 67 69 6e 61 6c 20 2e 2f  rite_original ./
> 00000060: 0d 0a 0d 0a                                      ....
> EOF
I actually get

Code: Select all

rene@p55m:~$ chmod +x rename_image_files.sh 
rene@p55m:~$ ./rename_image_files.sh 
-bash: ./rename_image_files.sh: /bin/bash^M: bad interpreter: No such file or directory
rather than your ""Failed to execute child process" so not sure what's up there (are you using something other than bash?) but it's in any case no good. You can / could have fixed the script also with

Code: Select all

fromdos rename_image_files.sh
t42
Level 11
Level 11
Posts: 3742
Joined: Mon Jan 20, 2014 6:48 pm

Re: Error on executing bash script for exiftool

Post by t42 »

rene wrote: Tue Feb 01, 2022 9:57 amtext editor
Good guess for my piece of mind, rene, thanks )
I stop itself from asking OP "did you put my text in xed with copy-paste?" as it was well above my accepted level of policing :
sneakyfox wrote: Mon Jan 17, 2022 6:13 pm
t42 wrote: Wed Jan 12, 2022 5:15 pm ...
Please try this one:

Code: Select all

#!/bin/bash
exiftool "-filename<CreateDate" -d %Y-%m-%d_%H.%M.%S%%-c.%%e -overwrite_original ./
Of course you need to place the script inside working directory, otherwise you need specify directory each time according with the exiftool syntax.
I get the same error when trying your version.
-=t42=-
Locked

Return to “Scripts & Bash”