Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shell command/script to delete files whose names are in a text file

I have a list of files in a .txt file (say list.txt). I want to delete the files in that list. I haven't done scripting before. Could some give the shell script/command I can use. I have bash shell.

like image 566
Romonov Avatar asked Apr 13 '12 22:04

Romonov


People also ask

How do I delete a file with a specific name in Linux?

Type the rm command, a space, and then the name of the file you want to delete. If the file is not in the current working directory, provide a path to the file's location. You can pass more than one filename to rm . Doing so deletes all of the specified files.

Which DOS command is used to delete a file named list txt?

Syntax: rm command to remove a file.

How do I delete a file that is in use by another command?

To do this, start by opening the Start menu (Windows key), typing run , and hitting Enter. In the dialogue that appears, type cmd and hit Enter again. With the command prompt open, enter del /f filename , where filename is the name of the file or files (you can specify multiple files using commas) you want to delete.

Which Linux command is used to delete all files whose filenames begin with a number from the current user's home directory?

The Linux rm command is used to remove files and directories. (As its name implies, this is a dangerous command, so be careful.)


1 Answers

while read -r filename; do
  rm "$filename"
done <list.txt

is slow.

rm $(<list.txt)

will fail if there are too many arguments.

I think it should work:

xargs -a list.txt -d'\n' rm
like image 189
yazu Avatar answered Sep 28 '22 07:09

yazu