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

為{幸葍}努か 提交于 2019-12-02 19:45:00
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

Try this command:

rm -f $(<file)

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
yamen

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

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

<list.txt tr "\n" "\0" | xargs -0 rm

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!