Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 14
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, ...
  1. #1
    Just Joined!
    Join Date
    Nov 2006
    Posts
    90

    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.

  2. #2
    Just Joined! whitey's Avatar
    Join Date
    May 2008
    Location
    San Antonio
    Posts
    6
    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

  3. #3
    Just Joined!
    Join Date
    Nov 2006
    Posts
    90
    cool thanks!!!!!!!!

  4. #4
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    Quicker would be
    Code:
    xargs rm -rf </lists/blah.txt
    - no loops necessary.

  5. #5
    Linux Guru
    Join Date
    Nov 2007
    Location
    Córdoba (Spain)
    Posts
    1,513
    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

  6. #6
    Just Joined!
    Join Date
    Nov 2006
    Posts
    90
    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

  7. #7
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    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.

  8. #8
    Linux Guru
    Join Date
    Nov 2007
    Location
    Córdoba (Spain)
    Posts
    1,513
    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

  9. #9
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    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.

  10. #10
    Linux Guru
    Join Date
    Nov 2007
    Location
    Córdoba (Spain)
    Posts
    1,513
    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.

Page 1 of 2 1 2 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...