MacOS – Simple deletion of files using text file list won’t work

bashmacos

I have to delete files specified by remove.txt
which looks like this:

    img_8138.dng.jpg
    img_8140.dng.jpg
    img_8141.dng.jpg
    img_8143.dng.jpg

I've tried several suggestions from the web:

while read file; do rm "$file"; done < remove.txt

xargs -rd '\n' --arg-file=remove.txt rm -i --

rm $(cat /Volumes/space1/remove.txt)

xargs rm <remove.txt etc

The files are in the current directory some of which
are to be deleted (remove.txt). Deletion works if I use rm
on the current directory. But using the list I get the
error below every time.

rm: cannot remove ‘img_8147.dng.jpg’: No such file or directory

Any ideas what's going wrong?

Best Answer

That error is expected. There is no full path leading to the directory containing the files within remove.txt. Thus, when you run rm $(cat /Volumes/space1/remove.txt) outside of the directory these files reside in, you get that error.

Simply run:

cd /Volumes/space1; rm `cat remove.txt`

Or modify the remove.txt file to include the full path.