Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Mac OS X > Deleting files in Terminal

Deleting files in Terminal
Thread Tools
Fresh-Faced Recruit
Join Date: Jul 2007
Status: Offline
Reply With Quote
May 31, 2009, 03:51 AM
 
Hi all

Very simple question. I need to delete a load of files in different locations. I have a txt file with all the file locations in it.

Is there any way I can pipe it into rm to so that I do not have to delete them individually. There are about 300 or so files so I really need to be able to pull the info from the txt file.

Any help would be greatly appreciated.

Thanks
     
Posting Junkie
Join Date: Oct 2005
Location: Houston, TX
Status: Offline
Reply With Quote
May 31, 2009, 09:51 AM
 
xargs will help you there, something like: cat file_locations.txt | xargs rm -f

You may want to do something like "cat file_locations.txt | xargs echo" first to make sure the paths in the text file are being parsed as expected (whitespace vs null termination, etc).
(Last edited by mduell; May 31, 2009 at 09:59 AM. )
     
Grizzled Veteran
Join Date: Mar 2004
Status: Offline
Reply With Quote
May 31, 2009, 12:20 PM
 
A little trick which helps xargs do the right thing is
to convert newlines to null chars and use the -0 flag:

cat filepaths.txt |tr '\n' '\000' |xargs -0 ls -d

An alternative way (slower perhaps) is the while/read construct:

cat filepaths.txt |while IFS= read -r x; do ls -d "$x"; done

The paths in the text file need to be absolute (full) pathnames,
else you'd need to cd into the same relative parent folder first.
-HI-
     
Posting Junkie
Join Date: Dec 2000
Status: Offline
Reply With Quote
May 31, 2009, 02:19 PM
 
The standard disclaimer is that piping a text file to rm is extremely dangerous, and if there's one little error in your file somewhere, you could end up deleting a lot more than you intended to.

What I myself would do instead would be to make a temporary folder somewhere, and then use xargs -J % mv % /path/to/temporary/folder/ instead of xargs rm -f. That way, it will simply move the files to the folder, and you can then inspect the contents of the folder to make sure it contains only the stuff you want to delete before trashing the folder.

Ticking sound coming from a .pkg package? Don't let the .bom go off! Inspect it first with Pacifist. Macworld - five mice!
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 01:48 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2