change many file names to hash (sha256)?












0















I have a bunch of video files where all the file names are un-useful gibberish.



1) Is there an easy command to change the names of all the files in a folder to a hash, like (sha256).mp4? I don't want to move / copy / modify any files, only rename them.



2) Can the command from 1) be modified to only add the hash onto the end of the original file name? Like (original name)_(sha256).mp4



I'm quite new to Ubuntu/Linux and would greatly appreciate an explanation for what each part of the command does.










share|improve this question









New contributor




Yamashita is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





















  • Any command you can come up with to do it ONCE you can put a loop around to have it repeat it on a next file so the answer is yes. There is a command "rename" and a command "rename.ul" in linux to rename files. It does accept regexes and the normal variables to change a filename

    – Rinzwind
    10 hours ago
















0















I have a bunch of video files where all the file names are un-useful gibberish.



1) Is there an easy command to change the names of all the files in a folder to a hash, like (sha256).mp4? I don't want to move / copy / modify any files, only rename them.



2) Can the command from 1) be modified to only add the hash onto the end of the original file name? Like (original name)_(sha256).mp4



I'm quite new to Ubuntu/Linux and would greatly appreciate an explanation for what each part of the command does.










share|improve this question









New contributor




Yamashita is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





















  • Any command you can come up with to do it ONCE you can put a loop around to have it repeat it on a next file so the answer is yes. There is a command "rename" and a command "rename.ul" in linux to rename files. It does accept regexes and the normal variables to change a filename

    – Rinzwind
    10 hours ago














0












0








0








I have a bunch of video files where all the file names are un-useful gibberish.



1) Is there an easy command to change the names of all the files in a folder to a hash, like (sha256).mp4? I don't want to move / copy / modify any files, only rename them.



2) Can the command from 1) be modified to only add the hash onto the end of the original file name? Like (original name)_(sha256).mp4



I'm quite new to Ubuntu/Linux and would greatly appreciate an explanation for what each part of the command does.










share|improve this question









New contributor




Yamashita is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












I have a bunch of video files where all the file names are un-useful gibberish.



1) Is there an easy command to change the names of all the files in a folder to a hash, like (sha256).mp4? I don't want to move / copy / modify any files, only rename them.



2) Can the command from 1) be modified to only add the hash onto the end of the original file name? Like (original name)_(sha256).mp4



I'm quite new to Ubuntu/Linux and would greatly appreciate an explanation for what each part of the command does.







command-line rename batch-rename hash sha256






share|improve this question









New contributor




Yamashita is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Yamashita is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 9 hours ago









dessert

24.1k670104




24.1k670104






New contributor




Yamashita is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 10 hours ago









YamashitaYamashita

11




11




New contributor




Yamashita is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Yamashita is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Yamashita is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.













  • Any command you can come up with to do it ONCE you can put a loop around to have it repeat it on a next file so the answer is yes. There is a command "rename" and a command "rename.ul" in linux to rename files. It does accept regexes and the normal variables to change a filename

    – Rinzwind
    10 hours ago



















  • Any command you can come up with to do it ONCE you can put a loop around to have it repeat it on a next file so the answer is yes. There is a command "rename" and a command "rename.ul" in linux to rename files. It does accept regexes and the normal variables to change a filename

    – Rinzwind
    10 hours ago

















Any command you can come up with to do it ONCE you can put a loop around to have it repeat it on a next file so the answer is yes. There is a command "rename" and a command "rename.ul" in linux to rename files. It does accept regexes and the normal variables to change a filename

– Rinzwind
10 hours ago





Any command you can come up with to do it ONCE you can put a loop around to have it repeat it on a next file so the answer is yes. There is a command "rename" and a command "rename.ul" in linux to rename files. It does accept regexes and the normal variables to change a filename

– Rinzwind
10 hours ago










2 Answers
2






active

oldest

votes


















2














You can actually do this with this rename command:



rename 'chomp(my $new=`sha256sum "$_" | cut -d" " -f1`); s/.*/$new.mp4/' *mp4


To preview what it will do before actually renaming, use -n. For example:



$ ls
'my movie file01.mp4' 'my movie file05.mp4' 'my movie file09.mp4'
'my movie file02.mp4' 'my movie file06.mp4' 'my movie file10.mp4'
'my movie file03.mp4' 'my movie file07.mp4'
'my movie file04.mp4' 'my movie file08.mp4'

