I have a list of lines like
/usr/lib/i386-linux-gnu/libavfilter.so
/usr/lib/i386-linux-gnu/libavfilter.so.3
/usr/lib/i386-linux-gnu/libavfilter.so.3.42.103
/usr/lib/i386-linux-gnu/i686/cmov/libavfilter.so
/usr/lib/i386-linux-gnu/i686/cmov/libavfilter.so.3
/usr/lib/i386-linux-gnu/i686/cmov/libavfilter.so.3.42.103
/usr/local/lib/libavfilter.so
/usr/local/lib/libavfilter.so.4
/usr/local/lib/libavfilter.so.4.4.100
How can I remove those files altogether instead of removing them one by one like
rm /usr/lib/i386-linux-gnu/libavfilter.so
rm /usr/lib/i386-linux-gnu/libavfilter.so.3
rm /usr/lib/i386-linux-gnu/libavfilter.so.3.42.103
rm /usr/lib/i386-linux-gnu/i686/cmov/libavfilter.so
rm /usr/lib/i386-linux-gnu/i686/cmov/libavfilter.so.3
rm /usr/lib/i386-linux-gnu/i686/cmov/libavfilter.so.3.42.103
rm /usr/local/lib/libavfilter.so
rm /usr/local/lib/libavfilter.so.4
rm /usr/local/lib/libavfilter.so.4.4.100
?
WARNING! Don't apply below commands without checking output at first
What's your input? A file? A command? Either way,
xargs
should be helpful:... will delete every path for every line in the file. If it's just a command that's outputting a path on each line, pipe it through xargs and it should work well.
Alternatively, use
xargs --arg-file="file.txt" rm
. This saves on pipe and unnecessary cat.If you're looking to do more with each line, you could use a traditional bash while loop:
If your list is the output of
find
, you have another couple of options, including a-delete
option or using-exec rm {} \+
. There are almost certainly a few dozen other viable routes to success. If you're doing automated deletion with find, just check the output before you delete. It won't forgive you your mistakes.Spaces can be a problem with piping things into
xargs
. This might not be an issue for you and your locate but bothlocate
andxargs
have a way of getting around that. They can both choose to use the\0
null character as a delimiter instead of return lines. Yay. In short, your command becomes:Pipe the output to
xargs
:Or if your list is in a file:
At the very basic level this can be done in Python:
In case you have exotic filenames (such as created with
touch with$'\n'backslash
orwith$'\n'newline
, which is very bad idea, but possible ) you could use an input file as so:And handle everything in Python 3 as so:
There's a way of doing that in Python 2 as well.
You can put the files after eachother divided by a space. Like so:
The easiest way is
While you can not use -P, which by default would run as many as possible, but this can be a problem if your list is large. See man pages for further explanation.
One down side for this, if your files have space in the name, this would fail. If that was not the case, you can just use, but if there were files with space in the name you expect, so that in case, make sure you specify the delimiter as newline character.
On Mac OS, this will produce an error, but you are not on Mac OS, so you are good to use this.
Note: Since this is something that can cause loss of data/config, or cripple your install, it is always recommended that you use list the files before actually removing them. Use ls, and if the files are listed properly as expected, change to rm.
use something like notepad++, go to replace (ctrl+r) enter -> find what: \r\n (be sure search mode is set to extended) and replace with: enter a space
then you have all in a line
copy it to a shell and enter rm -rf [paste file/foldernames from notepad++ here]