1

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

Pwdr
  • 303
  • 3
  • 12

2 Answers2

2

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

while read line; do rm "$line"; done < filename
Alex L
  • 1,114
  • 8
  • 11
  • 1
    What would I do with that? Where does it go? Which parts do I replace with my specific attributes (name of text file etc.)? – Pwdr Mar 08 '17 at 15:52
  • Sorry for not being clearer. I'm assuming that "filename" is the name of the text file containing around 500 lines. If the file is named "foo.txt", then substitute "foo.txt" for filename. The code snippet I gave can be typed (or copied-and-pasted) into a Terminal window. It reads the file one line at a time and deletes the file named by what it reads in such a way as to preserve internal space characters. – Alex L Mar 08 '17 at 16:08
1

To remove the file which name has spaces you can just wrap the whole path in quotes.
And to delete the list of files I would recommend to change each line of your file so that it looks like rm call. The fastest way is to use sed. So if your file is in following format:

/home/path/file name.asd
/opt/some/string/another name.wasd
...

The oneliner for that would be something like this:

sed -e 's/^/rm -f "/' file.txt | sed -e 's/$/" ;/' > newfile.sh

First sed replaces beginning of the line with rm -f ", second sed end of the line with " ;.

It would produce file with following content:

rm -rf "/home/path/file name.asd" ;
rm -rf "/opt/some/string/another name.wasd" ;
...

So you can just execute this file as a bash script.

streetturtle
  • 5,472
  • 2
  • 25
  • 43
  • Running this creates newfile.sh, which is empty, and returns: ": unescaped newline inside substitute pattern – Pwdr Mar 08 '17 at 15:44
  • Hmm, it populates newfile.sh in the way you said it should but when running I get: "./newfile.sh: line 21: syntax error: unexpected end of file". Line 21 is the final line (empty) of the script. Also, the files are not deleted. (I'm running on a test directory btw, hence only 21 lines!) – Pwdr Mar 08 '17 at 16:01
  • Ah, yeah. It should be not the double ampersand `&&` but semicolon `;`. I'll update the answer. This time should work... I hope =) – streetturtle Mar 08 '17 at 16:09
  • Worked great. I gave the 'official' answer to @Alex L but only because it was a one-liner, so more efficient. Thanks for your help! – Pwdr Mar 08 '17 at 16:39