$ rename -n 'chomp(my $new=`sha256sum "$_" | cut -d" " -f1`); s/.*/$new.mp4/' *mp4
my movie file01.mp4 -> 6a260f4adeb47000afeb9c53029ce0e14b6ea80be5c649ecdb155c8e7cfa1496.mp4
my movie file02.mp4 -> 14f24228ead10d639ef3db11ea09c6216aa80879327a2a57fb549e6474fb05b0.mp4
my movie file03.mp4 -> 6c9ecec6a75e217fa1786dd6f920a5c1db8473f17447595e0d33d7fc3530453a.mp4
my movie file04.mp4 -> 96a29ae5bc3a5e35537bcea1068c746ddba205d17fcc8e522a52402c7ed69927.mp4
my movie file05.mp4 -> 189207823129b57e52c86a743ea7154f65b8eccb5182f322abc3aae35f14057d.mp4
my movie file06.mp4 -> 93be0fd15be56bb22af94a0a0aa272d56f61e77160f1170305d41166d6135913.mp4
my movie file07.mp4 -> 05c92b5632414f14445af3facdb0e187746b3b0f40bad94a5f351aeb7f9a8847.mp4
my movie file08.mp4 -> 0d3cf9c165d4c18af8922b8daa9a6b1031c6762d179fb910b968d60af9aed255.mp4
my movie file09.mp4 -> da04afebaec6ea3d43dc3a0e66b790ab13751b00fd81aa1f882986c097c36298.mp4
my movie file10.mp4 -> 47bf532970d3e759346462a85d15ab9d9171c5a7688a6e90a1e976e071ecd167.mp4


And, to keep the original name:



$ rename -n 'chomp(my $new=`sha256sum "$_" | cut -d" " -f1`); s/.*/$_ $new.mp4/' *mp4
my movie file01.mp4 -> my movie file01.mp4 6a260f4adeb47000afeb9c53029ce0e14b6ea80be5c649ecdb155c8e7cfa1496.mp4
my movie file02.mp4 -> my movie file02.mp4 14f24228ead10d639ef3db11ea09c6216aa80879327a2a57fb549e6474fb05b0.mp4
my movie file03.mp4 -> my movie file03.mp4 6c9ecec6a75e217fa1786dd6f920a5c1db8473f17447595e0d33d7fc3530453a.mp4
my movie file04.mp4 -> my movie file04.mp4 96a29ae5bc3a5e35537bcea1068c746ddba205d17fcc8e522a52402c7ed69927.mp4
my movie file05.mp4 -> my movie file05.mp4 189207823129b57e52c86a743ea7154f65b8eccb5182f322abc3aae35f14057d.mp4
my movie file06.mp4 -> my movie file06.mp4 93be0fd15be56bb22af94a0a0aa272d56f61e77160f1170305d41166d6135913.mp4
my movie file07.mp4 -> my movie file07.mp4 05c92b5632414f14445af3facdb0e187746b3b0f40bad94a5f351aeb7f9a8847.mp4
my movie file08.mp4 -> my movie file08.mp4 0d3cf9c165d4c18af8922b8daa9a6b1031c6762d179fb910b968d60af9aed255.mp4
my movie file09.mp4 -> my movie file09.mp4 da04afebaec6ea3d43dc3a0e66b790ab13751b00fd81aa1f882986c097c36298.mp4
my movie file10.mp4 -> my movie file10.mp4 47bf532970d3e759346462a85d15ab9d9171c5a7688a6e90a1e976e071ecd167.mp4


The explanation here is a bit complicated as it requires understanding some basic Perl. The main idea is:





  • my $new=`sha256sum "$_" | cut -d" " -f1` : this will run the command sha256sum on each input file ($_ is Perl magic for "current thing"), and cut to keep only the hash. To illustrate:



    $ sha256sum my movie file01.mp4 
    6a260f4adeb47000afeb9c53029ce0e14b6ea80be5c649ecdb155c8e7cfa1496 my movie file01.mp4
    $ sha256sum my movie file01.mp4 | cut -d" " -f1
    6a260f4adeb47000afeb9c53029ce0e14b6ea80be5c649ecdb155c8e7cfa1496


    The chomp() just removes the trailing newline from the command's output. The result is stored in the variable $new.




  • s/.*/$new.mp4/' : substitute everything (.*) with the $new name.


Or, for the other command:





  • s/.*/$_ $new.mp4/' : substitute everything with the original name ($_) followed by the new one.






