3

I have multiple .mkv files in eng.md which listed by codes

    $ grep -i 'mkv' eng.md
    ./Volumes/Transcend/Downloads/The.Adventure.of.English.Ep4.mkv
    ./Volumes/Transcend/Downloads/The.Adventure.of.English.Ep5.mkv
    ./Volumes/Transcend/Downloads/The.Adventure.of.English.Ep6.mkv
    ./Volumes/Transcend/Downloads/The.Adventure.of.English.Ep7.mkv
    ./Volumes/Transcend/Downloads/._The.Adventure.of.English.Ep4.mkv
    ./Volumes/Transcend/Downloads/The.Adventure.of.English.Ep8.mkv
    ./Volumes/Transcend/Downloads/._The.Adventure.of.English.Ep8.mkv
    ./Volumes/Transcend/Downloads/._The.Adventure.of.English.Ep5.mkv
    ./Volumes/Transcend/Downloads/._The.Adventure.of.English.Ep6.mkv

I decide to remove mkv files with pipeline methods and try

$ rm < grep -i 'mkv' eng.md
-bash: grep: No such file or directory

Try alternatively

$ grep -i 'mkv' eng.md | rm 
usage: rm [-f | -i] [-dPRrvW] file ...
       unlink file
grep: eng.md: No such file or directory

How to resolve such a problem?

2 Answers2

4

Like this, (unless it's some huge list of files that exceeds the command line maximum length):

rm $(grep -i mkv eng.md)
agc
  • 7,973
  • 2
  • 29
  • 50
  • 1
    It maybe should be noted that this works unless there are "too many" mkv files, while the solution posted by @shirish works for any number of mkv files. – user1934428 Mar 26 '18 at 05:57
  • and also I would recommend to use `\.mkv$` for the grep pattern who knows if there is `mkv` in the file name of some records – Allan Mar 26 '18 at 05:59
  • Yes, and the `-i` should be dropped. I put everything together in an answer. – user1934428 Mar 26 '18 at 06:02
3

Since neither of the comments really gave a solution which works for every situation, I propose here an approach slightly modified from what shirish suggested in his comment:

grep '[.]mkv$' eng.md | xargs rm
user1934428
  • 19,864
  • 7
  • 42
  • 87