how to remove all characters after and including “?” in filename?

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





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








-2















I have a large amount of files that were downloaded and ended up having what looks like authentication parameters added to the filename which I would like to remove. Examples:



file1.doc?b1h2uj3b123uiyb12
file2.xls?oi12jo3ij123oij
file3.jpg?0990xcizx0cxzczixuchbiasdu


Is there an easy way for me to strip everything after the question mark on the entire folder of files?










share|improve this question






















  • Are those the only files in the folder?

    – Nasir Riley
    Mar 17 at 19:14











  • Are you sure that's a literal question mark in the file name, and not some special character that e.g. ls might print as a question mark? What does, e.g. printf "%qn" file1.doc* (in Bash) show?

    – ilkkachu
    Mar 17 at 19:25

















-2















I have a large amount of files that were downloaded and ended up having what looks like authentication parameters added to the filename which I would like to remove. Examples:



file1.doc?b1h2uj3b123uiyb12
file2.xls?oi12jo3ij123oij
file3.jpg?0990xcizx0cxzczixuchbiasdu


Is there an easy way for me to strip everything after the question mark on the entire folder of files?










share|improve this question






















  • Are those the only files in the folder?

    – Nasir Riley
    Mar 17 at 19:14











  • Are you sure that's a literal question mark in the file name, and not some special character that e.g. ls might print as a question mark? What does, e.g. printf "%qn" file1.doc* (in Bash) show?

    – ilkkachu
    Mar 17 at 19:25













-2












-2








-2








I have a large amount of files that were downloaded and ended up having what looks like authentication parameters added to the filename which I would like to remove. Examples:



file1.doc?b1h2uj3b123uiyb12
file2.xls?oi12jo3ij123oij
file3.jpg?0990xcizx0cxzczixuchbiasdu


Is there an easy way for me to strip everything after the question mark on the entire folder of files?










share|improve this question














I have a large amount of files that were downloaded and ended up having what looks like authentication parameters added to the filename which I would like to remove. Examples:



file1.doc?b1h2uj3b123uiyb12
file2.xls?oi12jo3ij123oij
file3.jpg?0990xcizx0cxzczixuchbiasdu


Is there an easy way for me to strip everything after the question mark on the entire folder of files?







scripting terminal filenames rename






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 17 at 18:56









PhilPhil

1




1












  • Are those the only files in the folder?

    – Nasir Riley
    Mar 17 at 19:14











  • Are you sure that's a literal question mark in the file name, and not some special character that e.g. ls might print as a question mark? What does, e.g. printf "%qn" file1.doc* (in Bash) show?

    – ilkkachu
    Mar 17 at 19:25

















  • Are those the only files in the folder?

    – Nasir Riley
    Mar 17 at 19:14











  • Are you sure that's a literal question mark in the file name, and not some special character that e.g. ls might print as a question mark? What does, e.g. printf "%qn" file1.doc* (in Bash) show?

    – ilkkachu
    Mar 17 at 19:25
















Are those the only files in the folder?

– Nasir Riley
Mar 17 at 19:14





Are those the only files in the folder?

– Nasir Riley
Mar 17 at 19:14













Are you sure that's a literal question mark in the file name, and not some special character that e.g. ls might print as a question mark? What does, e.g. printf "%qn" file1.doc* (in Bash) show?

– ilkkachu
Mar 17 at 19:25





Are you sure that's a literal question mark in the file name, and not some special character that e.g. ls might print as a question mark? What does, e.g. printf "%qn" file1.doc* (in Bash) show?

– ilkkachu
Mar 17 at 19:25










2 Answers
2






active

oldest

votes


















0














You can match all filenames with a question mark with *?*, and remove the part after the ? from a variable with $var%%?*. The question mark itself is wildcard character, so has to be escaped in both cases.



A simple loop over the files and running mv should do:



