Results 1 to 5 of 5

Thread: delete the files listed in a plain-text document

  1. #1
    Join Date
    Jun 2008
    Location
    Brisbane, Australia
    Posts
    207

    Lightbulb delete the files listed in a plain-text document

    For all you crazy bash gurus out there.

    I have a list of about 15 files I want to delete. They're on their own line in a text file. how can I go about deleting them from the command line in one command string?

    I mostly just doing this as a personal exercise in bash scripting. I think I understand the for loop thing, but I don't know how to read a specific line number from my text file. Do I use head,sed,tr, cut?

    thanks all!
    NVIDIA! Listen to your customers! We want Free drivers.
    Petition #1. Petition #2. Use your VOICE! Sign the petitions!

  2. #2
    Join Date
    Jun 2008
    Location
    UTC+10
    Posts
    9,941
    Blog Entries
    4

    Default Re: delete the files listed in a plain-text document

    Code:
    rm $(cat list.txt)
    in bash, assuming you wanted to delete every file listed

    Code:
    rm `cat list.txt`
    for people who want to be compatible with Bourne shell.

    If the filename is in a particular line you can use sed (among many choices) to pick out the line.

    Code:
    rm $(sed -n 15p something.txt)
    picks out the 15th line and deletes the file named there.

    That's not the whole story though, you have to be careful about filenames with spaces and other characters, but this will do for a start.

  3. #3
    Join Date
    Jun 2008
    Location
    Brisbane, Australia
    Posts
    207

    Default Re: delete the files listed in a plain-text document

    that was so easy - i didn't even think of it. Some of the most complex things really aren't all that complex. I appreciate you taking the time to read and answer this silly post. I hope to learn more about *nix and bash commands and syntax.
    NVIDIA! Listen to your customers! We want Free drivers.
    Petition #1. Petition #2. Use your VOICE! Sign the petitions!

  4. #4

    Default Re: delete the files listed in a plain-text document

    to make your script resilient, its still better to follow normal way, ie, to quote your variables. Since you are using bash, the normal way to read files is
    Code:
    while read -r line
    do
     rm "$line"
    done < "file_to_delete_files"

  5. #5
    Join Date
    Jun 2008
    Location
    UTC+10
    Posts
    9,941
    Blog Entries
    4

    Default Re: delete the files listed in a plain-text document

    There is actually another trick you can employ and that is to convert the \n's to \0's and feed the output to xargs -0

    Say your file contains:

    Code:
    a b
    c
    d
    e f
    Code:
    tr '\012' '\0' < file.txt | xargs -0 rm
    should do the job. No guarantees, if it breaks you can keep all the pieces, etc, etc. (Yes, it will fail on filenames with newlines in it. )

Posting Permissions

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