Add random number to file names

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











up vote
-2
down vote

favorite
1












I want the new file to have the same name as the new one but with random number in the name.



For example input: sometext.txt, output: sometext_XX.txt and the XX will be a random number each time.



The following code is my attempt but after the tr command it says:




no such directory exists.




#!/bin/bash
END="$#"
i=1
while [ "$i" -le "$END" ]; do
tr -d ' ' < $temp > "input_XX.txt"
i=$(($i+1)
done






share|improve this question


















  • 1




    Sounds like you're trying to re-invent mktemp
    – steeldriver
    Nov 27 '17 at 18:22










  • What do you mean "after the tr command"? How do you know where the error comes from since it doesn't seem to say? Is that the exact error message? Because it doesn't look like anything tr or bash would give. Where's $temp set?
    – ilkkachu
    Nov 27 '17 at 19:24














up vote
-2
down vote

favorite
1












I want the new file to have the same name as the new one but with random number in the name.



For example input: sometext.txt, output: sometext_XX.txt and the XX will be a random number each time.



The following code is my attempt but after the tr command it says:




no such directory exists.




#!/bin/bash
END="$#"
i=1
while [ "$i" -le "$END" ]; do
tr -d ' ' < $temp > "input_XX.txt"
i=$(($i+1)
done






share|improve this question


















  • 1




    Sounds like you're trying to re-invent mktemp
    – steeldriver
    Nov 27 '17 at 18:22










  • What do you mean "after the tr command"? How do you know where the error comes from since it doesn't seem to say? Is that the exact error message? Because it doesn't look like anything tr or bash would give. Where's $temp set?
    – ilkkachu
    Nov 27 '17 at 19:24












up vote
-2
down vote

favorite
1









up vote
-2
down vote

favorite
1






1





I want the new file to have the same name as the new one but with random number in the name.



For example input: sometext.txt, output: sometext_XX.txt and the XX will be a random number each time.



The following code is my attempt but after the tr command it says:




no such directory exists.




#!/bin/bash
END="$#"
i=1
while [ "$i" -le "$END" ]; do
tr -d ' ' < $temp > "input_XX.txt"
i=$(($i+1)
done






share|improve this question














I want the new file to have the same name as the new one but with random number in the name.



For example input: sometext.txt, output: sometext_XX.txt and the XX will be a random number each time.



The following code is my attempt but after the tr command it says:




no such directory exists.




#!/bin/bash
END="$#"
i=1
while [ "$i" -le "$END" ]; do
tr -d ' ' < $temp > "input_XX.txt"
i=$(($i+1)
done








share|improve this question













share|improve this question




share|improve this question








edited Nov 27 '17 at 18:03









jasonwryan

47k14127176




47k14127176










asked Nov 27 '17 at 17:52









Giorgos

1




1







  • 1




    Sounds like you're trying to re-invent mktemp
    – steeldriver
    Nov 27 '17 at 18:22










  • What do you mean "after the tr command"? How do you know where the error comes from since it doesn't seem to say? Is that the exact error message? Because it doesn't look like anything tr or bash would give. Where's $temp set?
    – ilkkachu
    Nov 27 '17 at 19:24












  • 1




    Sounds like you're trying to re-invent mktemp
    – steeldriver
    Nov 27 '17 at 18:22










  • What do you mean "after the tr command"? How do you know where the error comes from since it doesn't seem to say? Is that the exact error message? Because it doesn't look like anything tr or bash would give. Where's $temp set?
    – ilkkachu
    Nov 27 '17 at 19:24







1




1




Sounds like you're trying to re-invent mktemp
– steeldriver
Nov 27 '17 at 18:22




Sounds like you're trying to re-invent mktemp
– steeldriver
Nov 27 '17 at 18:22












What do you mean "after the tr command"? How do you know where the error comes from since it doesn't seem to say? Is that the exact error message? Because it doesn't look like anything tr or bash would give. Where's $temp set?
– ilkkachu
Nov 27 '17 at 19:24




What do you mean "after the tr command"? How do you know where the error comes from since it doesn't seem to say? Is that the exact error message? Because it doesn't look like anything tr or bash would give. Where's $temp set?
– ilkkachu
Nov 27 '17 at 19:24










2 Answers
2






active

oldest

votes

















up vote
0
down vote













Maybe something like this:



#!/bin/bash

for filename; do
base="$filename%.*"
ext="$filename##*."

tr -d ' ' < "$filename" > "$base_$RANDOM.$ext"
done


The for filename will loop over all parameters automatically. The base= and ext= lines separate the filename into the basename and the extension. I use $RANDOM for random numbers, so the size of that will be bigger than two digits. Also note that this expects the input files to be in the current working directory – you'd need to do a bit more work to make this for for files in arbitrary directories.






share|improve this answer



























    up vote
    0
    down vote













    How about just looping over the files? You don’t need to pass them all to the script in one go.



    for file in /path/to/files/*.txt ; do mv “$file” “$file%.*”_”$RANDOM”.txt ;done


    I can’t test this right now, but there is a bash builtin environment variable ($RANDOM) for just such purposes. AFAIK it should change each time its used (someone please correct me if not).



    $RANDOM may need curly braces too for the command to parse correctly, but can’t test it right now as I said.






    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',
      convertImagesToLinks: false,
      noModals: false,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      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%2f407335%2fadd-random-number-to-file-names%23new-answer', 'question_page');

      );

      Post as a guest






























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      0
      down vote













      Maybe something like this:



      #!/bin/bash

      for filename; do
      base="$filename%.*"
      ext="$filename##*."

      tr -d ' ' < "$filename" > "$base_$RANDOM.$ext"
      done


      The for filename will loop over all parameters automatically. The base= and ext= lines separate the filename into the basename and the extension. I use $RANDOM for random numbers, so the size of that will be bigger than two digits. Also note that this expects the input files to be in the current working directory – you'd need to do a bit more work to make this for for files in arbitrary directories.






      share|improve this answer
























        up vote
        0
        down vote













        Maybe something like this:



        #!/bin/bash

        for filename; do
        base="$filename%.*"
        ext="$filename##*."

        tr -d ' ' < "$filename" > "$base_$RANDOM.$ext"
        done


        The for filename will loop over all parameters automatically. The base= and ext= lines separate the filename into the basename and the extension. I use $RANDOM for random numbers, so the size of that will be bigger than two digits. Also note that this expects the input files to be in the current working directory – you'd need to do a bit more work to make this for for files in arbitrary directories.






        share|improve this answer






















          up vote
          0
          down vote










          up vote
          0
          down vote









          Maybe something like this:



          #!/bin/bash

          for filename; do
          base="$filename%.*"
          ext="$filename##*."

          tr -d ' ' < "$filename" > "$base_$RANDOM.$ext"
          done


          The for filename will loop over all parameters automatically. The base= and ext= lines separate the filename into the basename and the extension. I use $RANDOM for random numbers, so the size of that will be bigger than two digits. Also note that this expects the input files to be in the current working directory – you'd need to do a bit more work to make this for for files in arbitrary directories.






          share|improve this answer












          Maybe something like this:



          #!/bin/bash

          for filename; do
          base="$filename%.*"
          ext="$filename##*."

          tr -d ' ' < "$filename" > "$base_$RANDOM.$ext"
          done


          The for filename will loop over all parameters automatically. The base= and ext= lines separate the filename into the basename and the extension. I use $RANDOM for random numbers, so the size of that will be bigger than two digits. Also note that this expects the input files to be in the current working directory – you'd need to do a bit more work to make this for for files in arbitrary directories.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 27 '17 at 18:05









          Andy Dalton

          4,7941520




          4,7941520






















              up vote
              0
              down vote













              How about just looping over the files? You don’t need to pass them all to the script in one go.



              for file in /path/to/files/*.txt ; do mv “$file” “$file%.*”_”$RANDOM”.txt ;done


              I can’t test this right now, but there is a bash builtin environment variable ($RANDOM) for just such purposes. AFAIK it should change each time its used (someone please correct me if not).



              $RANDOM may need curly braces too for the command to parse correctly, but can’t test it right now as I said.






              share|improve this answer


























                up vote
                0
                down vote













                How about just looping over the files? You don’t need to pass them all to the script in one go.



                for file in /path/to/files/*.txt ; do mv “$file” “$file%.*”_”$RANDOM”.txt ;done


                I can’t test this right now, but there is a bash builtin environment variable ($RANDOM) for just such purposes. AFAIK it should change each time its used (someone please correct me if not).



                $RANDOM may need curly braces too for the command to parse correctly, but can’t test it right now as I said.






                share|improve this answer
























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  How about just looping over the files? You don’t need to pass them all to the script in one go.



                  for file in /path/to/files/*.txt ; do mv “$file” “$file%.*”_”$RANDOM”.txt ;done


                  I can’t test this right now, but there is a bash builtin environment variable ($RANDOM) for just such purposes. AFAIK it should change each time its used (someone please correct me if not).



                  $RANDOM may need curly braces too for the command to parse correctly, but can’t test it right now as I said.






                  share|improve this answer














                  How about just looping over the files? You don’t need to pass them all to the script in one go.



                  for file in /path/to/files/*.txt ; do mv “$file” “$file%.*”_”$RANDOM”.txt ;done


                  I can’t test this right now, but there is a bash builtin environment variable ($RANDOM) for just such purposes. AFAIK it should change each time its used (someone please correct me if not).



                  $RANDOM may need curly braces too for the command to parse correctly, but can’t test it right now as I said.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 27 '17 at 20:44

























                  answered Nov 27 '17 at 17:56









                  Joe Healey

                  298212




                  298212



























                       

                      draft saved


                      draft discarded















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f407335%2fadd-random-number-to-file-names%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Popular posts from this blog

                      How to check contact read email or not when send email to Individual?

                      Displaying single band from multi-band raster using QGIS

                      How many registers does an x86_64 CPU actually have?