Shell command/script to delete files whose names are in a text file

后端 未结 6 1953
醉酒成梦
醉酒成梦 2020-12-24 04:00

I have a list of files in a .txt file (say list.txt). I want to delete the files in that list. I haven\'t done scripting before. Could some give the shell script/command I c

相关标签:
6条回答
  • 2020-12-24 04:10

    If the file names have spaces in them, none of the other answers will work; they'll treat each word as a separate file name. Assuming the list of files is in list.txt, this will always work:

    while read name; do
      rm "$name"
    done < list.txt
    
    0 讨论(0)
  • 2020-12-24 04:12
    while read -r filename; do
      rm "$filename"
    done <list.txt
    

    is slow.

    rm $(<list.txt)
    

    will fail if there are too many arguments.

    I think it should work:

    xargs -a list.txt -d'\n' rm
    
    0 讨论(0)
  • 2020-12-24 04:13

    On linux, you can try:

    printf "%s\n" $(<list.txt) | xargs -I@ rm @
    

    In my case, my .txt file contained a list of items of the kind *.ext and worked fine.

    0 讨论(0)
  • 2020-12-24 04:15

    The following should work and leaves you room to do other things as you loop through.

    Edit: Don't do this, see here: http://porkmail.org/era/unix/award.html

    for file in $(cat list.txt); do rm $file; done

    0 讨论(0)
  • 2020-12-24 04:17

    Try this command:

    rm -f $(<file)
    
    0 讨论(0)
  • 2020-12-24 04:22

    For fast execution on macOS, where xargs custom delimiter d is not possible:

    <list.txt tr "\n" "\0" | xargs -0 rm
    
    0 讨论(0)
提交回复
热议问题