Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have a list of directories in a text file and each of them need to be deleted. How can I read in that list into the command (rm -r or rmdir)?

share|improve this question

2 Answers 2

up vote 0 down vote accepted

The "more correct" solution would be the following:

xargs -I{} rm -r {} < files

This calls rm -r, where {} is replaced with the file name.


Why? Piping files with spaces to xargs will result in wrong arguments. Let's say your list of files looks like this:

/path/to/file 1
/path/to/file_2

Then xargs rm -r < list.txt would try to delete /path/to/file, 1 and /path/to/file_2. Definitely not what you want. Always be aware of spaces in paths when piping from and to UNIX / Linux commands.

share|improve this answer

assuming you have paths with spaces in file list.txt - one path per line. Then the following way of invoking xargs will preserve spaces:

cat list.txt | xargs -d \\n rm -r
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.