Remove files in a directory with ls grep and rm

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











up vote
1
down vote

favorite












I've a bash file on my project root with this line



$ ls | grep -P '^some_pattern_matching_regex_goeshere.txt$' | xargs rm -f


The above line removes all the .txt files from the project root but when I push all the .txt files to another folder e.g process_logs/ and try the same commands with ls, grep and rm its doesn't work.



This is what I tried but not worked to removed files on the process_logs directory.



 $ ls process_logs/ | grep -P '^some_pattern_matching_regex_goeshere.txt$' | xargs rm -f


N.B: I've also tried the command with simple regex pattern like
ls process_logs/ | grep -P '^*.txt$' | xargs rm -f to remove files from directory but It doesn't work though.







share|improve this question



















  • Why not parse ls?
    – Cyrus
    Apr 28 at 8:51














up vote
1
down vote

favorite












I've a bash file on my project root with this line



$ ls | grep -P '^some_pattern_matching_regex_goeshere.txt$' | xargs rm -f


The above line removes all the .txt files from the project root but when I push all the .txt files to another folder e.g process_logs/ and try the same commands with ls, grep and rm its doesn't work.



This is what I tried but not worked to removed files on the process_logs directory.



 $ ls process_logs/ | grep -P '^some_pattern_matching_regex_goeshere.txt$' | xargs rm -f


N.B: I've also tried the command with simple regex pattern like
ls process_logs/ | grep -P '^*.txt$' | xargs rm -f to remove files from directory but It doesn't work though.







share|improve this question



















  • Why not parse ls?
    – Cyrus
    Apr 28 at 8:51












up vote
1
down vote

favorite









up vote
1
down vote

favorite











I've a bash file on my project root with this line



$ ls | grep -P '^some_pattern_matching_regex_goeshere.txt$' | xargs rm -f


The above line removes all the .txt files from the project root but when I push all the .txt files to another folder e.g process_logs/ and try the same commands with ls, grep and rm its doesn't work.



This is what I tried but not worked to removed files on the process_logs directory.



 $ ls process_logs/ | grep -P '^some_pattern_matching_regex_goeshere.txt$' | xargs rm -f


N.B: I've also tried the command with simple regex pattern like
ls process_logs/ | grep -P '^*.txt$' | xargs rm -f to remove files from directory but It doesn't work though.







share|improve this question











I've a bash file on my project root with this line



$ ls | grep -P '^some_pattern_matching_regex_goeshere.txt$' | xargs rm -f


The above line removes all the .txt files from the project root but when I push all the .txt files to another folder e.g process_logs/ and try the same commands with ls, grep and rm its doesn't work.



This is what I tried but not worked to removed files on the process_logs directory.



 $ ls process_logs/ | grep -P '^some_pattern_matching_regex_goeshere.txt$' | xargs rm -f


N.B: I've also tried the command with simple regex pattern like
ls process_logs/ | grep -P '^*.txt$' | xargs rm -f to remove files from directory but It doesn't work though.









share|improve this question










share|improve this question




share|improve this question









asked Apr 28 at 5:36









Being Sunny

1084




1084











  • Why not parse ls?
    – Cyrus
    Apr 28 at 8:51
















  • Why not parse ls?
    – Cyrus
    Apr 28 at 8:51















Why not parse ls?
– Cyrus
Apr 28 at 8:51




Why not parse ls?
– Cyrus
Apr 28 at 8:51










3 Answers
3






active

oldest

votes

















up vote
2
down vote



accepted










Try using find instead. If you don't want find to be recursive, you can use depth options:



find /process_logs -maxdepth 1 -mindepth 1 -type f -name 'some_shell_glob_pattern_here' -delete


Parsing the output of ls is not recommended because ls is not always accurate because ls prints a human-readable version of the filename, which may not match the actual filename. For more info see the parsing ls wiki article.






share|improve this answer





















  • find worked for me
    – Being Sunny
    May 1 at 7:05

















up vote
1
down vote













I agree with using find is the better option. But I want to add why your command is not working.



Your command:



$ ls process_logs/ | grep -P '^some_pattern_matching_regex_goeshere.txt$' | xargs rm -f


ls outputs filenames without the path. But you don't add the path to rm. So it will try to delete file names from your subdirectory in your current/ root directory.



Try



$ cd process_logs/; ls | grep -P '^some_pattern_matching_regex_goeshere.txt$' | xargs rm -f


Or



$ ls process_logs/ | grep -P '^some_pattern_matching_regex_goeshere.txt$' | xargs -i rm -f "process_logs/"





share|improve this answer





















  • Though I'll use find but your answer is very clear to me why my approach with ls doesn't work, thanks :)
    – Being Sunny
    Apr 28 at 12:32

















up vote
0
down vote













If you have a simple shell filename globbing pattern, just use that.