share|improve this answer


























  • does not work. rename -n 'chomp($new=sha256sum "$_" | cut -d" " -f1); s/.*/$_ $new.mp4/' *mp4 Global symbol "$new" requires explicit package name (did you forget to declare "my $new"?) at (user-supplied code).

    – VeeJay
    7 hours ago











  • @VeeJay wow, Ubuntu seems to have changed rename so that it runs with use strict! How strange. Thanks for the heads up, see updated answer.

    – terdon
    7 hours ago



















-1














This needs a simple shell loop:



for i in *.mp4 ; do
j="$(openssl dgst -sha256 "$i" | cut '-d ' -f2)"
mv "$i" "$j.mp4"
done


The 2nd part of your question needs a little more work:



for i in *.mp4 ; do
j="$(openssl dgst -sha256 "$i" | cut '-d ' -f2)"
k="${i%%.mp4}"
mv "$i" "$k_$j.mp4"
done





share|improve this answer

























    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "89"
    };
    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: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    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
    });


    }
    });






    Yamashita is a new contributor. Be nice, and check out our Code of Conduct.










    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1122247%2fchange-many-file-names-to-hash-sha256%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    You can actually do this with this rename command:



    rename 'chomp(my $new=`sha256sum "$_" | cut -d" " -f1`); s/.*/$new.mp4/' *mp4


    To preview what it will do before actually renaming, use -n. For example:



    $ ls
    'my movie file01.mp4' 'my movie file05.mp4' 'my movie file09.mp4'
    'my movie file02.mp4' 'my movie file06.mp4' 'my movie file10.mp4'
    'my movie file03.mp4' 'my movie file07.mp4'
    'my movie file04.mp4' 'my movie file08.mp4'

    $ rename -n 'chomp(my $new=`sha256sum "$_" | cut -d" " -f1`); s/.*/$new.mp4/' *mp4
    my movie file01.mp4 -> 6a260f4adeb47000afeb9c53029ce0e14b6ea80be5c649ecdb155c8e7cfa1496.mp4
    my movie file02.mp4 -> 14f24228ead10d639ef3db11ea09c6216aa80879327a2a57fb549e6474fb05b0.mp4
    my movie file03.mp4 -> 6c9ecec6a75e217fa1786dd6f920a5c1db8473f17447595e0d33d7fc3530453a.mp4
    my movie file04.mp4 -> 96a29ae5bc3a5e35537bcea1068c746ddba205d17fcc8e522a52402c7ed69927.mp4
    my movie file05.mp4 -> 189207823129b57e52c86a743ea7154f65b8eccb5182f322abc3aae35f14057d.mp4
    my movie file06.mp4 -> 93be0fd15be56bb22af94a0a0aa272d56f61e77160f1170305d41166d6135913.mp4
    my movie file07.mp4 -> 05c92b5632414f14445af3facdb0e187746b3b0f40bad94a5f351aeb7f9a8847.mp4
    my movie file08.mp4 -> 0d3cf9c165d4c18af8922b8daa9a6b1031c6762d179fb910b968d60af9aed255.mp4
    my movie file09.mp4 -> da04afebaec6ea3d43dc3a0e66b790ab13751b00fd81aa1f882986c097c36298.mp4
    my movie file10.mp4 -> 47bf532970d3e759346462a85d15ab9d9171c5a7688a6e90a1e976e071ecd167.mp4


    And, to keep the original name:



    $ rename -n 'chomp(my $new=`sha256sum "$_" | cut -d" " -f1`); s/.*/$_ $new.mp4/' *mp4
    my movie file01.mp4 -> my movie file01.mp4 6a260f4adeb47000afeb9c53029ce0e14b6ea80be5c649ecdb155c8e7cfa1496.mp4
    my movie file02.mp4 -> my movie file02.mp4 14f24228ead10d639ef3db11ea09c6216aa80879327a2a57fb549e6474fb05b0.mp4
    my movie file03.mp4 -> my movie file03.mp4 6c9ecec6a75e217fa1786dd6f920a5c1db8473f17447595e0d33d7fc3530453a.mp4
    my movie file04.mp4 -> my movie file04.mp4 96a29ae5bc3a5e35537bcea1068c746ddba205d17fcc8e522a52402c7ed69927.mp4
    my movie file05.mp4 -> my movie file05.mp4 189207823129b57e52c86a743ea7154f65b8eccb5182f322abc3aae35f14057d.mp4
    my movie file06.mp4 -> my movie file06.mp4 93be0fd15be56bb22af94a0a0aa272d56f61e77160f1170305d41166d6135913.mp4
    my movie file07.mp4 -> my movie file07.mp4 05c92b5632414f14445af3facdb0e187746b3b0f40bad94a5f351aeb7f9a8847.mp4
    my movie file08.mp4 -> my movie file08.mp4 0d3cf9c165d4c18af8922b8daa9a6b1031c6762d179fb910b968d60af9aed255.mp4
    my movie file09.mp4 -> my movie file09.mp4 da04afebaec6ea3d43dc3a0e66b790ab13751b00fd81aa1f882986c097c36298.mp4
    my movie file10.mp4 -> my movie file10.mp4 47bf532970d3e759346462a85d15ab9d9171c5a7688a6e90a1e976e071ecd167.mp4


    The explanation here is a bit complicated as it requires understanding some basic Perl. The main idea is:





    • my $new=`sha256sum "$_" | cut -d" " -f1` : this will run the command sha256sum on each input file ($_ is Perl magic for "current thing"), and cut to keep only the hash. To illustrate:



      $ sha256sum my movie file01.mp4 
      6a260f4adeb47000afeb9c53029ce0e14b6ea80be5c649ecdb155c8e7cfa1496 my movie file01.mp4
      $ sha256sum my movie file01.mp4 | cut -d" " -f1
      6a260f4adeb47000afeb9c53029ce0e14b6ea80be5c649ecdb155c8e7cfa1496


      The chomp() just removes the trailing newline from the command's output. The result is stored in the variable $new.




    • s/.*/$new.mp4/' : substitute everything (.*) with the $new name.


    Or, for the other command:





    • s/.*/$_ $new.mp4/' : substitute everything with the original name ($_) followed by the new one.






    share|improve this answer


























    • does not work. rename -n 'chomp($new=sha256sum "$_" | cut -d" " -f1); s/.*/$_ $new.mp4/' *mp4 Global symbol "$new" requires explicit package name (did you forget to declare "my $new"?) at (user-supplied code).

      – VeeJay
      7 hours ago











    • @VeeJay wow, Ubuntu seems to have changed rename so that it runs with use strict! How strange. Thanks for the heads up, see updated answer.

      – terdon
      7 hours ago
















    2














    You can actually do this with this rename command:



    rename 'chomp(my $new=`sha256sum "$_" | cut -d" " -f1`); s/.*/$new.mp4/' *mp4


    To preview what it will do before actually renaming, use -n. For example:



    $ ls
    'my movie file01.mp4' 'my movie file05.mp4' 'my movie file09.mp4'
    'my movie file02.mp4' 'my movie file06.mp4' 'my movie file10.mp4'
    'my movie file03.mp4' 'my movie file07.mp4'
    'my movie file04.mp4' 'my movie file08.mp4'

    $ rename -n 'chomp(my $new=`sha256sum "$_" | cut -d" " -f1`); s/.*/$new.mp4/' *mp4
    my movie file01.mp4 -> 6a260f4adeb47000afeb9c53029ce0e14b6ea80be5c649ecdb155c8e7cfa1496.mp4
    my movie file02.mp4 -> 14f24228ead10d639ef3db11ea09c6216aa80879327a2a57fb549e6474fb05b0.mp4
    my movie file03.mp4 -> 6c9ecec6a75e217fa1786dd6f920a5c1db8473f17447595e0d33d7fc3530453a.mp4
    my movie file04.mp4 -> 96a29ae5bc3a5e35537bcea1068c746ddba205d17fcc8e522a52402c7ed69927.mp4
    my movie file05.mp4 -> 189207823129b57e52c86a743ea7154f65b8eccb5182f322abc3aae35f14057d.mp4
    my movie file06.mp4 -> 93be0fd15be56bb22af94a0a0aa272d56f61e77160f1170305d41166d6135913.mp4
    my movie file07.mp4 -> 05c92b5632414f14445af3facdb0e187746b3b0f40bad94a5f351aeb7f9a8847.mp4
    my movie file08.mp4 -> 0d3cf9c165d4c18af8922b8daa9a6b1031c6762d179fb910b968d60af9aed255.mp4
    my movie file09.mp4 -> da04afebaec6ea3d43dc3a0e66b790ab13751b00fd81aa1f882986c097c36298.mp4
    my movie file10.mp4 -> 47bf532970d3e759346462a85d15ab9d9171c5a7688a6e90a1e976e071ecd167.mp4


    And, to keep the original name:



    $ rename -n 'chomp(my $new=`sha256sum "$_" | cut -d" " -f1`); s/.*/$_ $new.mp4/' *mp4
    my movie file01.mp4 -> my movie file01.mp4 6a260f4adeb47000afeb9c53029ce0e14b6ea80be5c649ecdb155c8e7cfa1496.mp4
    my movie file02.mp4 -> my movie file02.mp4 14f24228ead10d639ef3db11ea09c6216aa80879327a2a57fb549e6474fb05b0.mp4
    my movie file03.mp4 -> my movie file03.mp4 6c9ecec6a75e217fa1786dd6f920a5c1db8473f17447595e0d33d7fc3530453a.mp4
    my movie file04.mp4 -> my movie file04.mp4 96a29ae5bc3a5e35537bcea1068c746ddba205d17fcc8e522a52402c7ed69927.mp4
    my movie file05.mp4 -> my movie file05.mp4 189207823129b57e52c86a743ea7154f65b8eccb5182f322abc3aae35f14057d.mp4
    my movie file06.mp4 -> my movie file06.mp4 93be0fd15be56bb22af94a0a0aa272d56f61e77160f1170305d41166d6135913.mp4
    my movie file07.mp4 -> my movie file07.mp4 05c92b5632414f14445af3facdb0e187746b3b0f40bad94a5f351aeb7f9a8847.mp4
    my movie file08.mp4 -> my movie file08.mp4 0d3cf9c165d4c18af8922b8daa9a6b1031c6762d179fb910b968d60af9aed255.mp4
    my movie file09.mp4 -> my movie file09.mp4 da04afebaec6ea3d43dc3a0e66b790ab13751b00fd81aa1f882986c097c36298.mp4
    my movie file10.mp4 -> my movie file10.mp4 47bf532970d3e759346462a85d15ab9d9171c5a7688a6e90a1e976e071ecd167.mp4


    The explanation here is a bit complicated as it requires understanding some basic Perl. The main idea is:





    • my $new=`sha256sum "$_" | cut -d" " -f1` : this will run the command sha256sum on each input file ($_ is Perl magic for "current thing"), and cut to keep only the hash. To illustrate:



      $ sha256sum my movie file01.mp4 
      6a260f4adeb47000afeb9c53029ce0e14b6ea80be5c649ecdb155c8e7cfa1496 my movie file01.mp4
      $ sha256sum my movie file01.mp4 | cut -d" " -f1
      6a260f4adeb47000afeb9c53029ce0e14b6ea80be5c649ecdb155c8e7cfa1496


      The chomp() just removes the trailing newline from the command's output. The result is stored in the variable $new.




    • s/.*/$new.mp4/' : substitute everything (.*) with the $new name.


    Or, for the other command:





    • s/.*/$_ $new.mp4/' : substitute everything with the original name ($_) followed by the new one.






    share|improve this answer


























    • does not work. rename -n 'chomp($new=sha256sum "$_" | cut -d" " -f1); s/.*/$_ $new.mp4/' *mp4 Global symbol "$new" requires explicit package name (did you forget to declare "my $new"?) at (user-supplied code).

      – VeeJay
      7 hours ago











    • @VeeJay wow, Ubuntu seems to have changed rename so that it runs with use strict! How strange. Thanks for the heads up, see updated answer.

      – terdon
      7 hours ago














    2












    2








    2







    You can actually do this with this rename command:



    rename 'chomp(my $new=`sha256sum "$_" | cut -d" " -f1`); s/.*/$new.mp4/' *mp4


    To preview what it will do before actually renaming, use -n. For example:



    $ ls
    'my movie file01.mp4' 'my movie file05.mp4' 'my movie file09.mp4'
    'my movie file02.mp4' 'my movie file06.mp4' 'my movie file10.mp4'
    'my movie file03.mp4' 'my movie file07.mp4'
    'my movie file04.mp4' 'my movie file08.mp4'

    $ rename -n 'chomp(my $new=`sha256sum "$_" | cut -d" " -f1`); s/.*/$new.mp4/' *mp4
    my movie file01.mp4 -> 6a260f4adeb47000afeb9c53029ce0e14b6ea80be5c649ecdb155c8e7cfa1496.mp4
    my movie file02.mp4 -> 14f24228ead10d639ef3db11ea09c6216aa80879327a2a57fb549e6474fb05b0.mp4
    my movie file03.mp4 -> 6c9ecec6a75e217fa1786dd6f920a5c1db8473f17447595e0d33d7fc3530453a.mp4
    my movie file04.mp4 -> 96a29ae5bc3a5e35537bcea1068c746ddba205d17fcc8e522a52402c7ed69927.mp4
    my movie file05.mp4 -> 189207823129b57e52c86a743ea7154f65b8eccb5182f322abc3aae35f14057d.mp4
    my movie file06.mp4 -> 93be0fd15be56bb22af94a0a0aa272d56f61e77160f1170305d41166d6135913.mp4
    my movie file07.mp4 -> 05c92b5632414f14445af3facdb0e187746b3b0f40bad94a5f351aeb7f9a8847.mp4
    my movie file08.mp4 -> 0d3cf9c165d4c18af8922b8daa9a6b1031c6762d179fb910b968d60af9aed255.mp4
    my movie file09.mp4 -> da04afebaec6ea3d43dc3a0e66b790ab13751b00fd81aa1f882986c097c36298.mp4
    my movie file10.mp4 -> 47bf532970d3e759346462a85d15ab9d9171c5a7688a6e90a1e976e071ecd167.mp4


    And, to keep the original name:



    $ rename -n 'chomp(my $new=`sha256sum "$_" | cut -d" " -f1`); s/.*/$_ $new.mp4/' *mp4
    my movie file01.mp4 -> my movie file01.mp4 6a260f4adeb47000afeb9c53029ce0e14b6ea80be5c649ecdb155c8e7cfa1496.mp4
    my movie file02.mp4 -> my movie file02.mp4 14f24228ead10d639ef3db11ea09c6216aa80879327a2a57fb549e6474fb05b0.mp4
    my movie file03.mp4 -> my movie file03.mp4 6c9ecec6a75e217fa1786dd6f920a5c1db8473f17447595e0d33d7fc3530453a.mp4
    my movie file04.mp4 -> my movie file04.mp4 96a29ae5bc3a5e35537bcea1068c746ddba205d17fcc8e522a52402c7ed69927.mp4
    my movie file05.mp4 -> my movie file05.mp4 189207823129b57e52c86a743ea7154f65b8eccb5182f322abc3aae35f14057d.mp4
    my movie file06.mp4 -> my movie file06.mp4 93be0fd15be56bb22af94a0a0aa272d56f61e77160f1170305d41166d6135913.mp4
    my movie file07.mp4 -> my movie file07.mp4 05c92b5632414f14445af3facdb0e187746b3b0f40bad94a5f351aeb7f9a8847.mp4
    my movie file08.mp4 -> my movie file08.mp4 0d3cf9c165d4c18af8922b8daa9a6b1031c6762d179fb910b968d60af9aed255.mp4
    my movie file09.mp4 -> my movie file09.mp4 da04afebaec6ea3d43dc3a0e66b790ab13751b00fd81aa1f882986c097c36298.mp4
    my movie file10.mp4 -> my movie file10.mp4 47bf532970d3e759346462a85d15ab9d9171c5a7688a6e90a1e976e071ecd167.mp4


    The explanation here is a bit complicated as it requires understanding some basic Perl. The main idea is:





    • my $new=`sha256sum "$_" | cut -d" " -f1` : this will run the command sha256sum on each input file ($_ is Perl magic for "current thing"), and cut to keep only the hash. To illustrate:



      $ sha256sum my movie file01.mp4 
      6a260f4adeb47000afeb9c53029ce0e14b6ea80be5c649ecdb155c8e7cfa1496 my movie file01.mp4
      $ sha256sum my movie file01.mp4 | cut -d" " -f1
      6a260f4adeb47000afeb9c53029ce0e14b6ea80be5c649ecdb155c8e7cfa1496


      The chomp() just removes the trailing newline from the command's output. The result is stored in the variable $new.




    • s/.*/$new.mp4/' : substitute everything (.*) with the $new name.


    Or, for the other command:





    • s/.*/$_ $new.mp4/' : substitute everything with the original name ($_) followed by the new one.






    share|improve this answer















    You can actually do this with this rename command:



    rename 'chomp(my $new=`sha256sum "$_" | cut -d" " -f1`); s/.*/$new.mp4/' *mp4


    To preview what it will do before actually renaming, use -n. For example:



    $ ls
    'my movie file01.mp4' 'my movie file05.mp4' 'my movie file09.mp4'
    'my movie file02.mp4' 'my movie file06.mp4' 'my movie file10.mp4'
    'my movie file03.mp4' 'my movie file07.mp4'
    'my movie file04.mp4' 'my movie file08.mp4'

    $ rename -n 'chomp(my $new=`sha256sum "$_" | cut -d" " -f1`); s/.*/$new.mp4/' *mp4
    my movie file01.mp4 -> 6a260f4adeb47000afeb9c53029ce0e14b6ea80be5c649ecdb155c8e7cfa1496.mp4
    my movie file02.mp4 -> 14f24228ead10d639ef3db11ea09c6216aa80879327a2a57fb549e6474fb05b0.mp4
    my movie file03.mp4 -> 6c9ecec6a75e217fa1786dd6f920a5c1db8473f17447595e0d33d7fc3530453a.mp4
    my movie file04.mp4 -> 96a29ae5bc3a5e35537bcea1068c746ddba205d17fcc8e522a52402c7ed69927.mp4
    my movie file05.mp4 -> 189207823129b57e52c86a743ea7154f65b8eccb5182f322abc3aae35f14057d.mp4
    my movie file06.mp4 -> 93be0fd15be56bb22af94a0a0aa272d56f61e77160f1170305d41166d6135913.mp4
    my movie file07.mp4 -> 05c92b5632414f14445af3facdb0e187746b3b0f40bad94a5f351aeb7f9a8847.mp4
    my movie file08.mp4 -> 0d3cf9c165d4c18af8922b8daa9a6b1031c6762d179fb910b968d60af9aed255.mp4
    my movie file09.mp4 -> da04afebaec6ea3d43dc3a0e66b790ab13751b00fd81aa1f882986c097c36298.mp4
    my movie file10.mp4 -> 47bf532970d3e759346462a85d15ab9d9171c5a7688a6e90a1e976e071ecd167.mp4


    And, to keep the original name:



    $ rename -n 'chomp(my $new=`sha256sum "$_" | cut -d" " -f1`); s/.*/$_ $new.mp4/' *mp4
    my movie file01.mp4 -> my movie file01.mp4 6a260f4adeb47000afeb9c53029ce0e14b6ea80be5c649ecdb155c8e7cfa1496.mp4
    my movie file02.mp4 -> my movie file02.mp4 14f24228ead10d639ef3db11ea09c6216aa80879327a2a57fb549e6474fb05b0.mp4
    my movie file03.mp4 -> my movie file03.mp4 6c9ecec6a75e217fa1786dd6f920a5c1db8473f17447595e0d33d7fc3530453a.mp4
    my movie file04.mp4 -> my movie file04.mp4 96a29ae5bc3a5e35537bcea1068c746ddba205d17fcc8e522a52402c7ed69927.mp4
    my movie file05.mp4 -> my movie file05.mp4 189207823129b57e52c86a743ea7154f65b8eccb5182f322abc3aae35f14057d.mp4
    my movie file06.mp4 -> my movie file06.mp4 93be0fd15be56bb22af94a0a0aa272d56f61e77160f1170305d41166d6135913.mp4
    my movie file07.mp4 -> my movie file07.mp4 05c92b5632414f14445af3facdb0e187746b3b0f40bad94a5f351aeb7f9a8847.mp4
    my movie file08.mp4 -> my movie file08.mp4 0d3cf9c165d4c18af8922b8daa9a6b1031c6762d179fb910b968d60af9aed255.mp4
    my movie file09.mp4 -> my movie file09.mp4 da04afebaec6ea3d43dc3a0e66b790ab13751b00fd81aa1f882986c097c36298.mp4
    my movie file10.mp4 -> my movie file10.mp4 47bf532970d3e759346462a85d15ab9d9171c5a7688a6e90a1e976e071ecd167.mp4


    The explanation here is a bit complicated as it requires understanding some basic Perl. The main idea is:





    • my $new=`sha256sum "$_" | cut -d" " -f1` : this will run the command sha256sum on each input file ($_ is Perl magic for "current thing"), and cut to keep only the hash. To illustrate:



      $ sha256sum my movie file01.mp4 
      6a260f4adeb47000afeb9c53029ce0e14b6ea80be5c649ecdb155c8e7cfa1496 my movie file01.mp4
      $ sha256sum my movie file01.mp4 | cut -d" " -f1
      6a260f4adeb47000afeb9c53029ce0e14b6ea80be5c649ecdb155c8e7cfa1496


      The chomp() just removes the trailing newline from the command's output. The result is stored in the variable $new.




    • s/.*/$new.mp4/' : substitute everything (.*) with the $new name.


    Or, for the other command:





    • s/.*/$_ $new.mp4/' : substitute everything with the original name ($_) followed by the new one.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited 7 hours ago

























    answered 9 hours ago









    terdonterdon

    66.5k12139221




    66.5k12139221













    • does not work. rename -n 'chomp($new=sha256sum "$_" | cut -d" " -f1); s/.*/$_ $new.mp4/' *mp4 Global symbol "$new" requires explicit package name (did you forget to declare "my $new"?) at (user-supplied code).

      – VeeJay
      7 hours ago











    • @VeeJay wow, Ubuntu seems to have changed rename so that it runs with use strict! How strange. Thanks for the heads up, see updated answer.

      – terdon
      7 hours ago



















    • does not work. rename -n 'chomp($new=sha256sum "$_" | cut -d" " -f1); s/.*/$_ $new.mp4/' *mp4 Global symbol "$new" requires explicit package name (did you forget to declare "my $new"?) at (user-supplied code).

      – VeeJay
      7 hours ago











    • @VeeJay wow, Ubuntu seems to have changed rename so that it runs with use strict! How strange. Thanks for the heads up, see updated answer.

      – terdon
      7 hours ago

















    does not work. rename -n 'chomp($new=sha256sum "$_" | cut -d" " -f1); s/.*/$_ $new.mp4/' *mp4 Global symbol "$new" requires explicit package name (did you forget to declare "my $new"?) at (user-supplied code).

    – VeeJay
    7 hours ago





    does not work. rename -n 'chomp($new=sha256sum "$_" | cut -d" " -f1); s/.*/$_ $new.mp4/' *mp4 Global symbol "$new" requires explicit package name (did you forget to declare "my $new"?) at (user-supplied code).

    – VeeJay
    7 hours ago













    @VeeJay wow, Ubuntu seems to have changed rename so that it runs with use strict! How strange. Thanks for the heads up, see updated answer.

    – terdon
    7 hours ago





    @VeeJay wow, Ubuntu seems to have changed rename so that it runs with use strict! How strange. Thanks for the heads up, see updated answer.

    – terdon
    7 hours ago













    -1














    This needs a simple shell loop:



    for i in *.mp4 ; do
    j="$(openssl dgst -sha256 "$i" | cut '-d ' -f2)"
    mv "$i" "$j.mp4"
    done


    The 2nd part of your question needs a little more work:



    for i in *.mp4 ; do
    j="$(openssl dgst -sha256 "$i" | cut '-d ' -f2)"
    k="${i%%.mp4}"
    mv "$i" "$k_$j.mp4"
    done





    share|improve this answer






























      -1














      This needs a simple shell loop:



      for i in *.mp4 ; do
      j="$(openssl dgst -sha256 "$i" | cut '-d ' -f2)"
      mv "$i" "$j.mp4"
      done


      The 2nd part of your question needs a little more work:



      for i in *.mp4 ; do
      j="$(openssl dgst -sha256 "$i" | cut '-d ' -f2)"
      k="${i%%.mp4}"
      mv "$i" "$k_$j.mp4"
      done





      share|improve this answer




























        -1












        -1








        -1







        This needs a simple shell loop:



        for i in *.mp4 ; do
        j="$(openssl dgst -sha256 "$i" | cut '-d ' -f2)"
        mv "$i" "$j.mp4"
        done


        The 2nd part of your question needs a little more work:



        for i in *.mp4 ; do
        j="$(openssl dgst -sha256 "$i" | cut '-d ' -f2)"
        k="${i%%.mp4}"
        mv "$i" "$k_$j.mp4"
        done





        share|improve this answer















        This needs a simple shell loop:



        for i in *.mp4 ; do
        j="$(openssl dgst -sha256 "$i" | cut '-d ' -f2)"
        mv "$i" "$j.mp4"
        done


        The 2nd part of your question needs a little more work:



        for i in *.mp4 ; do
        j="$(openssl dgst -sha256 "$i" | cut '-d ' -f2)"
        k="${i%%.mp4}"
        mv "$i" "$k_$j.mp4"
        done






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 9 hours ago









        Kulfy

        4,81651743




        4,81651743










        answered 10 hours ago









        waltinatorwaltinator

        22.6k74169




        22.6k74169






















            Yamashita is a new contributor. Be nice, and check out our Code of Conduct.










            draft saved

            draft discarded


















            Yamashita is a new contributor. Be nice, and check out our Code of Conduct.













            Yamashita is a new contributor. Be nice, and check out our Code of Conduct.












            Yamashita is a new contributor. Be nice, and check out our Code of Conduct.
















            Thanks for contributing an answer to Ask Ubuntu!


            • 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%2faskubuntu.com%2fquestions%2f1122247%2fchange-many-file-names-to-hash-sha256%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

            Category:香港粉麵

            List *all* the tuples!

            Channel [V]