for f in ./*?*; do
echo mv -n "$f" "$f%%?*"
done


(The echo is there so you can see what would be done before any changes being made. Remove it if the output looks sensible.)






share|improve this answer






























    0














    Using the Perl rename utility:



    $ rename -v -n 's/[?].*//' *[?]*
    rename(file1.doc?b1h2uj3b123uiyb12, file1.doc)
    rename(file2.xls?oi12jo3ij123oij, file2.xls)
    rename(file3.jpg?0990xcizx0cxzczixuchbiasdu, file3.jpg)


    This applies the Perl substitution s/[?].*// to each name. This would truncate the name just before the first question mark. The *[?]* filename globbing pattern would expand to any name in the current directory that contains a question mark.



    Run the command without -n to actually rename 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%2f506861%2fhow-to-remove-all-characters-after-and-including-in-filename%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









      0














      You can match all filenames with a question mark with *?*, and remove the part after the ? from a variable with $var%%?*. The question mark itself is wildcard character, so has to be escaped in both cases.



      A simple loop over the files and running mv should do:



      for f in ./*?*; do
      echo mv -n "$f" "$f%%?*"
      done


      (The echo is there so you can see what would be done before any changes being made. Remove it if the output looks sensible.)






      share|improve this answer



























        0














        You can match all filenames with a question mark with *?*, and remove the part after the ? from a variable with $var%%?*. The question mark itself is wildcard character, so has to be escaped in both cases.



        A simple loop over the files and running mv should do:



        for f in ./*?*; do
        echo mv -n "$f" "$f%%?*"
        done


        (The echo is there so you can see what would be done before any changes being made. Remove it if the output looks sensible.)






        share|improve this answer

























          0












          0








          0







          You can match all filenames with a question mark with *?*, and remove the part after the ? from a variable with $var%%?*. The question mark itself is wildcard character, so has to be escaped in both cases.



          A simple loop over the files and running mv should do:



          for f in ./*?*; do
          echo mv -n "$f" "$f%%?*"
          done


          (The echo is there so you can see what would be done before any changes being made. Remove it if the output looks sensible.)






          share|improve this answer













          You can match all filenames with a question mark with *?*, and remove the part after the ? from a variable with $var%%?*. The question mark itself is wildcard character, so has to be escaped in both cases.



          A simple loop over the files and running mv should do:



          for f in ./*?*; do
          echo mv -n "$f" "$f%%?*"
          done


          (The echo is there so you can see what would be done before any changes being made. Remove it if the output looks sensible.)







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 17 at 19:29









          ilkkachuilkkachu

          63.4k10104181




          63.4k10104181























              0














              Using the Perl rename utility:



              $ rename -v -n 's/[?].*//' *[?]*
              rename(file1.doc?b1h2uj3b123uiyb12, file1.doc)
              rename(file2.xls?oi12jo3ij123oij, file2.xls)
              rename(file3.jpg?0990xcizx0cxzczixuchbiasdu, file3.jpg)


              This applies the Perl substitution s/[?].*// to each name. This would truncate the name just before the first question mark. The *[?]* filename globbing pattern would expand to any name in the current directory that contains a question mark.



              Run the command without -n to actually rename files.






              share|improve this answer



























                0














                Using the Perl rename utility:



                $ rename -v -n 's/[?].*//' *[?]*
                rename(file1.doc?b1h2uj3b123uiyb12, file1.doc)
                rename(file2.xls?oi12jo3ij123oij, file2.xls)
                rename(file3.jpg?0990xcizx0cxzczixuchbiasdu, file3.jpg)


                This applies the Perl substitution s/[?].*// to each name. This would truncate the name just before the first question mark. The *[?]* filename globbing pattern would expand to any name in the current directory that contains a question mark.



                Run the command without -n to actually rename files.






                share|improve this answer

























                  0












                  0








                  0







                  Using the Perl rename utility:



                  $ rename -v -n 's/[?].*//' *[?]*
                  rename(file1.doc?b1h2uj3b123uiyb12, file1.doc)
                  rename(file2.xls?oi12jo3ij123oij, file2.xls)
                  rename(file3.jpg?0990xcizx0cxzczixuchbiasdu, file3.jpg)


                  This applies the Perl substitution s/[?].*// to each name. This would truncate the name just before the first question mark. The *[?]* filename globbing pattern would expand to any name in the current directory that contains a question mark.



                  Run the command without -n to actually rename files.






                  share|improve this answer













                  Using the Perl rename utility:



                  $ rename -v -n 's/[?].*//' *[?]*
                  rename(file1.doc?b1h2uj3b123uiyb12, file1.doc)
                  rename(file2.xls?oi12jo3ij123oij, file2.xls)
                  rename(file3.jpg?0990xcizx0cxzczixuchbiasdu, file3.jpg)


                  This applies the Perl substitution s/[?].*// to each name. This would truncate the name just before the first question mark. The *[?]* filename globbing pattern would expand to any name in the current directory that contains a question mark.



                  Run the command without -n to actually rename files.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Mar 17 at 19:44









                  KusalanandaKusalananda

                  142k18265440




                  142k18265440



























                      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%2f506861%2fhow-to-remove-all-characters-after-and-including-in-filename%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

                      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?