Code: Select all
#!/bin/bash
exiftool -d %Y-%m-%d_%H.%M.%S%%-c.%%e "-filename<CreateDate" -overwrite_original ./
If I just copy the command into a terminal it works fine.
Why doesn't it work when executing the script?
Thanks
Code: Select all
#!/bin/bash
exiftool -d %Y-%m-%d_%H.%M.%S%%-c.%%e "-filename<CreateDate" -overwrite_original ./
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.sneakyfox wrote: Why doesn't it work when executing the script?
Code: Select all
exiftool -d %Y-%m-%d_%H.%M.%S%%-c.%%e \"-filename<CreateDate\" -overwrite_original ./
Why you can not use mvI have the following simple script which I (would like to) use for renaming image files from my camera:
Code: Select all
mv old_file_name new_file_name
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"
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 *; {
...
}
Code: Select all
for File in *.txt; { [[ -f $File ]] || continue; mv "$File" "${File%.txt}".sh; }
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
%F_%X
within the parentheses.-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.-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.-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.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 tested your script and it works fine almost without modification.
Code: Select all
#!/bin/bash
exiftool "-filename<CreateDate" -d %Y-%m-%d_%H.%M.%S%%-c.%%e -overwrite_original ./
Not an answer to your question, but I would (and did it in the past) use XnViewMP for this task.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:Why doesn't it work when executing the script?Code: Select all
#!/bin/bash exiftool -d %Y-%m-%d_%H.%M.%S%%-c.%%e "-filename<CreateDate" -overwrite_original ./
I get the same error when trying your version. Thanks for the reply though.t42 wrote: ⤴Wed Jan 12, 2022 5:15 pmI tested your script and it works fine almost without modification.
Please try this one:Of course you need to place the script inside working directory, otherwise you need specify directory each time according with the exiftool syntax.Code: Select all
#!/bin/bash exiftool "-filename<CreateDate" -d %Y-%m-%d_%H.%M.%S%%-c.%%e -overwrite_original ./
Thanks for the tipMAlfare wrote: ⤴Wed Jan 12, 2022 8:11 pmNot an answer to your question, but I would (and did it in the past) use XnViewMP for this task.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:Why doesn't it work when executing the script?Code: Select all
#!/bin/bash exiftool -d %Y-%m-%d_%H.%M.%S%%-c.%%e "-filename<CreateDate" -overwrite_original ./
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.
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 wrote: ⤴Mon Jan 17, 2022 7:40 pmThis script is working fine all the time while placed in the working directory. Check if an output ofwhich 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?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:t42 wrote: ⤴Mon Jan 31, 2022 3:52 pmIt'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 ...
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
Code: Select all
xxd -g1 scriptname.sh
#!/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: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.
that the first bytes areCode: Select all
xxd -g1 scriptname.sh
#!/bin/bash
, i.e.,23 21 2f 62 69 6e 2f 62 61 73 68 0a
and not that but prefixed withef bb bf
or similar.
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 ....
Thanks for trying
xxd
output.\r\n
line-endings rather than UNIX-style \n
. If I recreate your literal script here withCode: 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
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
Code: Select all
fromdos rename_image_files.sh
Good guess for my piece of mind, rene, thanks )
sneakyfox wrote: ⤴Mon Jan 17, 2022 6:13 pmI get the same error when trying your version.t42 wrote: ⤴Wed Jan 12, 2022 5:15 pm ...
Please try this one:Of course you need to place the script inside working directory, otherwise you need specify directory each time according with the exiftool syntax.Code: Select all
#!/bin/bash exiftool "-filename<CreateDate" -d %Y-%m-%d_%H.%M.%S%%-c.%%e -overwrite_original ./