Single command to rename a file to the first N characters of its hash

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP












0















I'm on macOS Mojave.



I hate having to name images when adding them to my website (Jekyll, for anyone who cares). Instead, I think a reasonable strategy is to generate a hash based on the contents of the file and use the first N characters of the hash as the file name. This strategy also helps with versioning my assets when serving the website out of Amazon S3 and CloudFront.



Example



Given a file somescreenshot.png, I would run this TBD command and the result is the renamed file 8dbm2hz1.png.



Progress



I've made progress on this front but currently it's a multi-step approach.



  1. Generate the hash, take the first eight characters and send to the clipboard.
    shasum -a 256 -b inputfile.png | head -c8 | pbcopy

  2. Rename the file.
    mv inputfile.png [paste from #1]

I need to make this command an efficient piped, single step process.










share|improve this question
























  • If you're happy with one or several of the answers, upvote them. If one is solving your issue, accepting it would be the best way of saying "Thank You!" Accepting an answer also indicates to future readers that the answer actually solved the problem.

    – Kusalananda
    Feb 18 at 20:30















0















I'm on macOS Mojave.



I hate having to name images when adding them to my website (Jekyll, for anyone who cares). Instead, I think a reasonable strategy is to generate a hash based on the contents of the file and use the first N characters of the hash as the file name. This strategy also helps with versioning my assets when serving the website out of Amazon S3 and CloudFront.



Example



Given a file somescreenshot.png, I would run this TBD command and the result is the renamed file 8dbm2hz1.png.



Progress



I've made progress on this front but currently it's a multi-step approach.



  1. Generate the hash, take the first eight characters and send to the clipboard.
    shasum -a 256 -b inputfile.png | head -c8 | pbcopy

  2. Rename the file.
    mv inputfile.png [paste from #1]

I need to make this command an efficient piped, single step process.










share|improve this question
























  • If you're happy with one or several of the answers, upvote them. If one is solving your issue, accepting it would be the best way of saying "Thank You!" Accepting an answer also indicates to future readers that the answer actually solved the problem.

    – Kusalananda
    Feb 18 at 20:30













0












0








0








I'm on macOS Mojave.



I hate having to name images when adding them to my website (Jekyll, for anyone who cares). Instead, I think a reasonable strategy is to generate a hash based on the contents of the file and use the first N characters of the hash as the file name. This strategy also helps with versioning my assets when serving the website out of Amazon S3 and CloudFront.



Example



Given a file somescreenshot.png, I would run this TBD command and the result is the renamed file 8dbm2hz1.png.



Progress



I've made progress on this front but currently it's a multi-step approach.



  1. Generate the hash, take the first eight characters and send to the clipboard.
    shasum -a 256 -b inputfile.png | head -c8 | pbcopy

  2. Rename the file.
    mv inputfile.png [paste from #1]

I need to make this command an efficient piped, single step process.










share|improve this question
















I'm on macOS Mojave.



I hate having to name images when adding them to my website (Jekyll, for anyone who cares). Instead, I think a reasonable strategy is to generate a hash based on the contents of the file and use the first N characters of the hash as the file name. This strategy also helps with versioning my assets when serving the website out of Amazon S3 and CloudFront.



Example



Given a file somescreenshot.png, I would run this TBD command and the result is the renamed file 8dbm2hz1.png.



Progress



I've made progress on this front but currently it's a multi-step approach.



  1. Generate the hash, take the first eight characters and send to the clipboard.
    shasum -a 256 -b inputfile.png | head -c8 | pbcopy

  2. Rename the file.
    mv inputfile.png [paste from #1]

I need to make this command an efficient piped, single step process.







shell-script






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 18 at 20:39









Rui F Ribeiro

41.5k1481140




41.5k1481140










asked Feb 17 at 16:12









Ryan RodemoyerRyan Rodemoyer

1032




1032












  • If you're happy with one or several of the answers, upvote them. If one is solving your issue, accepting it would be the best way of saying "Thank You!" Accepting an answer also indicates to future readers that the answer actually solved the problem.

    – Kusalananda
    Feb 18 at 20:30

















  • If you're happy with one or several of the answers, upvote them. If one is solving your issue, accepting it would be the best way of saying "Thank You!" Accepting an answer also indicates to future readers that the answer actually solved the problem.

    – Kusalananda
    Feb 18 at 20:30
















If you're happy with one or several of the answers, upvote them. If one is solving your issue, accepting it would be the best way of saying "Thank You!" Accepting an answer also indicates to future readers that the answer actually solved the problem.

– Kusalananda
Feb 18 at 20:30





If you're happy with one or several of the answers, upvote them. If one is solving your issue, accepting it would be the best way of saying "Thank You!" Accepting an answer also indicates to future readers that the answer actually solved the problem.

– Kusalananda
Feb 18 at 20:30










3 Answers
3






active

oldest

votes


















4














Why not use:



mv -- inputfile.png "$(shasum -a 256 -b inputfile.png | head -c8)"


It will move your file to the output of $(shasum -a 256 -b inputfile.png | head -c8) i.e. first 8 character of the hash.






share|improve this answer
































    3














    Use command substitution to save the hash in a shell variable:



    file=inputfile.png
    h=$(shasum -a 256 -b "$file" | head -c8)


    Then you can use it for renaming, printing to the terminal, sending to pbcopy, or whatever.



    mv -- "$file" "$h"
    echo "$h" | pbcopy
    echo "'$file' renamed to '$h'"


    If you want to keep the file extension, use something like this:



    newname=$h.$file##*.
    mv -- "$file" "$newname"


    ($var##*. removes everything up to the last dot.)






    share|improve this answer






























      1














      If you want to rename all files, then use xargs. This is an incantation of GNU tools, check your man pages to see if the corresponding options exist.



      printf "%s" * |
      xargs -0 -I sh -c 'echo mv "$1" "$(shasum -a 256 -b "$1"| head -c8)"' sh


      This uses the null byte to separate filenames, which makes working with filenames that contain whitespace a safe operation.



      The echo command is to be removed to actually move the files.






      share|improve this answer
























        Your Answer








        StackExchange.ready(function()
        var channelOptions =
        tags: "".split(" "),
        id: "106"
        ;
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function()
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled)
        StackExchange.using("snippets", function()
        createEditor();
        );

        else
        createEditor();

        );

        function createEditor()
        StackExchange.prepareEditor(
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: false,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: null,
        bindNavPrevention: true,
        postfix: "",
        imageUploader:
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        ,
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        );



        );













        draft saved

        draft discarded


















        StackExchange.ready(
        function ()
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f501191%2fsingle-command-to-rename-a-file-to-the-first-n-characters-of-its-hash%23new-answer', 'question_page');

        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        4














        Why not use:



        mv -- inputfile.png "$(shasum -a 256 -b inputfile.png | head -c8)"


        It will move your file to the output of $(shasum -a 256 -b inputfile.png | head -c8) i.e. first 8 character of the hash.






        share|improve this answer





























          4














          Why not use:



          mv -- inputfile.png "$(shasum -a 256 -b inputfile.png | head -c8)"


          It will move your file to the output of $(shasum -a 256 -b inputfile.png | head -c8) i.e. first 8 character of the hash.






          share|improve this answer



























            4












            4








            4







            Why not use:



            mv -- inputfile.png "$(shasum -a 256 -b inputfile.png | head -c8)"


            It will move your file to the output of $(shasum -a 256 -b inputfile.png | head -c8) i.e. first 8 character of the hash.






            share|improve this answer















            Why not use:



            mv -- inputfile.png "$(shasum -a 256 -b inputfile.png | head -c8)"


            It will move your file to the output of $(shasum -a 256 -b inputfile.png | head -c8) i.e. first 8 character of the hash.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Feb 17 at 16:26

























            answered Feb 17 at 16:17









            Prvt_YadvPrvt_Yadv

            2,66231027




            2,66231027























                3














                Use command substitution to save the hash in a shell variable:



                file=inputfile.png
                h=$(shasum -a 256 -b "$file" | head -c8)


                Then you can use it for renaming, printing to the terminal, sending to pbcopy, or whatever.



                mv -- "$file" "$h"
                echo "$h" | pbcopy
                echo "'$file' renamed to '$h'"


                If you want to keep the file extension, use something like this:



                newname=$h.$file##*.
                mv -- "$file" "$newname"


                ($var##*. removes everything up to the last dot.)






                share|improve this answer



























                  3














                  Use command substitution to save the hash in a shell variable:



                  file=inputfile.png
                  h=$(shasum -a 256 -b "$file" | head -c8)


                  Then you can use it for renaming, printing to the terminal, sending to pbcopy, or whatever.



                  mv -- "$file" "$h"
                  echo "$h" | pbcopy
                  echo "'$file' renamed to '$h'"


                  If you want to keep the file extension, use something like this:



                  newname=$h.$file##*.
                  mv -- "$file" "$newname"


                  ($var##*. removes everything up to the last dot.)






                  share|improve this answer

























                    3












                    3








                    3







                    Use command substitution to save the hash in a shell variable:



                    file=inputfile.png
                    h=$(shasum -a 256 -b "$file" | head -c8)


                    Then you can use it for renaming, printing to the terminal, sending to pbcopy, or whatever.



                    mv -- "$file" "$h"
                    echo "$h" | pbcopy
                    echo "'$file' renamed to '$h'"


                    If you want to keep the file extension, use something like this:



                    newname=$h.$file##*.
                    mv -- "$file" "$newname"


                    ($var##*. removes everything up to the last dot.)






                    share|improve this answer













                    Use command substitution to save the hash in a shell variable:



                    file=inputfile.png
                    h=$(shasum -a 256 -b "$file" | head -c8)


                    Then you can use it for renaming, printing to the terminal, sending to pbcopy, or whatever.



                    mv -- "$file" "$h"
                    echo "$h" | pbcopy
                    echo "'$file' renamed to '$h'"


                    If you want to keep the file extension, use something like this:



                    newname=$h.$file##*.
                    mv -- "$file" "$newname"


                    ($var##*. removes everything up to the last dot.)







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Feb 17 at 16:25









                    ilkkachuilkkachu

                    61.1k1099175




                    61.1k1099175





















                        1














                        If you want to rename all files, then use xargs. This is an incantation of GNU tools, check your man pages to see if the corresponding options exist.



                        printf "%s" * |
                        xargs -0 -I sh -c 'echo mv "$1" "$(shasum -a 256 -b "$1"| head -c8)"' sh


                        This uses the null byte to separate filenames, which makes working with filenames that contain whitespace a safe operation.



                        The echo command is to be removed to actually move the files.






                        share|improve this answer





























                          1














                          If you want to rename all files, then use xargs. This is an incantation of GNU tools, check your man pages to see if the corresponding options exist.



                          printf "%s" * |
                          xargs -0 -I sh -c 'echo mv "$1" "$(shasum -a 256 -b "$1"| head -c8)"' sh


                          This uses the null byte to separate filenames, which makes working with filenames that contain whitespace a safe operation.



                          The echo command is to be removed to actually move the files.






                          share|improve this answer



























                            1












                            1








                            1







                            If you want to rename all files, then use xargs. This is an incantation of GNU tools, check your man pages to see if the corresponding options exist.



                            printf "%s" * |
                            xargs -0 -I sh -c 'echo mv "$1" "$(shasum -a 256 -b "$1"| head -c8)"' sh


                            This uses the null byte to separate filenames, which makes working with filenames that contain whitespace a safe operation.



                            The echo command is to be removed to actually move the files.






                            share|improve this answer















                            If you want to rename all files, then use xargs. This is an incantation of GNU tools, check your man pages to see if the corresponding options exist.



                            printf "%s" * |
                            xargs -0 -I sh -c 'echo mv "$1" "$(shasum -a 256 -b "$1"| head -c8)"' sh


                            This uses the null byte to separate filenames, which makes working with filenames that contain whitespace a safe operation.



                            The echo command is to be removed to actually move the files.







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Feb 17 at 21:27


























                            community wiki





                            2 revs, 2 users 86%
                            glenn jackman




























                                draft saved

                                draft discarded
















































                                Thanks for contributing an answer to Unix & Linux Stack Exchange!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid


                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.

                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function ()
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f501191%2fsingle-command-to-rename-a-file-to-the-first-n-characters-of-its-hash%23new-answer', 'question_page');

                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown






                                Popular posts from this blog

                                Peggy Mitchell

                                Palaiologos

                                The Forum (Inglewood, California)