E.g. (the * may be replaced by some more precise pattern),



rm -f process_logs/*.txt


or, if there are hundred of thousands of files, use xargs:



printf '%s' process_logs/*.txt | xargs -0 rm -f


If you absolutely need to use a regular expression, GNU find may do that with



find process_logs -maxdepth 1 -type f -regex '.txt$' -delete


Note that the -regex predicate matches against the complete pathname, not just the filename portion at the end (which is why I anchored the expression to the end of the pathname with $). See also the -regextype option in the GNU find manual.






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%2f440534%2fremove-files-in-a-directory-with-ls-grep-and-rm%23new-answer', 'question_page');

    );

    Post as a guest






























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote



    accepted










    Try using find instead. If you don't want find to be recursive, you can use depth options:



    find /process_logs -maxdepth 1 -mindepth 1 -type f -name 'some_shell_glob_pattern_here' -delete


    Parsing the output of ls is not recommended because ls is not always accurate because ls prints a human-readable version of the filename, which may not match the actual filename. For more info see the parsing ls wiki article.






    share|improve this answer





















    • find worked for me
      – Being Sunny
      May 1 at 7:05














    up vote
    2
    down vote



    accepted










    Try using find instead. If you don't want find to be recursive, you can use depth options:



    find /process_logs -maxdepth 1 -mindepth 1 -type f -name 'some_shell_glob_pattern_here' -delete


    Parsing the output of ls is not recommended because ls is not always accurate because ls prints a human-readable version of the filename, which may not match the actual filename. For more info see the parsing ls wiki article.






    share|improve this answer





















    • find worked for me
      – Being Sunny
      May 1 at 7:05












    up vote
    2
    down vote



    accepted







    up vote
    2
    down vote



    accepted






    Try using find instead. If you don't want find to be recursive, you can use depth options:



    find /process_logs -maxdepth 1 -mindepth 1 -type f -name 'some_shell_glob_pattern_here' -delete


    Parsing the output of ls is not recommended because ls is not always accurate because ls prints a human-readable version of the filename, which may not match the actual filename. For more info see the parsing ls wiki article.






    share|improve this answer













    Try using find instead. If you don't want find to be recursive, you can use depth options:



    find /process_logs -maxdepth 1 -mindepth 1 -type f -name 'some_shell_glob_pattern_here' -delete


    Parsing the output of ls is not recommended because ls is not always accurate because ls prints a human-readable version of the filename, which may not match the actual filename. For more info see the parsing ls wiki article.







    share|improve this answer













    share|improve this answer



    share|improve this answer











    answered Apr 28 at 5:49









    jordanm

    28.9k27690




    28.9k27690











    • find worked for me
      – Being Sunny
      May 1 at 7:05
















    • find worked for me
      – Being Sunny
      May 1 at 7:05















    find worked for me
    – Being Sunny
    May 1 at 7:05




    find worked for me
    – Being Sunny
    May 1 at 7:05












    up vote
    1
    down vote













    I agree with using find is the better option. But I want to add why your command is not working.



    Your command:



    $ ls process_logs/ | grep -P '^some_pattern_matching_regex_goeshere.txt$' | xargs rm -f


    ls outputs filenames without the path. But you don't add the path to rm. So it will try to delete file names from your subdirectory in your current/ root directory.



    Try



    $ cd process_logs/; ls | grep -P '^some_pattern_matching_regex_goeshere.txt$' | xargs rm -f


    Or



    $ ls process_logs/ | grep -P '^some_pattern_matching_regex_goeshere.txt$' | xargs -i rm -f "process_logs/"





    share|improve this answer





















    • Though I'll use find but your answer is very clear to me why my approach with ls doesn't work, thanks :)
      – Being Sunny
      Apr 28 at 12:32














    up vote
    1
    down vote













    I agree with using find is the better option. But I want to add why your command is not working.



    Your command:



    $ ls process_logs/ | grep -P '^some_pattern_matching_regex_goeshere.txt$' | xargs rm -f


    ls outputs filenames without the path. But you don't add the path to rm. So it will try to delete file names from your subdirectory in your current/ root directory.



    Try



    $ cd process_logs/; ls | grep -P '^some_pattern_matching_regex_goeshere.txt$' | xargs rm -f


    Or



    $ ls process_logs/ | grep -P '^some_pattern_matching_regex_goeshere.txt$' | xargs -i rm -f "process_logs/"





    share|improve this answer





















    • Though I'll use find but your answer is very clear to me why my approach with ls doesn't work, thanks :)
      – Being Sunny
      Apr 28 at 12:32












    up vote
    1
    down vote










    up vote
    1
    down vote









    I agree with using find is the better option. But I want to add why your command is not working.



    Your command:



    $ ls process_logs/ | grep -P '^some_pattern_matching_regex_goeshere.txt$' | xargs rm -f


    ls outputs filenames without the path. But you don't add the path to rm. So it will try to delete file names from your subdirectory in your current/ root directory.



    Try



    $ cd process_logs/; ls | grep -P '^some_pattern_matching_regex_goeshere.txt$' | xargs rm -f


    Or



    $ ls process_logs/ | grep -P '^some_pattern_matching_regex_goeshere.txt$' | xargs -i rm -f "process_logs/"





    share|improve this answer













    I agree with using find is the better option. But I want to add why your command is not working.



    Your command:



    $ ls process_logs/ | grep -P '^some_pattern_matching_regex_goeshere.txt$' | xargs rm -f


    ls outputs filenames without the path. But you don't add the path to rm. So it will try to delete file names from your subdirectory in your current/ root directory.



    Try



    $ cd process_logs/; ls | grep -P '^some_pattern_matching_regex_goeshere.txt$' | xargs rm -f


    Or



    $ ls process_logs/ | grep -P '^some_pattern_matching_regex_goeshere.txt$' | xargs -i rm -f "process_logs/"






    share|improve this answer













    share|improve this answer



    share|improve this answer











    answered Apr 28 at 6:18









    RoVo

    84219




    84219











    • Though I'll use find but your answer is very clear to me why my approach with ls doesn't work, thanks :)
      – Being Sunny
      Apr 28 at 12:32
















    • Though I'll use find but your answer is very clear to me why my approach with ls doesn't work, thanks :)
      – Being Sunny
      Apr 28 at 12:32















    Though I'll use find but your answer is very clear to me why my approach with ls doesn't work, thanks :)
    – Being Sunny
    Apr 28 at 12:32




    Though I'll use find but your answer is very clear to me why my approach with ls doesn't work, thanks :)
    – Being Sunny
    Apr 28 at 12:32










    up vote
    0
    down vote













    If you have a simple shell filename globbing pattern, just use that.



    E.g. (the * may be replaced by some more precise pattern),



    rm -f process_logs/*.txt


    or, if there are hundred of thousands of files, use xargs:



    printf '%s' process_logs/*.txt | xargs -0 rm -f


    If you absolutely need to use a regular expression, GNU find may do that with



    find process_logs -maxdepth 1 -type f -regex '.txt$' -delete


    Note that the -regex predicate matches against the complete pathname, not just the filename portion at the end (which is why I anchored the expression to the end of the pathname with $). See also the -regextype option in the GNU find manual.






    share|improve this answer



























      up vote
      0
      down vote













      If you have a simple shell filename globbing pattern, just use that.



      E.g. (the * may be replaced by some more precise pattern),



      rm -f process_logs/*.txt


      or, if there are hundred of thousands of files, use xargs:



      printf '%s' process_logs/*.txt | xargs -0 rm -f


      If you absolutely need to use a regular expression, GNU find may do that with



      find process_logs -maxdepth 1 -type f -regex '.txt$' -delete


      Note that the -regex predicate matches against the complete pathname, not just the filename portion at the end (which is why I anchored the expression to the end of the pathname with $). See also the -regextype option in the GNU find manual.






      share|improve this answer

























        up vote
        0
        down vote










        up vote
        0
        down vote









        If you have a simple shell filename globbing pattern, just use that.



        E.g. (the * may be replaced by some more precise pattern),



        rm -f process_logs/*.txt


        or, if there are hundred of thousands of files, use xargs:



        printf '%s' process_logs/*.txt | xargs -0 rm -f


        If you absolutely need to use a regular expression, GNU find may do that with



        find process_logs -maxdepth 1 -type f -regex '.txt$' -delete


        Note that the -regex predicate matches against the complete pathname, not just the filename portion at the end (which is why I anchored the expression to the end of the pathname with $). See also the -regextype option in the GNU find manual.






        share|improve this answer















        If you have a simple shell filename globbing pattern, just use that.



        E.g. (the * may be replaced by some more precise pattern),



        rm -f process_logs/*.txt


        or, if there are hundred of thousands of files, use xargs:



        printf '%s' process_logs/*.txt | xargs -0 rm -f


        If you absolutely need to use a regular expression, GNU find may do that with



        find process_logs -maxdepth 1 -type f -regex '.txt$' -delete


        Note that the -regex predicate matches against the complete pathname, not just the filename portion at the end (which is why I anchored the expression to the end of the pathname with $). See also the -regextype option in the GNU find manual.







        share|improve this answer















        share|improve this answer



        share|improve this answer








        edited May 20 at 7:41


























        answered May 7 at 18:29









        Kusalananda

        102k13199315




        102k13199315






















             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f440534%2fremove-files-in-a-directory-with-ls-grep-and-rm%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?

            Bahrain

            Postfix configuration issue with fips on centos 7; mailgun relay