Welcome to Linux Forums!

With a comprehensive Linux Forum, information on various types of Linux software and many Linux Reviews articles, we have all the knowledge you need a click away, or accessible via our knowledgeable members.

Linux Forum ArticlesLinux ForumsLinux Forum DownloadsLinux Hosts
Home|Register|FAQ|Member List|Calendar|Unanswered Posts|Forum Rules|Today's Posts|Advanced Search|
SEARCH FOR IN
Go Back   Linux Forums > GNU Linux Zone > Linux Programming & Scripting
Reload this Page Deleting files stored in a text file
Linux Forums
Linux Forums
Welcome To The Linux Forums!
Welcome to Linux Forums. We pride ourselves in being one of the largest Linux communities on the web, we encourage you to REGISTER on our forums and participate in the community. There are over 150,000 members ready to answer your questions. JOINING US today will allow you to make new posts, get support, send messages to other members and submit downloads to our downloads directory and many other great features!

Linux Programming & Scripting C, Perl, PHP, Bash Scripts, anything programming or script related post in here!

Reply
 
Thread Tools Display Modes
Old 05-14-2008   #1 (permalink)
jwmw1023
Just Joined!
 
Join Date: Nov 2006
Posts: 76
Deleting files stored in a text file

I need help with something. I use the find command to create lists of files of a certain name (or such) that get stored as text files. Sometimes, later on, i want to delete this whole list. I need a command that will look in the text file, read the paths and names, and delete the named files. I suspect it involves cat and grep, but im not quite sure how.

the files are created with this or similar:

find / -iname "name" >> /lists/blah.txt


i need to be able to delete all the listed files in that text file. any help is appreciated.
jwmw1023 is offline   Reply With Quote
Old 05-14-2008   #2 (permalink)
whitey
Just Joined!
 
whitey's Avatar
 
Join Date: May 2008
Location: San Antonio
Posts: 6
Send a message via ICQ to whitey Send a message via MSN to whitey Send a message via Yahoo to whitey
Quote:
Originally Posted by jwmw1023 View Post
find / -iname "name" >> /lists/blah.txt
If you've already created the list of files you want to delete, you can kill it with the following:


# for i in `cat /lists/blah.txt`; do rm -rf $i ; done

This will loop through the list and delete as long as there is an item in the list to delete.

~whitey
whitey is offline   Reply With Quote
Old 05-14-2008   #3 (permalink)
jwmw1023
Just Joined!
 
Join Date: Nov 2006
Posts: 76
cool thanks!!!!!!!!
jwmw1023 is offline   Reply With Quote
Old 05-17-2008   #4 (permalink)
scm
Linux Engineer
 
Join Date: Feb 2005
Posts: 924
Quicker would be
Code:
xargs rm -rf </lists/blah.txt
- no loops necessary.
scm is offline   Reply With Quote
Old 05-17-2008   #5 (permalink)
i92guboj
Linux Engineer
 
Join Date: Nov 2007
Location: Córdoba (Spain)
Posts: 886
Though both ways will fail if the list is long enough. So I prefer this:

Code:
cat filename.txt | while read file; do rm "$file"; done
i92guboj is offline   Reply With Quote
Old 05-17-2008   #6 (permalink)
jwmw1023
Just Joined!
 
Join Date: Nov 2006
Posts: 76
ok thats cool, thanks guys, i really appreciate it. also, what if Im looking for a certain thing like

filepath/filename detected
filepath/filename deleted
filepath/filename update failed.


and i want to find all of the lines with update failed and then remove the file that failed. reason i ask is the output of bdc antivirus cant remove zip files so it does something like this (cant get real output right now, but if you would like it, let me know):

/mnt/hda1/Documents And Settings/Owner/Temorary Files/ax00340340.zip:virus.messupstuff update failed.

I have to use a scanner like bdc, clam is too paranoid and deletes files that i would rather it not like zips that have too much compression or gag viruses that dont do any harm
jwmw1023 is offline   Reply With Quote
Old 05-18-2008   #7 (permalink)
scm
Linux Engineer
 
Join Date: Feb 2005
Posts: 924
Quote:
Originally Posted by i92guboj View Post
Though both ways will fail if the list is long enough.
Unless the Linux implementation of xargs is broken, the xargs solution will work regardless of list size. I suggest you learn the use of xargs before you make misleading statements like that.
scm is offline   Reply With Quote
Old 05-18-2008   #8 (permalink)
i92guboj
Linux Engineer
 
Join Date: Nov 2007
Location: Córdoba (Spain)
Posts: 886
Quote:
Originally Posted by scm View Post
Unless the Linux implementation of xargs is broken, the xargs solution will work regardless of list size. I suggest you learn the use of xargs before you make misleading statements like that.
It's not xargs, it's the shell.

As far as I know, there's a limit on how long a command can be, and that has nothing to do with the command, it's a limitation of the shells themselves.

The while loop will always work, because the command is not long, only a single argument is passed on each iteration.

I would be more than glad to hear proof of the opposite, but I think that I am right.

EDITED: I am not sure anymore about the xargs stuff, I will have to research about it. There's no need to be that harsh though. I am sure you are perfect and never commit mistakes, but I am just a mere mortal and I am happy to stand corrected, though if possible in a more friendly way. I don't intentionally spread misleading statements.

Cheers and take it easy
i92guboj is offline   Reply With Quote
Old 05-18-2008   #9 (permalink)
scm
Linux Engineer
 
Join Date: Feb 2005
Posts: 924
Quote:
Originally Posted by i92guboj View Post
It's not xargs, it's the shell.

As far as I know, there's a limit on how long a command can be, and that has nothing to do with the command, it's a limitation of the shells themselves.
Yes, I know the shell has a limit as to how long the command can be (4096 bytes?) - xargs was invented to overcome that problem by fitting as many of its stdin fields to the command to be run, exec-ing the command as many times as it takes to exhaust that list. Thus it should never blow the shell's limit, unless it's been poorly implemented, which is unlikely. Also, the use of xargs is quicker than a for loop because it doesn't have to exec the command for every file, and is every bit as reliable. And it's less to type.

Sorry if you thought my previous post was a bit harsh - that wasn't my intention - but you did state categorically that "both ways will fail", which isn't true.
scm is offline   Reply With Quote
Old 05-18-2008   #10 (permalink)
i92guboj
Linux Engineer
 
Join Date: Nov 2007
Location: Córdoba (Spain)
Posts: 886
No harm done. I apologise as well. I wan't in the best mood today and I should have refrained from posting.

No problem. I stand corrected and that's a good thing, because I learned something and my innacurate statement will not confuse anyone now that it's been corrected.

All in all it's just another brick in the wall

Thanks.
i92guboj is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off


All times are GMT. The time now is 12:02 AM.

Powered by vBulletin 3.6.8 ©2000 - 2007, content relevant URLs by vBSEO, Property of Core Root.

Content Relevant URLs by vBSEO 3.0.0