Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete elements caught from a file

Tags:

bash

shell

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 Answers

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

rm $(grep -i mkv eng.md)
like image 69
agc Avatar answered Jun 02 '26 07:06

agc


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
like image 38
user1934428 Avatar answered Jun 02 '26 07:06

user1934428