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
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
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
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.
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
Try this command:
rm -f $(<file)
For fast execution on macOS, where xargs
custom delimiter d
is not possible:
<list.txt tr "\n" "\0" | xargs -0 rm