Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have a file that exported a bunch of file names that need to be removed. I need to know how to go about removing each file without having to issue it one at a time at the command line.

I've thought about just cating it inside a for loop, which would probably work, but wanted to know if there was an easier, or even a better solution to doing this.

Thanks.

share|improve this question

8 Answers 8

up vote 4 down vote accepted
rm -rf `cat /path/to/filename`

`` characters can be replace with $()

from bash man page:

   Command Substitution
       Command substitution allows the output of a command to replace the command
       name.  There are two forms:

              $(command)
       or
              `command`

       Bash performs the expansion by executing command and replacing the command
       substitution  with  the  standard output of the command, with any trailing
       newlines deleted.  Embedded newlines are not  deleted,  but  they  may  be
       removed  during  word splitting.  The command substitution $(cat file) can
       be replaced by the equivalent but faster $(< file).

       When the old-style backquote  form  of  substitution  is  used,  backslash
       retains its literal meaning except when followed by $, `, or \.  The first
       backquote not preceded by a backslash terminates the command substitution.
       When  using  the  $(command)  form, all characters between the parentheses
       make up the command; none are treated specially.

       Command substitutions may be nested.  To nest when  using  the  backquoted
       form, escape the inner backquotes with backslashes.

       If the substitution appears within double quotes, word splitting and path‐
       name expansion are not performed on the results.
share|improve this answer
1  
Will fail with filenames that contain whitespace. –  Ignacio Vazquez-Abrams Aug 27 '10 at 16:29
    
My failnames did not have spaces and this worked better in this case. This does need work though to handle spaces in filenames. –  drewrockshard Aug 27 '10 at 16:42
1  
Fair warning, this will also be limited by the number of results you get--anything over 32k will fail on kernels pre-2.6.23. This is generally why xargs is used: cat /path/to/filename | xargs rm -rf. Its also much easier to read and debug. –  Andrew M. Aug 30 '10 at 12:48

No need for cat or a loop:

xargs -d '\n' -a file.list rm
share|improve this answer
$ cat file.list | xargs rm
share|improve this answer
1  
I would prefer xargs - it handles whitespace and special characters as it should. –  Andreas Rehm Aug 27 '10 at 17:30
while read filename ; do rm "$filename" ; done < files.lst
share|improve this answer
perl -lne 'unlink' files_to_remove.txt

If you need to remove lots of files this is several times faster than xargs + rm, and many many times faster than a shell loop.

share|improve this answer

Just for the hell of it, just make the file into a script and execute it handles whitespace and most other awkward characters and is simple. Does not spawn more processes than most of the above methods.

sed -ie 's/^/rm -f "/;s/$/"/' <filename>
sh <filename>
share|improve this answer
2  
You don't need to modify the original file, just do it on the fly: sh -c "$(sed -e 's/\(.*\)/rm -f "\1"/' <filename>)" (also showing an alternative sed script). –  Dennis Williamson Aug 28 '10 at 14:39

Current answers are sufficient - xargs may fail if you have too many files though - in that case you'll need some kind of loop.

Also - when performing this kind of thing it's not a bad idea to , rather than delete, move the files to another folder, so you can manually verify that some weird filenames haven't made some kind of mistake. Then, when you are confident you are okay, just delete the folder.

share|improve this answer

Not the best one, but it works -:)

cat myfile | awk '{print "rm -rf " $0}' | bash
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.