Remove files which not named “today.md”

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











up vote
2
down vote

favorite












I have the a series of markdown files in the working directory:



$ ls *.md
csv_reader.md egrep.md find.md found_pdfs.md osPathSep_help.md readme.md smtplib_help.md today.md


I want to remove them except "today.md"



#!/usr/local/bin/bash
for i in ./*.md ; do
if [[ $i != "today.md" ]]; then
echo $i
fi
done


Run it and get



$ bash bash/remove_files.sh
./csv_reader.md
./egrep.md
./find.md
./found_pdfs.md
./osPathSep_help.md
./readme.md
./smtplib_help.md
./today.md


Nonetheless, the structured commands are not handy in the command line, how could I accomplish such a task with shorter commands










share|improve this question









New contributor




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























    up vote
    2
    down vote

    favorite












    I have the a series of markdown files in the working directory:



    $ ls *.md
    csv_reader.md egrep.md find.md found_pdfs.md osPathSep_help.md readme.md smtplib_help.md today.md


    I want to remove them except "today.md"



    #!/usr/local/bin/bash
    for i in ./*.md ; do
    if [[ $i != "today.md" ]]; then
    echo $i
    fi
    done


    Run it and get



    $ bash bash/remove_files.sh
    ./csv_reader.md
    ./egrep.md
    ./find.md
    ./found_pdfs.md
    ./osPathSep_help.md
    ./readme.md
    ./smtplib_help.md
    ./today.md


    Nonetheless, the structured commands are not handy in the command line, how could I accomplish such a task with shorter commands










    share|improve this question









    New contributor




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





















      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      I have the a series of markdown files in the working directory:



      $ ls *.md
      csv_reader.md egrep.md find.md found_pdfs.md osPathSep_help.md readme.md smtplib_help.md today.md


      I want to remove them except "today.md"



      #!/usr/local/bin/bash
      for i in ./*.md ; do
      if [[ $i != "today.md" ]]; then
      echo $i
      fi
      done


      Run it and get



      $ bash bash/remove_files.sh
      ./csv_reader.md
      ./egrep.md
      ./find.md
      ./found_pdfs.md
      ./osPathSep_help.md
      ./readme.md
      ./smtplib_help.md
      ./today.md


      Nonetheless, the structured commands are not handy in the command line, how could I accomplish such a task with shorter commands










      share|improve this question









      New contributor




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











      I have the a series of markdown files in the working directory:



      $ ls *.md
      csv_reader.md egrep.md find.md found_pdfs.md osPathSep_help.md readme.md smtplib_help.md today.md


      I want to remove them except "today.md"



      #!/usr/local/bin/bash
      for i in ./*.md ; do
      if [[ $i != "today.md" ]]; then
      echo $i
      fi
      done


      Run it and get



      $ bash bash/remove_files.sh
      ./csv_reader.md
      ./egrep.md
      ./find.md
      ./found_pdfs.md
      ./osPathSep_help.md
      ./readme.md
      ./smtplib_help.md
      ./today.md


      Nonetheless, the structured commands are not handy in the command line, how could I accomplish such a task with shorter commands







      bash shell-script files rm






      share|improve this question









      New contributor




      Sawajiri 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




      Sawajiri 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 1 hour ago









      Jeff Schaller

      34.8k952115




      34.8k952115






      New contributor




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









      asked 2 hours ago









      Sawajiri

      1225




      1225




      New contributor




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





      New contributor





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






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




















          4 Answers
          4






          active

          oldest

          votes

















          up vote
          3
          down vote













          find . -maxdepth 1 -name '*.md' ! -name today.md -type f -print



          Should find all the files (-type f) in the current directory (. -- or explicitly put a directory name there) only (-maxdepth 1 prevents following subdirectories) that end in .md (-name '*.md'), excluding (!) the file today.md.



          Be sure to include the single quotes around '*.md' so your shell doesn't try to expand that to the list of .md files in the current directory before it executes find.



          It will print the list of files to be deleted. Change -print to -delete to delete them instead.






          share|improve this answer



























            up vote
            1
            down vote













            Use a negative match (requires shopt -s extglob, but possibly already set):



            rm !(today).md


            (you can first use ls instead of rm to check the result).



            Lots of power in extglob, you could also do



            rm !(yesterday|today).md


            if you wanted to spare two files.






            share|improve this answer



























              up vote
              1
              down vote













              You may find it shorter to use bash's extended globbing feature to exclude the file you don't want:



              shopt -s extglob
              echo rm -- !(today).md


              The above (after removing the echo for testing) says to match anything except today followed by .md.



              Your example didn't match dot-files by default; you can change that behavior with shopt dotglob, if desired



              Your script didn't work as written because the glob pattern you used (appropriately) prefixed the filenames with ./; therefore, your inner test should have compared against ./today.md.






              share|improve this answer





























                up vote
                1
                down vote













                The above are better solutions, but the reason your code isn't working is because of the comparison.



                "./today.md" is not equal to "today.md".






                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: "",
                  imageUploader:
                  brandingHtml: "",
                  contentPolicyHtml: "",
                  allowUrls: true
                  ,
                  onDemand: true,
                  discardSelector: ".discard-answer"
                  ,immediatelyShowMarkdownHelp:true
                  );



                  );






                  Sawajiri 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%2funix.stackexchange.com%2fquestions%2f478765%2fremove-files-which-not-named-today-md%23new-answer', 'question_page');

                  );

                  Post as a guest






























                  4 Answers
                  4






                  active

                  oldest

                  votes








                  4 Answers
                  4






                  active

                  oldest

                  votes









                  active

                  oldest

                  votes






                  active

                  oldest

                  votes








                  up vote
                  3
                  down vote













                  find . -maxdepth 1 -name '*.md' ! -name today.md -type f -print



                  Should find all the files (-type f) in the current directory (. -- or explicitly put a directory name there) only (-maxdepth 1 prevents following subdirectories) that end in .md (-name '*.md'), excluding (!) the file today.md.



                  Be sure to include the single quotes around '*.md' so your shell doesn't try to expand that to the list of .md files in the current directory before it executes find.



                  It will print the list of files to be deleted. Change -print to -delete to delete them instead.






                  share|improve this answer
























                    up vote
                    3
                    down vote













                    find . -maxdepth 1 -name '*.md' ! -name today.md -type f -print



                    Should find all the files (-type f) in the current directory (. -- or explicitly put a directory name there) only (-maxdepth 1 prevents following subdirectories) that end in .md (-name '*.md'), excluding (!) the file today.md.



                    Be sure to include the single quotes around '*.md' so your shell doesn't try to expand that to the list of .md files in the current directory before it executes find.



                    It will print the list of files to be deleted. Change -print to -delete to delete them instead.






                    share|improve this answer






















                      up vote
                      3
                      down vote










                      up vote
                      3
                      down vote









                      find . -maxdepth 1 -name '*.md' ! -name today.md -type f -print



                      Should find all the files (-type f) in the current directory (. -- or explicitly put a directory name there) only (-maxdepth 1 prevents following subdirectories) that end in .md (-name '*.md'), excluding (!) the file today.md.



                      Be sure to include the single quotes around '*.md' so your shell doesn't try to expand that to the list of .md files in the current directory before it executes find.



                      It will print the list of files to be deleted. Change -print to -delete to delete them instead.






                      share|improve this answer












                      find . -maxdepth 1 -name '*.md' ! -name today.md -type f -print



                      Should find all the files (-type f) in the current directory (. -- or explicitly put a directory name there) only (-maxdepth 1 prevents following subdirectories) that end in .md (-name '*.md'), excluding (!) the file today.md.



                      Be sure to include the single quotes around '*.md' so your shell doesn't try to expand that to the list of .md files in the current directory before it executes find.



                      It will print the list of files to be deleted. Change -print to -delete to delete them instead.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered 2 hours ago









                      drewbenn

                      5,01251835




                      5,01251835






















                          up vote
                          1
                          down vote













                          Use a negative match (requires shopt -s extglob, but possibly already set):



                          rm !(today).md


                          (you can first use ls instead of rm to check the result).



                          Lots of power in extglob, you could also do



                          rm !(yesterday|today).md


                          if you wanted to spare two files.






                          share|improve this answer
























                            up vote
                            1
                            down vote













                            Use a negative match (requires shopt -s extglob, but possibly already set):



                            rm !(today).md


                            (you can first use ls instead of rm to check the result).



                            Lots of power in extglob, you could also do



                            rm !(yesterday|today).md


                            if you wanted to spare two files.






                            share|improve this answer






















                              up vote
                              1
                              down vote










                              up vote
                              1
                              down vote









                              Use a negative match (requires shopt -s extglob, but possibly already set):



                              rm !(today).md


                              (you can first use ls instead of rm to check the result).



                              Lots of power in extglob, you could also do



                              rm !(yesterday|today).md


                              if you wanted to spare two files.






                              share|improve this answer












                              Use a negative match (requires shopt -s extglob, but possibly already set):



                              rm !(today).md


                              (you can first use ls instead of rm to check the result).



                              Lots of power in extglob, you could also do



                              rm !(yesterday|today).md


                              if you wanted to spare two files.







                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered 1 hour ago









                              xenoid

                              2,1611621




                              2,1611621




















                                  up vote
                                  1
                                  down vote













                                  You may find it shorter to use bash's extended globbing feature to exclude the file you don't want:



                                  shopt -s extglob
                                  echo rm -- !(today).md


                                  The above (after removing the echo for testing) says to match anything except today followed by .md.



                                  Your example didn't match dot-files by default; you can change that behavior with shopt dotglob, if desired



                                  Your script didn't work as written because the glob pattern you used (appropriately) prefixed the filenames with ./; therefore, your inner test should have compared against ./today.md.






                                  share|improve this answer


























                                    up vote
                                    1
                                    down vote













                                    You may find it shorter to use bash's extended globbing feature to exclude the file you don't want:



                                    shopt -s extglob
                                    echo rm -- !(today).md


                                    The above (after removing the echo for testing) says to match anything except today followed by .md.



                                    Your example didn't match dot-files by default; you can change that behavior with shopt dotglob, if desired



                                    Your script didn't work as written because the glob pattern you used (appropriately) prefixed the filenames with ./; therefore, your inner test should have compared against ./today.md.






                                    share|improve this answer
























                                      up vote
                                      1
                                      down vote










                                      up vote
                                      1
                                      down vote









                                      You may find it shorter to use bash's extended globbing feature to exclude the file you don't want:



                                      shopt -s extglob
                                      echo rm -- !(today).md


                                      The above (after removing the echo for testing) says to match anything except today followed by .md.



                                      Your example didn't match dot-files by default; you can change that behavior with shopt dotglob, if desired



                                      Your script didn't work as written because the glob pattern you used (appropriately) prefixed the filenames with ./; therefore, your inner test should have compared against ./today.md.






                                      share|improve this answer














                                      You may find it shorter to use bash's extended globbing feature to exclude the file you don't want:



                                      shopt -s extglob
                                      echo rm -- !(today).md


                                      The above (after removing the echo for testing) says to match anything except today followed by .md.



                                      Your example didn't match dot-files by default; you can change that behavior with shopt dotglob, if desired



                                      Your script didn't work as written because the glob pattern you used (appropriately) prefixed the filenames with ./; therefore, your inner test should have compared against ./today.md.







                                      share|improve this answer














                                      share|improve this answer



                                      share|improve this answer








                                      edited 58 mins ago

























                                      answered 1 hour ago









                                      Jeff Schaller

                                      34.8k952115




                                      34.8k952115




















                                          up vote
                                          1
                                          down vote













                                          The above are better solutions, but the reason your code isn't working is because of the comparison.



                                          "./today.md" is not equal to "today.md".






                                          share|improve this answer
























                                            up vote
                                            1
                                            down vote













                                            The above are better solutions, but the reason your code isn't working is because of the comparison.



                                            "./today.md" is not equal to "today.md".






                                            share|improve this answer






















                                              up vote
                                              1
                                              down vote










                                              up vote
                                              1
                                              down vote









                                              The above are better solutions, but the reason your code isn't working is because of the comparison.



                                              "./today.md" is not equal to "today.md".






                                              share|improve this answer












                                              The above are better solutions, but the reason your code isn't working is because of the comparison.



                                              "./today.md" is not equal to "today.md".







                                              share|improve this answer












                                              share|improve this answer



                                              share|improve this answer










                                              answered 37 mins ago









                                              user208145

                                              1,08621115




                                              1,08621115




















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









                                                   

                                                  draft saved


                                                  draft discarded


















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












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











                                                  Sawajiri 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%2funix.stackexchange.com%2fquestions%2f478765%2fremove-files-which-not-named-today-md%23new-answer', 'question_page');

                                                  );

                                                  Post as a guest













































































                                                  Comments

                                                  Popular posts from this blog

                                                  Long meetings (6-7 hours a day): Being “babysat” by supervisor

                                                  Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

                                                  Confectionery