rm -f a text file separated by \n


 
Thread Tools Search this Thread
Operating Systems OS X (Apple) rm -f a text file separated by \n
# 1  
Old 06-04-2010
Question 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:
Code:
"/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:
Code:
function large_rm ()
{
	while read line; do
		rm -rfv $line
	done
}
large_rm /Erase/duplicates

Then I changed my file to this: (using \n)
Code:
"/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.
Code:
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 Smilie.
# 2  
Old 06-04-2010
You almost had it on your second try.

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

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

Also remove the quotes from your file.
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 06-04-2010
Thank You Corona688!!!!!! It worked perfectly. Smilie
# 4  
Old 06-22-2010
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:

Code:
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:

Code:
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:

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

# 5  
Old 06-22-2010
Wow, thanks IkkiSmilie, It'd have been great if I knew that back then!
I'll add that command to my mental repository.

Thanks again,
Víctor.Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Match text to lines in a file, iterate backwards until text or text substring matches, print to file

hi all, trying this using shell/bash with sed/awk/grep I have two files, one containing one column, the other containing multiple columns (comma delimited). file1.txt abc12345 def12345 ghi54321 ... file2.txt abc1,text1,texta abc,text2,textb def123,text3,textc gh,text4,textd... (6 Replies)
Discussion started by: shogun1970
6 Replies

2. Shell Programming and Scripting

Convert tab separated text to non-trivial xml. (pwsafe --> KeePassx)

I'd like to take the output of `pwsafe --exportdb > database.txt` and convert it to a KeePassX XML friendly format (feature request in pwsafe). I found flat file converter but the syntax is beyond me with this example. Solutions are welcomed. More details Here is the pwsafe --> KeePassX XML... (2 Replies)
Discussion started by: graysky
2 Replies

3. Shell Programming and Scripting

How to cut a pipe delimited file and paste it with another file to form a comma separated outputfile

Hello ppl I have a requirement to split (cut in unix) a file (A.txt) which is a pipe delimited file into A1.txt and A2.txt Now I have to join (paste in unix) this A2.txt with external file A3.txt to form output file A4.txt which should be CSV (comma separated file) so that third party can... (25 Replies)
Discussion started by: etldev
25 Replies

4. Shell Programming and Scripting

Read record from the text file contain multiple separated values & assign those values to variables

I have a file containing multiple values, some of them are pipe separated which are to be read as separate values and some of them are single value all are these need to store in variables. I need to read this file which is an input to my script Config.txt file name, first path, second... (7 Replies)
Discussion started by: ketanraut
7 Replies

5. Shell Programming and Scripting

Split text separated by ; in a column into multiple columns

Hi, I need help to split a long text in a column which is separated by ; and i need to print them out in multiple columns. My input file is tab-delimited and has 11 columns as below:- aRg02004 21452 asdfwf 21452 21452 4.6e-29 5e-29 -1 3 50 ffg|GGD|9009 14101.10 High class -node. ; ffg|GGD|969... (3 Replies)
Discussion started by: redse171
3 Replies

6. UNIX for Dummies Questions & Answers

[solved] Comma separated values to space separated

Hi, I have a large number of files which are written as csv (comma-separated values). Does anyone know of simple sed/awk command do achieve this? Thanks! ---------- Post updated at 10:59 AM ---------- Previous update was at 10:54 AM ---------- Guess I asked this too soon. Found the... (0 Replies)
Discussion started by: lost.identity
0 Replies

7. Shell Programming and Scripting

Text file to CSV with field data separated by blank lines

Hello, I have some data in a text file where fields are separated by blank lines. There are only 6 fields however some fields have several lines of data as I will explain. Also data in a particular field is not consistently the same size but does end on a blank line. The first field start with... (6 Replies)
Discussion started by: vestport
6 Replies

8. Shell Programming and Scripting

Parsing and filtering multiline text into comma separated line

I have a log file that contains several reports with following format. <Start of delimiter> Report1 header Report1 header continue Report1 header continue Record1 header Record1 header continue Record1 header continue field1 field2 field3 field4 ------... (1 Reply)
Discussion started by: yoda9691
1 Replies

9. Shell Programming and Scripting

How to format file into comma separated text file?

Hi Guys, I have text file which is tab/space separated but I want it to re-format into a comma separated and trim the spaces in between. Can someone spare me a perl or sed script that can do the job? INPUT FILE: 500010245623 500 21-APR-11 05.58.21 PM ... (14 Replies)
Discussion started by: pinpe
14 Replies

10. UNIX for Dummies Questions & Answers

Sum up a decimal column in a tab separated text file and error handling

Hi, I have a small requirement where i need to sum up a column in a text file. Input file 66ab 000000 534385 -00000106350.00 66cd 000000 534485 -00013364511.00 66ad 000000 534485 -00000426548.00 672a 000000 534485 000000650339.82... (5 Replies)
Discussion started by: pssandeep
5 Replies
Login or Register to Ask a Question