Search code examples
unixdelete-filerm

Delete files from a list in a text file


I have a text file containing around 500 lines. Each line is an absolute path to a file. I want to delete these files using a script.

There's a suggestion here but my files have spaces in them. They have been treated with \ to escape the space but it still doesn't work. There is discussion on that thread about problems with white spaces but no solutions.

I can't simply use the find command as that won't give me the precise result, I need to use the list (which was created by running find and editing out the discrepancies).

Edit: some context. I noticed that iTunes has re-downloaded and copied multiple songs and put them in the same directory as the original songs, e.g., inside a particular album directory is '01 This Song.aac' and '01 This Song 1.aac'.

I ran a find to produce a text file with all songs matching "* 1.*" to get songs ending in 1 but of any file type. I ran this in my iTunes Media/Music directory.

Some of these songs included in the file had the number 1 in but weren't actually duplicates (victims of circumstance), so I manually deleted them.

The file I am left with is around 500 lines with songs all including spaces in the filenames. Because it's an iTunes issue, there are just a few songs in one directory, then more in another, then another, and so on -- I can't just run a script on a single directory, it has to work recursively and run only on the files named in my list.txt


Solution

  • As you would expect, the trick is to get the quoting right:

    while read line; do rm "$line"; done < filename