rm -f a text file separated by \n

Hello guys, this is my first post here.
Before I reformated my Time Machine Backups drive, I copied several thousands of files to another drive.
After using "Tidy Up!" I constantly had errors telling me I didn't have enough privileges to erase duplicate files. I tried everything I could.
Using chmod -R 775, and chflags -R nouchg made the trick for the first 15,000 files. I even rebooted in single user mode to apply this fix.
I decided to export the path of the missing 5,000 files using Tidy Up!, and after replacing strings with regular expressions in textmate I got something like this:

"/Backup/Time Machine/dir_11735865/Desktop/current books/otros+/Ebooks/(Yoga) - Dream Yoga and the Practice of Natural Light - Namkhai Norbu.rar" "/Backup/Time Machine/dir_11735865/Desktop/current books/otros+/Ebooks/(Yoga) - Easy Steps to Yoga.zip" "/Backup/Time Machine/dir_11735865/Desktop/current books/otros+/Ebooks/(Yoga) - Essence of Yoga.zip" "/Backup/Time Machine/dir_11735865/Desktop/current books/otros+/Ebooks/(Yoga) - Hatha Yoga Pradipika.rar" "/Backup/Time Machine/dir_11735865/Desktop/current books/otros+/Ebooks/(Yoga) - Yoga and Yoga Discipline.rar" "/Backup/Time Machine/dir_11735865/Desktop/current books/otros+/Ebooks/(Yoga) - Yoga Swami Svatmarama_ Hatha yoga pradipika.zip" "/Backup/Time Machine/dir_11735865/Desktop/current books/otros+/Ebooks/(Yoga) - Yoga.rar"

I thought it'd be as simple as appending this several thousand files into the terminal after rm -f
Of course, an "Argument list too long" dumped.
I thought I could write a shell function like this one adapted to my case:

function large_rm ()
{
	while read line; do
		rm -rfv $line
	done
}
large_rm /Erase/duplicates

Then I changed my file to this: (using \n)

"/Backup/Time Machine/dir_11735865/Desktop/current books/otros+/Ebooks/(Yoga) - Dream Yoga and the Practice of Natural Light - Namkhai Norbu.rar"
"/Backup/Time Machine/dir_11735865/Desktop/current books/otros+/Ebooks/(Yoga) - Easy Steps to Yoga.zip"
"/Backup/Time Machine/dir_11735865/Desktop/current books/otros+/Ebooks/(Yoga) - Essence of Yoga.zip"
"/Backup/Time Machine/dir_11735865/Desktop/current books/otros+/Ebooks/(Yoga) - Hatha Yoga Pradipika.rar"
"/Backup/Time Machine/dir_11735865/Desktop/current books/otros+/Ebooks/(Yoga) - Yoga and Yoga Discipline.rar"
"/Backup/Time Machine/dir_11735865/Desktop/current books/otros+/Ebooks/(Yoga) - Yoga Swami Svatmarama_ Hatha yoga pradipika.zip"
"/Backup/Time Machine/dir_11735865/Desktop/current books/otros+/Ebooks/(Yoga) - Yoga.rar"

But I have no idea how to use this. I have no idea how to use the function in the terminal window. I think I'm missing the grep or something. Then tried the same article's find approach, but I just don't know how to use it either.

find /Erase/duplicates -type f -name '".*"' -exec rm -fv {} \;

What am I missing? The grep, the pipe, the part where the text file is read, escaping the ""... I'm completely lost... Please help me!

Gratefully Yours, V�ctor
PS: Excuse me for my complicated way of expressing myself, English is not my mother tongue :o.

You almost had it on your second try.

function large_rm ()
{
	while read line; do
		rm -rfv $line
	done
}
large_rm /Erase/duplicates
function large_rm ()
{
	while read line; do
		rm -rfv "$line"
	done
}
large_rm < /Erase/duplicates

Also remove the quotes from your file.

1 Like

Thank You Corona688!!!!!! It worked perfectly. :smiley:

I realize this has already been solved, but for future reference: another solutions would have been the basic POSIX tool xargs, shipped with any unix-like os.

It's syntax is very close to the -exec switch used in find (another great tool, more of that later). In this case after removing the quotes from the lines in the file, a single command would have sufficed:

xargs rm -f < files.txt

xargs is designed to overcome the Argument list too long error, and in the process has developed a nice set of features.

If you hadn't had a file containing the filenames, you could easily have generated one using find, assuming /Backup/Time Machine/dir_11735865/ is the directory containing the files you wanted to delete:

find /Backup/Time Machine/dir_11735865 -type f | xargs rm -f

The directories would have been left alone, but cleaning them would have been a simple task.

Also Beware of executing rm in a aforementioned way without checking what's going to happen. For example, I usually change the command to echo:

find /Backup/Time Machine/dir_11735865 -type f | xargs echo rm -f

Wow, thanks Ikki:b:, It'd have been great if I knew that back then!
I'll add that command to my mental repository.

Thanks again,
V�ctor.:slight_smile: