Remove files that are listed in a text file
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.
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.
No need for cat
or a loop:
xargs -d '\n' -a file.list rm
$ cat file.list | xargs rm
while read filename ; do rm "$filename" ; done < files.lst