Trying to script deleting files from wildcards in a text file

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











up vote
3
down vote

favorite












I'm trying to script deleting files whose filenames match wildcards in a text file.



I'm using the following



if [ -z "$1" ]; then
echo -e "Usage: $(basename $0) FILEn"
exit 1
fi

if [ ! -e "$1" ]; then
echo -e "$1: File doesn't exist.n"
exit 1
fi

while read -r line; do
[ -n "$line" ] && rm -- "$line"
done < "$1"


in the file list there are lines



file*
test*


I get the following if i run this



rm: cannot remove ‘file*’: No such file or directory
rm: cannot remove ‘test*’: No such file or directory


I think the * isn't accepted to delete files such as



file 1
file2
file2.txt
test 001 more teskt.txt


Sorry I'm no linux expert. Maybe someone has an easy answer, maybe replace the * with something?










share|improve this question









New contributor




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























    up vote
    3
    down vote

    favorite












    I'm trying to script deleting files whose filenames match wildcards in a text file.



    I'm using the following



    if [ -z "$1" ]; then
    echo -e "Usage: $(basename $0) FILEn"
    exit 1
    fi

    if [ ! -e "$1" ]; then
    echo -e "$1: File doesn't exist.n"
    exit 1
    fi

    while read -r line; do
    [ -n "$line" ] && rm -- "$line"
    done < "$1"


    in the file list there are lines



    file*
    test*


    I get the following if i run this



    rm: cannot remove ‘file*’: No such file or directory
    rm: cannot remove ‘test*’: No such file or directory


    I think the * isn't accepted to delete files such as



    file 1
    file2
    file2.txt
    test 001 more teskt.txt


    Sorry I'm no linux expert. Maybe someone has an easy answer, maybe replace the * with something?










    share|improve this question









    New contributor




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





















      up vote
      3
      down vote

      favorite









      up vote
      3
      down vote

      favorite











      I'm trying to script deleting files whose filenames match wildcards in a text file.



      I'm using the following



      if [ -z "$1" ]; then
      echo -e "Usage: $(basename $0) FILEn"
      exit 1
      fi

      if [ ! -e "$1" ]; then
      echo -e "$1: File doesn't exist.n"
      exit 1
      fi

      while read -r line; do
      [ -n "$line" ] && rm -- "$line"
      done < "$1"


      in the file list there are lines



      file*
      test*


      I get the following if i run this



      rm: cannot remove ‘file*’: No such file or directory
      rm: cannot remove ‘test*’: No such file or directory


      I think the * isn't accepted to delete files such as



      file 1
      file2
      file2.txt
      test 001 more teskt.txt


      Sorry I'm no linux expert. Maybe someone has an easy answer, maybe replace the * with something?










      share|improve this question









      New contributor




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











      I'm trying to script deleting files whose filenames match wildcards in a text file.



      I'm using the following



      if [ -z "$1" ]; then
      echo -e "Usage: $(basename $0) FILEn"
      exit 1
      fi

      if [ ! -e "$1" ]; then
      echo -e "$1: File doesn't exist.n"
      exit 1
      fi

      while read -r line; do
      [ -n "$line" ] && rm -- "$line"
      done < "$1"


      in the file list there are lines



      file*
      test*


      I get the following if i run this



      rm: cannot remove ‘file*’: No such file or directory
      rm: cannot remove ‘test*’: No such file or directory


      I think the * isn't accepted to delete files such as



      file 1
      file2
      file2.txt
      test 001 more teskt.txt


      Sorry I'm no linux expert. Maybe someone has an easy answer, maybe replace the * with something?







      command-line bash scripts






      share|improve this question









      New contributor




      Marco 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




      Marco 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 3 hours ago









      wjandrea

      7,23342256




      7,23342256






      New contributor




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









      asked 3 hours ago









      Marco

      165




      165




      New contributor




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





      New contributor





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






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




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          4
          down vote



          accepted










          Pathname expansion happens after variable expansion, but only on unquoted parts of the command line.



          rm -- $line # <- no double quotes to expand wildcards


          This wouldn't work for filenames containing spaces, as word splitting also happens after variable expansion.



          You can use Perl to expand globs:



          perl -lne 'unlink glob' -- list.txt


          Normal glob needs whitespace backslashed, but you can switch to File::Glob for a different behaviour if it's more convenient to you:



          perl -MFile::Glob=:bsd_glob -lne 'unlink glob' -- list.txt





          share|improve this answer






















          • To clarify, this won't work as expected if $line contains spaces, but it will still delete filenames that contain spaces. By the way, is there a way to do pathname expansion without word splitting?
            – wjandrea
            3 hours ago







          • 1




            or just iterate over glob . . . less mess that way
            – Sergiy Kolodyazhnyy
            3 hours ago










          • Is there an command/way to be able to do this on lines with spaces? Maybe replace al " " with " " I have no idea how to use glob i know my basics but
            – Marco
            3 hours ago











          • @Marco For a thorough solution, you'd have to escape every character in $IFS.
            – wjandrea
            3 hours ago






          • 1




            No, backslashing doesn't work, because it doesn't prevent wordsplitting. Switch to a real programming language where variables are first class citizens, not expanding macros.
            – choroba
            3 hours ago

















          up vote
          -1
          down vote













          Thanks all



          perl -MFile::Glob=:bsd_glob -lne 'unlink glob' -- list.txt


          work well upto now





          share








          New contributor




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

















            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',
            convertImagesToLinks: true,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );






            Marco 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%2f1079014%2ftrying-to-script-deleting-files-from-wildcards-in-a-text-file%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
            4
            down vote



            accepted










            Pathname expansion happens after variable expansion, but only on unquoted parts of the command line.



            rm -- $line # <- no double quotes to expand wildcards


            This wouldn't work for filenames containing spaces, as word splitting also happens after variable expansion.



            You can use Perl to expand globs:



            perl -lne 'unlink glob' -- list.txt


            Normal glob needs whitespace backslashed, but you can switch to File::Glob for a different behaviour if it's more convenient to you:



            perl -MFile::Glob=:bsd_glob -lne 'unlink glob' -- list.txt





            share|improve this answer






















            • To clarify, this won't work as expected if $line contains spaces, but it will still delete filenames that contain spaces. By the way, is there a way to do pathname expansion without word splitting?
              – wjandrea
              3 hours ago







            • 1




              or just iterate over glob . . . less mess that way
              – Sergiy Kolodyazhnyy
              3 hours ago










            • Is there an command/way to be able to do this on lines with spaces? Maybe replace al " " with " " I have no idea how to use glob i know my basics but
              – Marco
              3 hours ago











            • @Marco For a thorough solution, you'd have to escape every character in $IFS.
              – wjandrea
              3 hours ago






            • 1




              No, backslashing doesn't work, because it doesn't prevent wordsplitting. Switch to a real programming language where variables are first class citizens, not expanding macros.
              – choroba
              3 hours ago














            up vote
            4
            down vote



            accepted










            Pathname expansion happens after variable expansion, but only on unquoted parts of the command line.



            rm -- $line # <- no double quotes to expand wildcards


            This wouldn't work for filenames containing spaces, as word splitting also happens after variable expansion.



            You can use Perl to expand globs:



            perl -lne 'unlink glob' -- list.txt


            Normal glob needs whitespace backslashed, but you can switch to File::Glob for a different behaviour if it's more convenient to you:



            perl -MFile::Glob=:bsd_glob -lne 'unlink glob' -- list.txt





            share|improve this answer






















            • To clarify, this won't work as expected if $line contains spaces, but it will still delete filenames that contain spaces. By the way, is there a way to do pathname expansion without word splitting?
              – wjandrea
              3 hours ago







            • 1




              or just iterate over glob . . . less mess that way
              – Sergiy Kolodyazhnyy
              3 hours ago










            • Is there an command/way to be able to do this on lines with spaces? Maybe replace al " " with " " I have no idea how to use glob i know my basics but
              – Marco
              3 hours ago











            • @Marco For a thorough solution, you'd have to escape every character in $IFS.
              – wjandrea
              3 hours ago






            • 1




              No, backslashing doesn't work, because it doesn't prevent wordsplitting. Switch to a real programming language where variables are first class citizens, not expanding macros.
              – choroba
              3 hours ago












            up vote
            4
            down vote



            accepted







            up vote
            4
            down vote



            accepted






            Pathname expansion happens after variable expansion, but only on unquoted parts of the command line.



            rm -- $line # <- no double quotes to expand wildcards


            This wouldn't work for filenames containing spaces, as word splitting also happens after variable expansion.



            You can use Perl to expand globs:



            perl -lne 'unlink glob' -- list.txt


            Normal glob needs whitespace backslashed, but you can switch to File::Glob for a different behaviour if it's more convenient to you:



            perl -MFile::Glob=:bsd_glob -lne 'unlink glob' -- list.txt





            share|improve this answer














            Pathname expansion happens after variable expansion, but only on unquoted parts of the command line.



            rm -- $line # <- no double quotes to expand wildcards


            This wouldn't work for filenames containing spaces, as word splitting also happens after variable expansion.



            You can use Perl to expand globs:



            perl -lne 'unlink glob' -- list.txt


            Normal glob needs whitespace backslashed, but you can switch to File::Glob for a different behaviour if it's more convenient to you:



            perl -MFile::Glob=:bsd_glob -lne 'unlink glob' -- list.txt






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited 3 hours ago

























            answered 3 hours ago









            choroba

            6,15911728




            6,15911728











            • To clarify, this won't work as expected if $line contains spaces, but it will still delete filenames that contain spaces. By the way, is there a way to do pathname expansion without word splitting?
              – wjandrea
              3 hours ago







            • 1




              or just iterate over glob . . . less mess that way
              – Sergiy Kolodyazhnyy
              3 hours ago










            • Is there an command/way to be able to do this on lines with spaces? Maybe replace al " " with " " I have no idea how to use glob i know my basics but
              – Marco
              3 hours ago











            • @Marco For a thorough solution, you'd have to escape every character in $IFS.
              – wjandrea
              3 hours ago






            • 1




              No, backslashing doesn't work, because it doesn't prevent wordsplitting. Switch to a real programming language where variables are first class citizens, not expanding macros.
              – choroba
              3 hours ago
















            • To clarify, this won't work as expected if $line contains spaces, but it will still delete filenames that contain spaces. By the way, is there a way to do pathname expansion without word splitting?
              – wjandrea
              3 hours ago







            • 1




              or just iterate over glob . . . less mess that way
              – Sergiy Kolodyazhnyy
              3 hours ago










            • Is there an command/way to be able to do this on lines with spaces? Maybe replace al " " with " " I have no idea how to use glob i know my basics but
              – Marco
              3 hours ago











            • @Marco For a thorough solution, you'd have to escape every character in $IFS.
              – wjandrea
              3 hours ago






            • 1




              No, backslashing doesn't work, because it doesn't prevent wordsplitting. Switch to a real programming language where variables are first class citizens, not expanding macros.
              – choroba
              3 hours ago















            To clarify, this won't work as expected if $line contains spaces, but it will still delete filenames that contain spaces. By the way, is there a way to do pathname expansion without word splitting?
            – wjandrea
            3 hours ago





            To clarify, this won't work as expected if $line contains spaces, but it will still delete filenames that contain spaces. By the way, is there a way to do pathname expansion without word splitting?
            – wjandrea
            3 hours ago





            1




            1




            or just iterate over glob . . . less mess that way
            – Sergiy Kolodyazhnyy
            3 hours ago




            or just iterate over glob . . . less mess that way
            – Sergiy Kolodyazhnyy
            3 hours ago












            Is there an command/way to be able to do this on lines with spaces? Maybe replace al " " with " " I have no idea how to use glob i know my basics but
            – Marco
            3 hours ago





            Is there an command/way to be able to do this on lines with spaces? Maybe replace al " " with " " I have no idea how to use glob i know my basics but
            – Marco
            3 hours ago













            @Marco For a thorough solution, you'd have to escape every character in $IFS.
            – wjandrea
            3 hours ago




            @Marco For a thorough solution, you'd have to escape every character in $IFS.
            – wjandrea
            3 hours ago




            1




            1




            No, backslashing doesn't work, because it doesn't prevent wordsplitting. Switch to a real programming language where variables are first class citizens, not expanding macros.
            – choroba
            3 hours ago




            No, backslashing doesn't work, because it doesn't prevent wordsplitting. Switch to a real programming language where variables are first class citizens, not expanding macros.
            – choroba
            3 hours ago












            up vote
            -1
            down vote













            Thanks all



            perl -MFile::Glob=:bsd_glob -lne 'unlink glob' -- list.txt


            work well upto now





            share








            New contributor




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





















              up vote
              -1
              down vote













              Thanks all



              perl -MFile::Glob=:bsd_glob -lne 'unlink glob' -- list.txt


              work well upto now





              share








              New contributor




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



















                up vote
                -1
                down vote










                up vote
                -1
                down vote









                Thanks all



                perl -MFile::Glob=:bsd_glob -lne 'unlink glob' -- list.txt


                work well upto now





                share








                New contributor




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









                Thanks all



                perl -MFile::Glob=:bsd_glob -lne 'unlink glob' -- list.txt


                work well upto now






                share








                New contributor




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








                share


                share






                New contributor




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









                answered 9 mins ago









                Marco

                165




                165




                New contributor




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





                New contributor





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






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




















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









                     

                    draft saved


                    draft discarded


















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












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











                    Marco 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%2f1079014%2ftrying-to-script-deleting-files-from-wildcards-in-a-text-file%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