0

From this website and on the internet I have looked for this answer, but could not find it specific. My knowledge of Linux is not so big, but here is my problem: I would like to convert a JPG file with a 8 characters file name. So from 2014-12-12 23.59.59.jpg to 484140b7.jpg (heximal). I came up or found this code:

ddate=$(exiv2 "${i}"|grep timestamp|cut -c 24-37|tr -d " :")
cp "$i" "${ddate}.jpg"

I saw here and there you can use something like printf "%x\n", but I don't manage to get it to work.

Can somebody help me with this?

Thank you already very much!

lurker
  • 56,987
  • 9
  • 69
  • 103
Helfenstein
  • 315
  • 1
  • 4
  • 13

1 Answers1

1

Maybe something like this:

for x in *.jpg; do
    f=$(sed "s/[^0-9]//g" <<< "$x")
    cp "$x" "$(printf "%x.jpg" "$f")"
done

If you want to remove the year from the front of the date first, you could use a cut as you attempted:

for x in *.jpg; do
    f=$(sed -e "s/[^0-9]//g" -e "s/^....//" <<< "$x")
    cp "$x" "$(printf "%x.jpg" "$f")"
done
lurker
  • 56,987
  • 9
  • 69
  • 103
  • Thank you for the editing. It looks way better. I am a bit confused here. Firstly it should be mv instead of cp ofcourse. So with my code I already get 1212235959.jpg. This should become in hex 484140b7.jpg . I didn't have the time to test your code, but I have the feeling you try to get something else out of the name of the file, than I would like to. I like your idea to get the number out like that, but than you can also use just something like exiv2 "%Y%m%d%h%m%s". But I am not so far in Linux, so please correct me if I am wrong. – Helfenstein Jan 08 '14 at 09:09
  • @Helfenstein I am not familiar with `exiv2`. I also see now you want to drop the year off of the date first before converting to hex? What exactly is the goal? Do you need exactly 8 characters in basename of the file name, or up to 8, or something else? Thanks. :) – lurker Jan 08 '14 at 11:44
  • 1
    @mbratch you can remove the year within the same sed: `sed -e 's/[^0-9]//g' -e 's/....//'` – janos Jan 08 '14 at 11:53
  • Good question mbratch. I want to get the information when the picture is taken and not of the file when it is made. Is this also done with sed? I am not familiar with that command. I would like to use files from a camera and put it on an ipad. The camera makes it the long name with date and time and it seems that an ipad can't take a longer file name than 8 character (when you use an adapter to transfer it from and sd-card). So I try it with a raspberry. So the filename can be up to 8 characters, but I was thinking with this way I get the most information out of the file and still sort it. – Helfenstein Jan 08 '14 at 13:46
  • @Helfenstein if you have a command that outputs the date in a known, fixed text format, then `sed` will work. If the format of the input date is different than what you showed (`YYYY-MM-DD HH:MM:SS`) then the regex string *may* need to be adjusted. But right now it's general enough it may work regardless. – lurker Jan 08 '14 at 13:49
  • Found it! ddate=$(exiv2 "${i}"|grep timestamp|cut -c 24-37|tr -d " :")|awk '{printf "%x", $1}') rm "$i" "${ddate}.jpg" – Helfenstein Jan 08 '14 at 21:47