Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have listed the names of the files which are to be deleted into a file. How can I pass the file to rm command so that it should delete them one by one.

share|improve this question
1  
See FAQ 001 on Greg's Wiki... –  jasonwryan Feb 21 '13 at 7:08
add comment

2 Answers

up vote 8 down vote accepted

If you have one file per line, one way to do it is:

tr '\n' '\0' < list_of_files_to_be_deleted.txt | xargs -0 -r rm --

The file list is given as input to the tr command which changes the file separator from linefeed to the null byte and the xargs command reads files separated by null bytes on input and launches the rm command with the files appended as arguments.

share|improve this answer
    
I'd also suggest using rm -- instead of just rm, so that filenames which may start with dash would not be treated as rm parameters. –  artyom Feb 21 '13 at 7:13
    
It didn't work buddy........ tr: extra operand monu.txt' Try tr --help' for more information. rm: missing operand Try `rm --help' for more information. ........This is all wat i am able to see –  monu Feb 21 '13 at 7:24
1  
Did you perhaps miss the < before the filename? –  Jonas Berlin Feb 21 '13 at 7:24
    
I improved the xargs command a bit also - added -- according to @artyom 's suggestion and added -r to make sure it doesn't run rm if there are no files to remove. –  Jonas Berlin Feb 21 '13 at 7:26
    
Thanks that works –  monu Feb 21 '13 at 7:26
add comment

The above answer is fine in that it goes to great lengths to handle filesnames with spaces and "strange caracters". But the simplest way, if the file names are sane, is just (warning, bashism!):

rm $(< /the/file/with/names)

For regular shell:

rm `cat /the/file/with/names`
share|improve this answer
add comment

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.