I have a .txt file and on each line is a different file location e.g.
file1.zip
file2.zip
file3.zip
How can I open that file, loop through each line and rm -f filename on each one?
Also, will deleting it throw an error if the file doesn’t exist (has already been deleted) and if so how can I avoid this?
EDIT: The file names may have spaces in them, so this needs to be catered for as well.
You can use a
forloop withcatto iterate through the lines:The
if [ -f $file ]will check if the file exists and is a regular file (not a directory). If the check fails, it will skip it.The
IFS=$'\n'at the top will set the delimiter to be newlines-only; This will allow you to process files with whitespace.