Shell command/script to delete files whose names a

2019-03-10 07:44发布

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 can use. I have bash shell.

5条回答
再贱就再见
2楼-- · 2019-03-10 07:59

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

<list.txt tr "\n" "\0" | xargs -0 rm
查看更多
ゆ 、 Hurt°
3楼-- · 2019-03-10 08:00

Try this command:

rm -f $(<file)
查看更多
祖国的老花朵
4楼-- · 2019-03-10 08:03

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
查看更多
欢心
5楼-- · 2019-03-10 08:10
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
查看更多
Juvenile、少年°
6楼-- · 2019-03-10 08:18

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

查看更多
登录 后发表回答