Remove files that are listed in a text file
16,456
Solution 1
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.
Solution 2
No need for cat
or a loop:
xargs -d '\n' -a file.list rm
Solution 3
$ cat file.list | xargs rm
Solution 4
while read filename ; do rm "$filename" ; done < files.lst
Solution 5
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.
Related videos on Youtube
Author by
drewrockshard
Updated on September 17, 2022Comments
-
drewrockshard almost 2 years
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.
-
Ignacio Vazquez-Abrams almost 14 yearsWill fail with filenames that contain whitespace.
-
drewrockshard almost 14 yearsMy failnames did not have spaces and this worked better in this case. This does need work though to handle spaces in filenames.
-
T. Israel almost 14 yearsI would prefer xargs - it handles whitespace and special characters as it should.
-
Dennis Williamson almost 14 yearsYou 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 alternativesed
script). -
Andrew M. almost 14 yearsFair 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. -
Adam Erickson over 5 yearsContrary to the comment of @AndresRehm this did not properly handle whitespace in my case. So, I removed the whitespace in the text file using the Vim command:
:%s/\s\+$//e